Introduction to NumPy arraysΒΆ
NumPy (Numerical Python) is an essential library for working with dimensional arrays (matrices and vectors). Because NumPy has optimizations it is usually much faster than working with Python when processing data.
With an activated virtual environment, install the library:
$ pip install numpy
import numpy as np
Arrays are homogenous, that is, its items must be of the same type (unlike a Python list)
Create arrays with listsΒΆ
The most straightforward way to create a NumPy array is by passing a Python list to np.array(). A single list produces a one-dimensional array (vector), while a list of lists produces a multi-dimensional array (matrix). The .shape attribute reveals the dimensions, which is critical for understanding how the array will behave in mathematical operations and when feeding data into ML models.
# single dimension arrays
array = np.array([1,2,3,4,5])
array
# two dimensions
array = np.array([[1,2],[2,2]])
print(array.shape)
array
Create arrays with built-in functionsΒΆ
NumPy provides several convenience functions for creating arrays without explicit lists: np.random.rand() for random floats between 0 and 1, np.full() for filling an array with a constant value, np.ones() for arrays of ones (commonly used for bias terms), and np.arange() for evenly-spaced sequences. These functions accept shape tuples, making it easy to create arrays of any dimensionality.
# 2x2 array with random floats of 0 to 1
np.random.rand(2,2)
# fill out the 3x4 array with the number 7
np.full((3,4), 8)
# using the number 1 is so common that `ones` is a function!
np.ones((4,4))
# a simple sequence from 0 to 4
np.arange(0,5)
# a sequence with three steps from 0 to 27
np.arange(0, 27, 3)