Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Numpy

Notes

A matrix refers to an array with two dimensions. For 3-D or higher dimensional arrays, the term tensor is also commonly used.

In NumPy, dimensions are called axes. This means that if you have a 2D array that looks like this:

[[0., 0., 0.],
 [1., 1., 1.]]

Your array has 2 axes. The first axis has a length of 2 and the second axis has a length of 3.

           axis1
    ┌──────────────────
  a │   [
  x │       [1, 2, 3],
  i │       [4, 5, 6],
  s │       [7, 8, 9]
  0 │   ]

matmul

import numpy as np

A = np.array([[1, 0, 1, 0],
              [-1, 1, 0, 1],
              [-1, 0, 0, 0],
              [0, -1, 0, 0]
              ])

B = np.array([
    [1, 2, 0, 0],
    [-2, 1, 0, 0],
    [1, 0, 0, -1],
    [0, 1, -1, 0],
    ])

print(np.matmul(A, B))

transpose(a, axes=None)

Retrun an array with axes transpose.

import numpy as np

A = np.array([
    [2, -1, 3],
    [1, 1, 1]
])

B = np.array([
    [1, -1],
    [0, 2],
    [-1, 1]
])

# calculate $ (AB)^T $

print( np.transpose(np.matmul(A, B)) )