Skip to main content
Albero Academy

The ndarray

NumPy's core object — the n-dimensional array of same-typed values.

Foundations10 min

Everything in NumPy revolves around one object: the ndarray (n-dimensional array). It's a grid of values, all of the same type, indexed by a tuple of integers. A 1-D array is a vector, 2-D is a matrix, and you can go to any number of dimensions.

Dimensions

1-D, 2-D and beyond
import numpy as np
a = np.array([1, 2, 3]) # 1-D — a vector
b = np.array([[1, 2, 3], [4, 5, 6]]) # 2-D — a matrix (2 rows, 3 cols)
a.ndim # 1 number of dimensions (axes)
b.ndim # 2
b.shape # (2, 3) size along each axis
b.size # 6 total elements

Axes

Each dimension is an axis. For a 2-D array, axis 0 runs down the rows and axis 1 runs across the columns. This idea returns constantly — reshaping, joining and aggregating all talk in terms of axes.

One type per array

Unlike a list, an ndarray is homogeneous — every element shares one dtype. That uniformity is exactly what lets NumPy store it compactly and process it fast:

homogeneous storage
np.array([1, 2, 3]).dtype # dtype('int64')
np.array([1.0, 2, 3]).dtype # dtype('float64') — promoted to fit all
np.array([1, "a"]).dtype # dtype('<U21') — everything becomes a string

Key takeaways

  1. 01The ndarray is a grid of same-typed values indexed by integers.
  2. 021-D is a vector, 2-D a matrix, and arrays can have any number of dimensions.
  3. 03ndim/shape/size describe the structure; each dimension is an axis (0=rows, 1=cols).
  4. 04Arrays are homogeneous — one dtype throughout — which is what makes them fast.

Finished this chapter?

Mark it complete to track your progress through NumPy.