Skip to content

Numpy



Generation:

Create an array of given shape

# All zeros in a given shape
Z = np.zeros((m, n_H, n_W, n_C))
# All ones in a given shape
Z = np.ones((m, n_H, n_W, n_C))
# Random values in a given shape.
x = np.random.rand(3,2)
# Return a sample (or samples) from the “standard normal” distribution.
x = np.random.randn(4, 3, 3, 2)

# Fill with given value
x = np.full((2, 2), 10)
# array([[10, 10],
#       [10, 10]])

Permutation - randomly generate a sequence

https://numpy.org/doc/stable/reference/random/generated/numpy.random.permutation.html?

np.random.permutation(10)
# output : array([1, 7, 4, 3, 0, 9, 2, 5, 8, 6]) # random

Remove axes that the length is 1 - Squeeze

https://numpy.org/doc/stable/reference/generated/numpy.squeeze.html

import numpy as np
a = np.array([[
    [[1],[2],[1]],
    [[2],[3],[4]],
    [[1],[2],[1]]
]])
print(f"shape: {a.shape}")
print(a.squeeze())
print(f"shape: {a.squeeze().shape}")
"""output:
shape: (1, 3, 3, 1)
[[1 2 1]
 [2 3 4]
 [1 2 1]]
shape: (3, 3)
"""

Padding:

Use np.pad.

e.g. Pad a list of colored images with zeros. List of images, a of shape (100,32,32,3) with pad = 2  for the 2nd & 3rd dimension, you would do:

# shape of a : (imgs, H, W, RGB channel)
a= np.pad(
    a, 
    ((0,0), (2,2), (2,2), (0,0)), 
    mode='constant', constant_values= (0,0)
)

Practical use case

Create a mask that set True to the max element:

e.g. \(X = \begin{bmatrix}1 && 3 \\4 && 2\end{bmatrix} \quad \rightarrow \quad M =\begin{bmatrix}0 && 0 \\1 && 0\end{bmatrix}\)

M = (X == np.max(X))

Distribute a value averagely into a new array:

e.g. \(dZ = 1 \quad \rightarrow  \quad dZ =\begin{bmatrix}1/4 && 1/4 \\1/4 && 1/4\end{bmatrix}\)

shape = (2,2)
dZ = np.full(shape, dZ / np.prod(shape))

Comments