Created
November 6, 2017 19:38
-
-
Save fedden/796a766ead5bb632129bcd047417e8f5 to your computer and use it in GitHub Desktop.
Fundamental Linear Algebra Objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> 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