NumPy Arrays
Learn about NumPy arrays and how they're used.
Chapter Goals:
- Learn about NumPy arrays and how to initialize them
- Write code to create several NumPy arrays
A. Arrays
NumPy arrays are basically just Python lists with added features. In fact, you can easily convert a Python list to a Numpy array using the np.array
function, which takes in a Python list as its required argument. The function also has quite a few keyword arguments, but the main one to know is dtype
. The dtype
keyword argument takes in a NumPy type and manually casts the array to the specified type.
The code below is an example usage of np.array
to create a 2-D matrix. Note that the array is manually cast to np.float32
.
Press + to interact
import numpy as nparr = np.array([[0, 1, 2], [3, 4, 5]],dtype=np.float32)print(repr(arr))
When the elements of a NumPy array are mixed types, then the array's type will be upcast to ...