ndarray basics
The ndarray is NumPy's grid of numbers; it can be a line, a table, or even a cube of values.
In this page:
Syntax
import numpy as np
arr = np.array([value1, value2, value3])
arr.shape
arr.dtype
ndarray basics
The ndarray is a homogeneous, fixed-size, N-dimensional container. A 1-D array is like a vector, a 2-D array is like a matrix, and higher dimensions work the same way. Every array knows its own shape and data type.
Note:
Nested lists of equal length become a 2-D array; ragged lists do not.
Example: ndarray basics
import numpy as np
v = np.array([1, 2, 3])
m = np.array([[1, 2, 3], [4, 5, 6]])
print("Vector:", v, "shape", v.shape)
print("Matrix:")
print(m)
print("Matrix shape:", m.shape)
# Output:
# Vector: [1 2 3] shape (3,)
# Matrix:
# [[1 2 3]
# [4 5 6]]
# Matrix shape: (2, 3)
Related Topics
Common Mistakes
- Passing ragged nested lists and expecting a clean 2-D array
- Confusing the array itself with a Python list
- Forgetting that NumPy indexes from 0
Chapter Summary
- ndarray is homogeneous and N-dimensional
- Nested lists build multi-dimensional arrays
- Arrays expose .shape and .dtype
- Indexing starts at 0
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: