numpy is python's package for doing math.
https://numpy.org/doc/stable/reference/
Includes special functions like cosine, exponetial, sqrt,
Can generate samples from many types of random variables
has powerful data type to define vectors, matrices, and tensors
allows linear algebra, matrix multiplication and matrix-vector solutions
import numpy as np
print(np.cos(np.pi))
print(np.sqrt(1.21))#square root
print(np.log(np.exp(5.2))) # natural logarithm. Only one you dont' have to specify base.
print(np.exp(4))
-1.0 1.1 5.2 54.598150033144236
vec = np.array([1,2,3]) # list converted to numpy array
print(vec)
# Matrices by converting lists of lists
mat = np.array([[1,2,1],[4,5,9],[1,8,9]]) # list of lists covnerted to numpy array
print('Normal Matrix')
print(mat)
print("Transpose Matrix Each List Up and Down")
print(mat.T) # Calculate Transpose of Matrix
[1 2 3] Normal Matrix [[1 2 1] [4 5 9] [1 8 9]] Transpose Matrix Each List Up and Down [[1 4 1] [2 5 8] [1 9 9]]
# Numpy Array
vec2 = np.arange(0,15) #NumPy Array Range same syntax as range command. Exclusive. Increments by 1 unless thrid number.
print(vec2)
print('')
vec3 = np.arange(3,21,6) # Step Size is 6. Exclusive.
print(vec3)
quizarr = np.arange(4,20,5)
print(quizarr)
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14] [ 3 9 15] [ 4 9 14 19]
produces (Step size) equally spaced values between starting and ending points.
reshape command transforms vector into a matrix.
NOTE: Size must be equal to number of rows * number of columns.
vec4 = np.linspace(0,5,10) # Both Starting and Ending Point Values are Inclusive.
print(vec4)
print('Transformed into Matrix. 5 rows and two columns')
vec4_reshaped = vec4.reshape(5,2) # transform vectors into matrix. 5 rows and two columns
print(vec4_reshaped)
[0. 0.55555556 1.11111111 1.66666667 2.22222222 2.77777778 3.33333333 3.88888889 4.44444444 5. ] Transformed into Matrix. 5 rows and two columns [[0. 0.55555556] [1.11111111 1.66666667] [2.22222222 2.77777778] [3.33333333 3.88888889] [4.44444444 5. ]]
zeros command - Fills matrix with zeros
ones command - Filles matrix with ones.
eye command - Indentiy Matrix. A square matrix filled with zeros excepot for Main diagonal.
mat2 = np.zeros([5,3]) # Matrix that is 5 rows X 3 Columns filled with zeros.
print(mat2)
mat3 = np.ones((3,5)) # Matrix that is 3 Rows x 5 columns. All filled with 1s.
print('')
print(mat3)
mat4 = np.eye(5) #Identities matrix. Square matrix 5X5. All zeros except for main diagonal.
print('')
print(mat4)
[[0. 0. 0.] [0. 0. 0.] [0. 0. 0.] [0. 0. 0.] [0. 0. 0.]] [[1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.]] [[1. 0. 0. 0. 0.] [0. 1. 0. 0. 0.] [0. 0. 1. 0. 0.] [0. 0. 0. 1. 0.] [0. 0. 0. 0. 1.]]
# can add and subtract arrtays together if they're the right size.
vec5 = np.arange(1,6) # 5 entries in both arrays.
vec6 = np.arange(3,8)
print(vec5)
print(vec6)
print(vec5+vec6) # first to first additon second to second etc
print(vec5*vec6)
print(1/vec5)
print(np.sqrt(vec6))
[1 2 3 4 5] [3 4 5 6 7] [ 4 6 8 10 12] [ 3 8 15 24 35] [1. 0.5 0.33333333 0.25 0.2 ] [1.73205081 2. 2.23606798 2.44948974 2.64575131]
print(mat)
print('')
print(vec)
print('')
product = np.matmul(mat,vec) #Matrix Multipication. Requires the dimensions match!
print(product)
[[1 2 1] [4 5 9] [1 8 9]] [1 2 3] [ 8 41 44]
print(np.linalg.solve(mat,product)) # Solving a matrix with an unknown vector.
print('')
print(np.linalg.inv(mat)) # Inverse of the matrix.
[1. 2. 3.] [[ 0.5 0.18518519 -0.24074074] [ 0.5 -0.14814815 0.09259259] [-0.5 0.11111111 0.05555556]]
vec7 = np.array(['blue','red','orange','purple','orange','Red',6]) # Every entry of a numpy array must be of the same datatype.
print(vec7)
print(np.unique(vec7))
['blue' 'red' 'orange' 'purple' 'orange' 'Red' '6'] ['6' 'Red' 'blue' 'orange' 'purple' 'red']
rand_mat = np.random.rand(5,5) # uniform random variable
print(rand_mat)
rand_mat2 = np.random.randn(10,5) # standard nomral random variable.
print('')
print(rand_mat2)
[[0.46005948 0.56087445 0.08133977 0.84320549 0.01675134] [0.96116804 0.32671022 0.30776931 0.83903327 0.5323951 ] [0.2458542 0.78092198 0.75404676 0.68434251 0.89683956] [0.5018215 0.95885962 0.64182175 0.97024157 0.02725458] [0.18726618 0.92271507 0.68501618 0.99573828 0.25375268]] [[-0.50150131 -1.8890062 -1.19661136 0.3761403 -0.32734463] [-0.50573841 0.85682277 -1.06562737 -0.81704593 0.40304628] [ 0.60498287 0.06165385 0.59282345 -0.44223077 -0.79073636] [ 0.23861539 1.53159573 0.48701017 0.24373215 1.61255948] [-0.527789 0.8342649 -1.17910653 -0.65019031 -0.11998135] [-0.24569092 -0.10701505 -0.23808667 -0.72550963 0.06506427] [ 0.71067979 -1.81982283 1.13954905 -1.11148324 0.01808341] [-0.09506058 -1.64954457 -1.45675953 0.03850064 0.37005809] [-0.65511624 0.87671821 0.83086194 -0.04833786 -1.6509809 ] [ 0.74395361 -0.10259543 1.56424815 0.3327845 -1.0472793 ]]
# statistical tools on arrays
print(np.mean(rand_mat))
print(np.std(rand_mat2))
0.5774319567803924 0.8730119773830536
print(np.min(rand_mat))
print(np.max(rand_mat2))
0.016751336994614396 1.6125594775430288
rand_vec = np.random.randn(19) # Standard Normal Random
print(rand_vec)
print(rand_vec[6]) # Index 6 location
[ 1.19089393 -0.64943211 0.75243405 0.09877991 -0.09649433 -0.93376275 -0.32726306 1.26959531 -1.46333472 -0.36299364 0.71958711 0.99172905 0.58677262 -0.69162651 -1.05573612 -1.15767039 -0.88092639 1.33599404 0.91667387] -0.32726305911504067
# Access multiple entries at once using a colon
print(rand_vec[4:9]) #First is inclusive second exclusive
[-0.09649433 -0.93376275 -0.32726306 1.26959531 -1.46333472]
# Access Multiple non-consecutive entries using np.arange
print(np.arange(0,15,3))
print(rand_vec[np.arange(0,15,3)]) # Random vecotr of the first step second step etc.
[ 0 3 6 9 12] [ 1.19089393 0.09877991 -0.32726306 -0.36299364 0.58677262]
print(rand_mat)
print('')
print(rand_mat[1][2]) #Second Row ,Thrid Column
print(rand_mat[1,2]) #Row index column index also works
[[0.46005948 0.56087445 0.08133977 0.84320549 0.01675134] [0.96116804 0.32671022 0.30776931 0.83903327 0.5323951 ] [0.2458542 0.78092198 0.75404676 0.68434251 0.89683956] [0.5018215 0.95885962 0.64182175 0.97024157 0.02725458] [0.18726618 0.92271507 0.68501618 0.99573828 0.25375268]] 0.30776931340681657 0.30776931340681657
print(rand_mat[0:2,1:3]) # Rows zero to one and Columsn 2 to 3
[[0.56087445 0.08133977] [0.32671022 0.30776931]]
#Change values in an array
print(rand_vec)
rand_vec[3:5] = 4 # Set index locations 3 and 4 equal to 4
print('')
print(rand_vec)
rand_vec[3:5] = [1,2] # Set Index location 3 to 1 and Index location 4 to 2
print('')
print(rand_vec)
[ 1.19089393 -0.64943211 0.75243405 0.09877991 -0.09649433 -0.93376275 -0.32726306 1.26959531 -1.46333472 -0.36299364 0.71958711 0.99172905 0.58677262 -0.69162651 -1.05573612 -1.15767039 -0.88092639 1.33599404 0.91667387] [ 1.19089393 -0.64943211 0.75243405 4. 4. -0.93376275 -0.32726306 1.26959531 -1.46333472 -0.36299364 0.71958711 0.99172905 0.58677262 -0.69162651 -1.05573612 -1.15767039 -0.88092639 1.33599404 0.91667387] [ 1.19089393 -0.64943211 0.75243405 1. 2. -0.93376275 -0.32726306 1.26959531 -1.46333472 -0.36299364 0.71958711 0.99172905 0.58677262 -0.69162651 -1.05573612 -1.15767039 -0.88092639 1.33599404 0.91667387]
print(rand_mat)
rand_mat[1:3,3:5] = 0 # Set row 1 and 2 column index 3 and 4 equal to zero
print('')
print(rand_mat)
[[0.46005948 0.56087445 0.08133977 0.84320549 0.01675134] [0.96116804 0.32671022 0.30776931 0.83903327 0.5323951 ] [0.2458542 0.78092198 0.75404676 0.68434251 0.89683956] [0.5018215 0.95885962 0.64182175 0.97024157 0.02725458] [0.18726618 0.92271507 0.68501618 0.99573828 0.25375268]] [[0.46005948 0.56087445 0.08133977 0.84320549 0.01675134] [0.96116804 0.32671022 0.30776931 0. 0. ] [0.2458542 0.78092198 0.75404676 0. 0. ] [0.5018215 0.95885962 0.64182175 0.97024157 0.02725458] [0.18726618 0.92271507 0.68501618 0.99573828 0.25375268]]
print(rand_mat)
sub_mat = rand_mat[0:2,0:3] # rows zero to 1 and columsn 1 to 2
# WARNING! MODIFIES BOTH MATRIX
print('')
print(sub_mat)
sub_mat[:] = 3 #Note: We are adding the columns present at index number 0, index number 1 and index number 2
print('')
print(sub_mat)
[[0.46005948 0.56087445 0.08133977 0.84320549 0.01675134] [0.96116804 0.32671022 0.30776931 0. 0. ] [0.2458542 0.78092198 0.75404676 0. 0. ] [0.5018215 0.95885962 0.64182175 0.97024157 0.02725458] [0.18726618 0.92271507 0.68501618 0.99573828 0.25375268]] [[0.46005948 0.56087445 0.08133977] [0.96116804 0.32671022 0.30776931]] [[3. 3. 3.] [3. 3. 3.]]
print(rand_mat) #Proves that it modifies original matrix as well.
[[3. 3. 3. 0.84320549 0.01675134] [3. 3. 3. 0. 0. ] [0.2458542 0.78092198 0.75404676 0. 0. ] [0.5018215 0.95885962 0.64182175 0.97024157 0.02725458] [0.18726618 0.92271507 0.68501618 0.99573828 0.25375268]]
# To not write over the orignal matrix example w proof print.
sub_mat2 = rand_mat[0:2,0:3].copy()
sub_mat2[:] = 99
print(sub_mat2)
print(rand_mat)
[[99. 99. 99.] [99. 99. 99.]] [[3. 3. 3. 0.84320549 0.01675134] [3. 3. 3. 0. 0. ] [0.2458542 0.78092198 0.75404676 0. 0. ] [0.5018215 0.95885962 0.64182175 0.97024157 0.02725458] [0.18726618 0.92271507 0.68501618 0.99573828 0.25375268]]
# !!!Quiz Example don't understand
qvec = np.arange(10,20)
print(qvec)
qvec2 = qvec[np.arange(0,10,3)]
print(qvec2)
[10 11 12 13 14 15 16 17 18 19] [10 13 16 19]
quizmatrix = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(quizmatrix)
print(quizmatrix[1][1])
[[1 2 3] [4 5 6] [7 8 9]] 5
rand_vec = np.random.randn(15)
print(rand_vec)
print('')
print(rand_vec>0)
print('')
print(rand_vec[rand_vec>0]) # use index as the positive of rand_vec
[ 0.6420723 1.76838765 0.33857888 0.90654107 -0.68284211 0.97933605 0.07409264 -0.42489833 0.63706899 1.41864802 -0.24060139 -1.21712012 -0.04062244 -1.96028964 1.69918674] [ True True True True False True True False True True False False False False True] [0.6420723 1.76838765 0.33857888 0.90654107 0.97933605 0.07409264 0.63706899 1.41864802 1.69918674]
print(rand_mat2)
print('')
print(rand_mat2[rand_mat2>0]) #transforms to an array (vector") since it can't store as a matrix.
[[-0.50150131 -1.8890062 -1.19661136 0.3761403 -0.32734463] [-0.50573841 0.85682277 -1.06562737 -0.81704593 0.40304628] [ 0.60498287 0.06165385 0.59282345 -0.44223077 -0.79073636] [ 0.23861539 1.53159573 0.48701017 0.24373215 1.61255948] [-0.527789 0.8342649 -1.17910653 -0.65019031 -0.11998135] [-0.24569092 -0.10701505 -0.23808667 -0.72550963 0.06506427] [ 0.71067979 -1.81982283 1.13954905 -1.11148324 0.01808341] [-0.09506058 -1.64954457 -1.45675953 0.03850064 0.37005809] [-0.65511624 0.87671821 0.83086194 -0.04833786 -1.6509809 ] [ 0.74395361 -0.10259543 1.56424815 0.3327845 -1.0472793 ]] [0.3761403 0.85682277 0.40304628 0.60498287 0.06165385 0.59282345 0.23861539 1.53159573 0.48701017 0.24373215 1.61255948 0.8342649 0.06506427 0.71067979 1.13954905 0.01808341 0.03850064 0.37005809 0.87671821 0.83086194 0.74395361 1.56424815 0.3327845 ]
print(rand_vec)
print('')
rand_vec[rand_vec>0.5] = -5 # set all values that are greater than 0.5 to negative 5
print(rand_vec)
[ 0.6420723 1.76838765 0.33857888 0.90654107 -0.68284211 0.97933605 0.07409264 -0.42489833 0.63706899 1.41864802 -0.24060139 -1.21712012 -0.04062244 -1.96028964 1.69918674] [-5. -5. 0.33857888 -5. -0.68284211 -5. 0.07409264 -0.42489833 -5. -5. -0.24060139 -1.21712012 -0.04062244 -1.96028964 -5. ]
np.save('saved_file_name',rand_mat2) # can save one and only one NP Array.
# Allows you to store multile NP Arrays in a single zipped file.
np.savez('zipped_file_name',rand_mat=rand_mat,rand_mat2=rand_mat2) #Left is what I want it the name to be in the zip. Right is what it currently is. Ok to be the same name.
loaded_vec = np.load('saved_file_name.npy')
loaded_zip = np.load('zipped_file_name.npz')
print(loaded_vec)
print('')
print(loaded_zip) #print Memory location in RAM
[[-0.50150131 -1.8890062 -1.19661136 0.3761403 -0.32734463] [-0.50573841 0.85682277 -1.06562737 -0.81704593 0.40304628] [ 0.60498287 0.06165385 0.59282345 -0.44223077 -0.79073636] [ 0.23861539 1.53159573 0.48701017 0.24373215 1.61255948] [-0.527789 0.8342649 -1.17910653 -0.65019031 -0.11998135] [-0.24569092 -0.10701505 -0.23808667 -0.72550963 0.06506427] [ 0.71067979 -1.81982283 1.13954905 -1.11148324 0.01808341] [-0.09506058 -1.64954457 -1.45675953 0.03850064 0.37005809] [-0.65511624 0.87671821 0.83086194 -0.04833786 -1.6509809 ] [ 0.74395361 -0.10259543 1.56424815 0.3327845 -1.0472793 ]] <numpy.lib.npyio.NpzFile object at 0x7f9d50882a00>
# List all key names in zip
## NOTE Missing from course.
for k in loaded_zip.keys():
print(k)
rand_mat rand_mat2
print(loaded_zip['rand_mat'])
print('')
print(loaded_zip['rand_mat2'])
[[3. 3. 3. 0.84320549 0.01675134] [3. 3. 3. 0. 0. ] [0.2458542 0.78092198 0.75404676 0. 0. ] [0.5018215 0.95885962 0.64182175 0.97024157 0.02725458] [0.18726618 0.92271507 0.68501618 0.99573828 0.25375268]] [[-0.50150131 -1.8890062 -1.19661136 0.3761403 -0.32734463] [-0.50573841 0.85682277 -1.06562737 -0.81704593 0.40304628] [ 0.60498287 0.06165385 0.59282345 -0.44223077 -0.79073636] [ 0.23861539 1.53159573 0.48701017 0.24373215 1.61255948] [-0.527789 0.8342649 -1.17910653 -0.65019031 -0.11998135] [-0.24569092 -0.10701505 -0.23808667 -0.72550963 0.06506427] [ 0.71067979 -1.81982283 1.13954905 -1.11148324 0.01808341] [-0.09506058 -1.64954457 -1.45675953 0.03850064 0.37005809] [-0.65511624 0.87671821 0.83086194 -0.04833786 -1.6509809 ] [ 0.74395361 -0.10259543 1.56424815 0.3327845 -1.0472793 ]]
# Save as a text file. NOTE Only a single variable.
np.savetxt('text_file_name.txt',rand_mat,delimiter=',') # use a comma as the delimiter
rand_mat_text = np.loadtxt('text_file_name.txt',delimiter=',')
print(rand_mat)
print('')
print(rand_mat_text)
[[3. 3. 3. 0.84320549 0.01675134] [3. 3. 3. 0. 0. ] [0.2458542 0.78092198 0.75404676 0. 0. ] [0.5018215 0.95885962 0.64182175 0.97024157 0.02725458] [0.18726618 0.92271507 0.68501618 0.99573828 0.25375268]] [[3. 3. 3. 0.84320549 0.01675134] [3. 3. 3. 0. 0. ] [0.2458542 0.78092198 0.75404676 0. 0. ] [0.5018215 0.95885962 0.64182175 0.97024157 0.02725458] [0.18726618 0.92271507 0.68501618 0.99573828 0.25375268]]
quizvec1 = np.array([4,7,8,9,10,6,1])
quizvec1[quizvec1>6] = 2
print(quizvec1)
[4 2 2 2 2 6 1]