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
= [1, 2, 3, 4, 5]
my_list = np.array(my_list)
my_array print(my_array) # Output: [1 2 3 4 5]
= (6, 7, 8, 9, 10)
my_tuple = np.array(my_tuple)
my_array_from_tuple print(my_array_from_tuple) # Output: [ 6 7 8 9 10]
You can also create arrays of specific shapes and data types:
= np.zeros((2, 3))
zeros_array print(zeros_array)
= np.ones((3, 3))
ones_array print(ones_array)
= np.full((2, 2), 7)
full_array print(full_array)
= np.arange(0, 10, 2) #Start, stop, step
arange_array print(arange_array)
= np.linspace(0, 1, 5) #Start, stop, number of samples
linspace_array print(linspace_array)
#Creating a 3x3 identity matrix
= np.identity(3)
identity_matrix print(identity_matrix)
#Creating an array of random numbers
= np.random.rand(2,3) # 2 rows, 3 columns
random_array print(random_array)
Array Attributes and Data Types
NumPy arrays have several useful attributes that provide information about their shape, data type, and size:
= np.array([[1, 2, 3], [4, 5, 6]])
my_array 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:
= np.array([1, 2, 3], dtype=np.float64)
float_array print(float_array.dtype) # Output: float64
Array Operations
NumPy’s power lies in its ability to perform efficient element-wise operations on arrays:
= np.array([1, 2, 3])
array1 = np.array([4, 5, 6])
array2
= array1 + array2
addition_result print(addition_result) # Output: [5 7 9]
= array1 * array2
multiplication_result print(multiplication_result) # Output: [ 4 10 18]
= np.dot(array1, array2) #or array1 @ array2
dot_product print(dot_product) # Output: 32
#Broadcasting: Adding a scalar to an array
= array1 + 5
scalar_addition print(scalar_addition) # Output: [6 7 8]
Array Slicing and Indexing
Accessing and manipulating portions of arrays is straightforward using slicing:
= np.array([[1, 2, 3], [4, 5, 6], [7,8,9]])
my_array 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
= my_array > 5
boolean_array 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:
= np.arange(12)
my_array = my_array.reshape((3, 4))
reshaped_array print(reshaped_array)
#Flattening an array
= reshaped_array.flatten()
flattened_array 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.