Creating NumPy Arrays
Create 1-D and 2-D arrays with np.array, np.zeros, np.ones, and np.arange, then inspect their shape and dtype.
What Is a NumPy Array?
NumPy is Python's go-to library for number crunching. Its core is the ndarray — a grid of same-typed values that's far faster and leaner than a list.
import numpy as np
# NumPy arrays are fast, typed, and memory-efficient
print(np.__version__)Creating Arrays from Lists
The simplest way to make an array: pass a list to np.array(). NumPy picks the dtype for you, and a list of lists becomes a 2-D matrix.
import numpy as np
a = np.array([1, 2, 3, 4, 5])
print(a) # [1 2 3 4 5]
print(a.dtype) # int64
m = np.array([[1, 2, 3], [4, 5, 6]])
print(m.shape) # (2, 3)All lessons in this course
- Creating NumPy Arrays
- Array Attributes and Inspection
- Element-Wise Arithmetic
- Array Slicing and Indexing