Numpy basics

Some nuances when using numpy

Range and reshaping

Numpy’s range

vec = np.arange(0,9,1,dtype=np.int16)
vec
array([0, 1, 2, 3, 4, 5, 6, 7, 8], dtype=int16)

Reshaping

vec.reshape((3,3))
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]], dtype=int16)

Sidenote on shuffling with np.random.permutation

np.random.seed(42)
np.random.permutation(vec)
array([7, 1, 5, 0, 8, 2, 4, 3, 6], dtype=int16)

Flattening from multiple dimensions

vec = np.arange(0,27, dtype=np.int8).reshape((3,3,3))
vec
array([[[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8]],

       [[ 9, 10, 11],
        [12, 13, 14],
        [15, 16, 17]],

       [[18, 19, 20],
        [21, 22, 23],
        [24, 25, 26]]], dtype=int8)
vec.reshape(3,-1)
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8],
       [ 9, 10, 11, 12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23, 24, 25, 26]], dtype=int8)
vec.reshape(-1,3).T
array([[ 0,  3,  6,  9, 12, 15, 18, 21, 24],
       [ 1,  4,  7, 10, 13, 16, 19, 22, 25],
       [ 2,  5,  8, 11, 14, 17, 20, 23, 26]], dtype=int8)

back to top

Linear operations and random/zeros initialization

W = np.random.randn(3,2)* 0.01
b = np.zeros((3,1))
X = np.array([[1,2],[3,6]])

Y = np.dot(W,X)+b
Y
array([[ 0.03310587,  0.06621174],
       [-0.02156388, -0.04312775],
       [-0.03343629, -0.06687257]])

back to top

Broadcasting operations

Operations with differing dimensions causes boradcasting of existing values to empty dimensions

print(f'{X = }')
D = np.array([1,2])
print(f'{D = }')
print('\nX/D =\n',np.divide(X,D))
X = array([[1, 2],
       [3, 6]])
D = array([1, 2])

X/D =
 [[1. 1.]
 [3. 3.]]
np.linalg.norm(D)
2.23606797749979

For a matrix np.linalg.norm performs the Frobenius norm \(||X||_{F}\)

np.linalg.norm(X)
7.0710678118654755

Aggregation and output dimension

numpy.sum and other aggregation methods use rows as default axis

print(f'{X=}')
print('axis=1 and keepdims=True')
print(np.sum(X, axis=1, keepdims=True))
print('axis=1 and keepdims=False')
print(np.sum(X, axis=1, keepdims=False))
print('axis=0 and keepdims=True')
print(np.sum(X, axis=0, keepdims=True))
X=array([[1, 2],
       [3, 6]])
axis=1 and keepdims=True
[[3]
 [9]]
axis=1 and keepdims=False
[3 9]
axis=0 and keepdims=True
[[4 8]]

Use numpy.squeeze to remove dimensions of size 1

np.squeeze(np.sum(X, axis=0, keepdims=True))
array([4, 8])

back to top