Learning Machines

Taught by Patrick Hebron at ITP, Fall 2015


Linear Algebra in Python and Numpy (Part 2):


Continued from Linear Algebra in Python and Numpy (Part 1)

Documentation:

Importing Numpy library:

import numpy as np

Matrix Transposition:

>>> a = np.array( [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ] )
>>> a.T
array([[ 1.,  4.],
       [ 2.,  5.],
       [ 3.,  6.]])

Matrix Addition:

>>> a = np.array( [ [  1.0,  2.0,  3.0 ], [  4.0,  5.0,  6.0 ] ] )
>>> b = np.array( [ [ 10.0, 20.0, 30.0 ], [ 40.0, 50.0, 60.0 ] ] )
>>> a + b
array([[ 11.,  22.,  33.],
       [ 44.,  55.,  66.]])

Matrix Subtraction:

>>> a = np.array( [ [  1.0,  2.0,  3.0 ], [  4.0,  5.0,  6.0 ] ] )
>>> b = np.array( [ [ 10.0, 20.0, 30.0 ], [ 40.0, 50.0, 60.0 ] ] )
>>> a - b
array([[ -9., -18., -27.],
       [-36., -45., -54.]])

Matrix Hadamard Product:

>>> a = np.array( [ [  1.0,  2.0,  3.0 ], [  4.0,  5.0,  6.0 ] ] )
>>> b = np.array( [ [ 10.0, 20.0, 30.0 ], [ 40.0, 50.0, 60.0 ] ] )
>>> a * b
array([[  10.,   40.,   90.],
       [ 160.,  250.,  360.]])

Matrix Multiplication:

>>> a = np.array( [ [ 2, -4, 6 ], [ 5, 7, -3 ] ] )
>>> b = np.array( [ [ 8, -5 ], [ 9, 3 ], [ -1, 4 ] ] )
>>> np.dot( a, b )
array([[-26,   2],
       [106, -16]])

Matrix-Scalar Addition:

>>> a = np.array( [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ] )
>>> a + 3.14
array([[ 4.14,  5.14,  6.14],
       [ 7.14,  8.14,  9.14]])

Matrix-Scalar Subtraction:

>>> a = np.array( [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ] )
>>> a - 3.14
array([[-2.14, -1.14, -0.14],
       [ 0.86,  1.86,  2.86]])

Matrix-Scalar Multiplication:

>>> a = np.array( [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ] )
>>> a * 3.14
array([[  3.14,   6.28,   9.42],
       [ 12.56,  15.7 ,  18.84]])

Matrix-Scalar Division:

>>> a = np.array( [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ] )
>>> a / 3.14
array([[ 0.31847134,  0.63694268,  0.95541401],
       [ 1.27388535,  1.59235669,  1.91082803]])