Skip to content

PRO TIP Block youtube.com at the DNS level — Pi-hole, NextDNS or your hosts file — but allow youtube-nocookie.com and i.ytimg.com. These tutorials keep playing; the rabbit hole does not.

Learning without distractions

Learn to reshape NumPy arrays in 4 minutes! ↔️

15K views on YouTube

#python #coding #numpy

# reshape() = Changes the shape of an array
# w/o altering its underlying data
# array.reshape(rows, columns)

import numpy as np

array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])

array = array.reshape(2, 6)
array = array.reshape(3, 4)
# array = array.reshape(4, 4) # Too many elements
# array = array.reshape(2, 4) # Too few elements
array = array.reshape(4, 3)
array = array.reshape(2, 2, 3) # 3D
array = array.reshape(3, 2, 2) # 3D

# -1 NumPy will automatically infer the correct size for that dimension
array = array.reshape(1, -1)
array = array.reshape(-1, 1)

print(array)