Skip to content

Instantly share code, notes, and snippets.

@fedden
Created November 6, 2017 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fedden/796a766ead5bb632129bcd047417e8f5 to your computer and use it in GitHub Desktop.
Save fedden/796a766ead5bb632129bcd047417e8f5 to your computer and use it in GitHub Desktop.
Fundamental Linear Algebra Objects
>>> import numpy as np
>>> # Scalars are just a single number.
>>> scalar = 5.0
>>> np.isscalar(scalar)
True
>>> # Vectors are a matrix with one column.
>>> vector = np.arange(10)
>>> vector
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> # Matrices are 2d.
>>> matrix = np.arange(9).reshape((3, 3))
>>> matrix
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> # We need to index these matrices with two values.
>>> first_matrix_element = matrix[0][0]
>>> first_matrix_element
0
>>> # Tensors are n-dimensional matrices.
>>> tensor = np.arange(27).reshape((3, 3, 3))
>>> tensor
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]]])
>>> # We can use Python index slicing to efficently get data.
>>> first_row_of_every_dim = tensor[:,0,:]
>>> first_row_of_every_dim
array([[ 0, 1, 2],
[ 9, 10, 11],
[18, 19, 20]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment