Sahaj.blogs
1 Feb 2024 · StudyPhase 3 · NumPy

NumPy · ndarray basics

The ndarray: creating arrays, shape, ndim, size, reshape, and why one NumPy array beats a list of floats.

I’m back in Python, now with NumPy. Its core object is the ndarray: one contiguous block of memory holding values of a single type, plus metadata describing how to read it:

  • shape: the length along each axis, e.g. (3, 4)
  • ndim: the number of axes
  • size: the total number of elements
  • dtype: the element type
  • strides: how many bytes to jump to move one step along each axis

This is the C++ std::vector idea from last month, wrapped for Python. Because the data is contiguous and typed, operations run in compiled loops instead of the Python interpreter.

Common ways to create arrays: np.array(list), np.zeros, np.ones, np.arange, np.linspace, np.eye.

import numpy as np

a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(a.shape, a.ndim, a.size, a.dtype, a.strides)

print(np.zeros((2, 3)))
print(np.arange(0, 10, 2), np.linspace(0, 1, 5))

b = a.reshape(4, 3)
print(b)
print("shares memory with a:", np.shares_memory(a, b))
print(a.T.shape, a.ravel()[:5])

Output:

(3, 4) 2 12 int64 (32, 8)
[[0. 0. 0.]
 [0. 0. 0.]]
[0 2 4 6 8] [0.   0.25 0.5  0.75 1.  ]
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]
shares memory with a: True
(4, 3) [1 2 3 4 5]

#numpy#arrays

Sources

  1. NumPy: the absolute basics for beginners — NumPy
  2. The N-dimensional array (ndarray) — NumPy
  3. Array creation — NumPy