NumPy Arrays

numpy
Published

February 22, 2024

Creating NumPy Arrays

The simplest way to create a NumPy array is using the array() function, which takes a list or tuple as input:

import numpy as np

my_list = [1, 2, 3, 4, 5]
my_array = np.array(my_list)
print(my_array)  # Output: [1 2 3 4 5]

my_tuple = (6, 7, 8, 9, 10)
my_array_from_tuple = np.array(my_tuple)
print(my_array_from_tuple) # Output: [ 6  7  8  9 10]

You can also create arrays of specific shapes and data types:

zeros_array = np.zeros((2, 3))
print(zeros_array)

ones_array = np.ones((3, 3))
print(ones_array)

full_array = np.full((2, 2), 7)
print(full_array)

arange_array = np.arange(0, 10, 2) #Start, stop, step
print(arange_array)

linspace_array = np.linspace(0, 1, 5) #Start, stop, number of samples
print(linspace_array)

#Creating a 3x3 identity matrix
identity_matrix = np.identity(3)
print(identity_matrix)

#Creating an array of random numbers
random_array = np.random.rand(2,3) # 2 rows, 3 columns
print(random_array)

Array Attributes and Data Types

NumPy arrays have several useful attributes that provide information about their shape, data type, and size:

my_array = np.array([[1, 2, 3], [4, 5, 6]])
print(my_array.shape)      # Output: (2, 3) - Dimensions of the array
print(my_array.ndim)      # Output: 2 - Number of dimensions
print(my_array.dtype)     # Output: int64 - Data type of the array elements
print(my_array.size)      # Output: 6 - Total number of elements
print(my_array.itemsize) # Output: 8 - Size of each element in bytes

You can also explicitly specify the data type when creating an array:

float_array = np.array([1, 2, 3], dtype=np.float64)
print(float_array.dtype)  # Output: float64

Array Operations

NumPy’s power lies in its ability to perform efficient element-wise operations on arrays:

array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])

addition_result = array1 + array2
print(addition_result)  # Output: [5 7 9]

multiplication_result = array1 * array2
print(multiplication_result)  # Output: [ 4 10 18]

dot_product = np.dot(array1, array2) #or array1 @ array2
print(dot_product)  # Output: 32


#Broadcasting:  Adding a scalar to an array
scalar_addition = array1 + 5
print(scalar_addition) # Output: [6 7 8]

Array Slicing and Indexing

Accessing and manipulating portions of arrays is straightforward using slicing:

my_array = np.array([[1, 2, 3], [4, 5, 6], [7,8,9]])
print(my_array[0, 1])  # Accessing the element at row 0, column 1 (Output: 2)
print(my_array[1:3, 0:2]) #Slicing a subarray (Output: [[4 5] [7 8]])

#Boolean indexing: Selecting elements based on a condition
boolean_array = my_array > 5
print(boolean_array) # Output: [[False False False] [False False  True] [ True  True  True]]
print(my_array[boolean_array]) # Output: [6 7 8 9]

Reshaping Arrays

The reshape() function allows you to change the dimensions of an array:

my_array = np.arange(12)
reshaped_array = my_array.reshape((3, 4))
print(reshaped_array)

#Flattening an array
flattened_array = reshaped_array.flatten()
print(flattened_array)

These are just some of the fundamental aspects of NumPy arrays. Exploring further into NumPy’s capabilities will significantly enhance your ability to work with numerical data in Python. There are many more powerful functionalities including array manipulation, linear algebra, statistics, and more available within the NumPy library.