vec = np.arange(0,9,1,dtype=np.int16)
vecarray([0, 1, 2, 3, 4, 5, 6, 7, 8], dtype=int16)Numpy’s range
Reshaping
Sidenote on shuffling with np.random.permutation
Flattening from multiple dimensions
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)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)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)array([[ 0.03310587,  0.06621174],
       [-0.02156388, -0.04312775],
       [-0.03343629, -0.06687257]])Operations with differing dimensions causes boradcasting of existing values to empty dimensions
X = array([[1, 2],
       [3, 6]])
D = array([1, 2])
X/D =
 [[1. 1.]
 [3. 3.]]For a matrix np.linalg.norm performs the Frobenius norm \(||X||_{F}\)
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