import numpy as np
L = [1,2,3]
L
[1, 2, 3]
for i in L:
print(L)
[1, 2, 3] [1, 2, 3] [1, 2, 3]
B = np.array([1,2,3])
for e in B:
print(B)
[1 2 3] [1 2 3] [1 2 3]
B = B + B
B
array([2, 4, 6])
B = B*2
B
array([ 4, 8, 12])
L = L*2
L
[1, 2, 3, 1, 2, 3]
L**2
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-8-99008242ace2> in <module> ----> 1 L**2 TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'
L2 = []
for i in L:
L2.append(i*i)
print(L2)
L.append(4)
L
np.sqrt(B)
array([2. , 2.82842712, 3.46410162])
np.log(B)
array([1.38629436, 2.07944154, 2.48490665])
np.exp(B)
array([5.45981500e+01, 2.98095799e+03, 1.62754791e+05])
a = np.array([1,2])
b = np.array([2,1])
dot = 0
for i,j in zip(a,b):
dot += i*j
dot
4
a*b
array([2, 2])
M = np.array([[1,2], [3,4]])
L = [[1,2], [3,4]]
L[0]
[1, 2]
L[0][0]
1
M[0][0]
1
M[0,0]
1
M2 = np.matrix([[1,2], [3,4]]) #works somewhat similarly to np.array but not exactly same.
M2
matrix([[1, 2], [3, 4]])
A = np.array(M2) # converting it into a matrix
A # you get the same matrix but as an array
array([[1, 2], [3, 4]])
A.T # Transposing values with T
array([[1, 3], [2, 4]])
A matrix is just a 2-dimensional numpy array and a vector is a 1-dimensional numpy array. Or you can see it this way: A matrix is actually like a 2-dimensional vector (a 2-dimensional math object that contains numbers) and a vector is a 1-dim mathematical object containing numbers).
A col vector is 3x1 and a row vector is 1x3. All you need to remember is 1-D array, which is a vector and 2 or multiple dimensional arrays that are matrices.
np.array([1,2,3,4]) #tedious way
array([1, 2, 3, 4])
Z = np.zeros(10) #gives us a vector of 10s using function zeros
Z # will give you a vector with length 10 of all zeros
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
Z = np.zeros((10, 10)) # for a matrix of 10x10
Z
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
O = np.ones((10, 10)) # for ones use ones function
O # gives you a 10x10 matrix with all numbers
array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])
RandomNumbers = np.random.random((10,10))
RandomNumbers # in a matrix of size 10x10
array([[0.36025123, 0.72314466, 0.75980308, 0.4889344 , 0.07937359, 0.58874256, 0.23199633, 0.15954813, 0.08774048, 0.54883146], [0.61296478, 0.83142723, 0.42934756, 0.95675646, 0.49010267, 0.93463107, 0.26782182, 0.27939492, 0.00624542, 0.84110478], [0.70859914, 0.20049763, 0.73385319, 0.27699811, 0.35205101, 0.86048545, 0.65022327, 0.0734312 , 0.56611973, 0.77298901], [0.98237141, 0.58668367, 0.05017497, 0.2214889 , 0.9140263 , 0.43997838, 0.18401642, 0.92542465, 0.28174944, 0.15925305], [0.40004748, 0.27031424, 0.7939485 , 0.55662059, 0.57639734, 0.22026038, 0.66965651, 0.46994527, 0.64628966, 0.47208764], [0.25350073, 0.49198554, 0.78046933, 0.80788364, 0.43132166, 0.44066448, 0.23294878, 0.88109595, 0.03925883, 0.79499483], [0.00282458, 0.09053165, 0.28571994, 0.28643215, 0.88058071, 0.95198574, 0.24897644, 0.49677318, 0.54125003, 0.70117 ], [0.69306255, 0.08100459, 0.99756119, 0.10458792, 0.34034728, 0.66861924, 0.57013185, 0.29346688, 0.89079804, 0.78827665], [0.10804648, 0.72548439, 0.31488051, 0.09796583, 0.2034459 , 0.8884706 , 0.52542411, 0.36312296, 0.82495533, 0.1989712 ], [0.24139471, 0.30244504, 0.21736982, 0.05061938, 0.52921445, 0.11172595, 0.57531755, 0.64141966, 0.80048169, 0.02479354]])
GaussianNumbers = np.random.randn((10,10))
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-36-95155644c9cd> in <module> ----> 1 GaussianNumbers = np.random.randn((10,10)) mtrand.pyx in numpy.random.mtrand.RandomState.randn() mtrand.pyx in numpy.random.mtrand.RandomState.standard_normal() _common.pyx in numpy.random._common.cont() TypeError: 'tuple' object cannot be interpreted as an integer
# It failed becuase Randn function takes each of the dimensions as individual objects, while all others took tuples.
GaussianNumbers = np.random.randn(10,10)
GaussianNumbers # will give a gauissian distribution with mean 0 and variance 1
array([[ 0.68873721, 0.40646635, -0.46884281, -0.83962372, 0.83088416, -0.15753662, -0.4931879 , -1.18580301, -1.65746007, 1.03332734], [-0.8391799 , 0.01795441, 1.83797408, 0.7605684 , 0.47645297, 0.23032845, 1.42804264, 0.8264387 , 1.09563666, -1.66961777], [ 0.63255906, 1.25713922, -0.30602351, 1.39022427, 1.10564676, -0.09887949, 0.83629044, 0.3796102 , -0.18988134, 0.198571 ], [-0.70496607, -1.03108825, -0.47010157, 0.53912367, -0.09601299, -1.16649096, -0.43630682, -0.04293193, -0.85203087, 0.48162699], [-0.44425368, 0.27800336, 0.00838862, 1.260695 , 1.03102215, 0.80398706, 0.49112148, 0.03444604, 0.7957862 , 0.70906202], [-0.70750981, -0.30315887, -1.07026434, -2.57104665, -1.40422339, 0.98663442, -1.43032106, 0.74071976, 1.10793193, 0.48403673], [ 0.40077711, 2.03026688, -1.27116926, -2.26781125, -0.82574864, 1.20537537, -0.25784436, 1.44374597, -0.59805266, 0.01893792], [ 0.04064512, 0.36757901, -0.88900875, 0.82229535, 0.94278058, 1.07100589, -0.53000048, 0.55559764, 0.95282399, -0.447541 ], [ 1.43817237, -0.98333888, 0.07721662, 0.04494657, -1.08003903, -2.06406738, -0.62480185, 1.59943142, 0.82769623, 1.00406658], [-0.59786427, -1.31757633, 0.13186859, 0.39142888, -0.2578217 , -0.38322703, -1.00889703, 0.44773885, -0.65285235, 0.04785387]])
GaussianNumbers.mean()
0.04353282914845535
GaussianNumbers.var()
0.8851575805787485
# matrix inverse
A = np.array([[1,2], [3,4]])
A_inverse = np.linalg.inv(A)
A_inverse
array([[-2. , 1. ], [ 1.5, -0.5]])
A*A_inverse
array([[-2. , 2. ], [ 4.5, -2. ]])
A.dot(A_inverse) #gives us an identity matrix
array([[1.00000000e+00, 1.11022302e-16], [0.00000000e+00, 1.00000000e+00]])
A_inverse.dot(A) # note this gives us an identity matrix as well
array([[1.0000000e+00, 4.4408921e-16], [0.0000000e+00, 1.0000000e+00]])
# matrix determinant
np.linalg.det(A)
-2.0000000000000004
np.diag(A) # gives us diagonal elements in a vector
array([1, 4])
# Note this: if you pass a 2D array to diag, you get a 1D array (as above), if you pass a 1D array
# 2 get a 2D array where off diagonals are zero as below
np.diag([1,2])
array([[1, 0], [0, 2]])
#Doing an outer product
a = np.array([1,2])
b = np.array([3,4])
np.outer(a,b)
array([[3, 4], [6, 8]])
# dot product ,as you may recall is also known as inner product
np.inner(a,b)
11
a.dot(b) # same as above
11
# Matrix trace is the sum of the diagonals of a matrix
np.diag(A).sum() # See In [92]
5
np.trace(A)
5
# Eigenvalues and Eigenvectors
X = np.random.randn(100,3)
covariance = np.cov(X)
covariance.shape
(100, 100)
covariance = np.cov(X.T) # transpose X
covariance
array([[ 0.93667764, -0.04833602, -0.04214239], [-0.04833602, 0.96862658, 0.02143732], [-0.04214239, 0.02143732, 0.89064545]])
or 2. eigenvalues, eigenvectors = np.eigh(C) # While a Hermitian matrix ia a conjugate of transpose of itself
So, Symmetricc $A = A^T$, while Hermitian matrix of $A = A^H$ or simply $A^H$ is conjugate transpose of A
np.linalg.eigh(covariance) #gives a tuple
(array([0.86512642, 0.91291087, 1.01791237]), array([[ 0.54664956, 0.58209677, -0.60194486], [ 0.08270375, 0.67781206, 0.73056888], [ 0.83326727, -0.44914826, 0.32238408]]))
type(np.linalg.eigh(covariance))
tuple
np.linalg.eig(covariance) # will give us a tuple, first tuple contains 3 eigenvalues and sector tuple contains eigenvectors stored in columns
(array([1.01791237, 0.91291087, 0.86512642]), array([[-0.60194486, 0.58209677, 0.54664956], [ 0.73056888, 0.67781206, 0.08270375], [ 0.32238408, -0.44914826, 0.83326727]]))
type(np.linalg.eig(covariance))
tuple
# You may have notice they can be in different order, try running it again to check it out for yourself!
A
array([[1, 2], [3, 4]])
b
array([3, 4])
b = np.array([1,2])
b
array([1, 2])
x = np.linalg.inv(A).dot(b)
x
array([2.22044605e-16, 5.00000000e-01])
x = np.linalg.solve(A, b) # much intuitive and efficient way to do it. also more accurate.
x
array([0. , 0.5])
Problem description: In a party, singles pay a entry ticket of 2.50 dollars and couples paid for a single 4.00 dollars . At this big rock concert event, some 20,000 tickets were sold and 75,500 dollars was made by the organizers. How many singles and how many couples partied that night?
1.1 X1 = number of singles, X2 = number of couples => 1.2 X1 + X2 = 20,000 => 1.3 2.5X1 + 4.00X2 = 75,000
As a matrix this is how it looks like: $$
= \begin{bmatrix} 20000 \\ 75000 \\ \end{bmatrix}$$
party_goers = np.array([[1,1], [2.5,4]]) # This is our 2D matrix A
collections = np.array([20000, 75000]) #This is our 1D matrix / vector b
np.linalg.solve(party_goers, collections)
print("There were ", np.linalg.solve(party_goers, collections)[0],\
" singles and ", np.linalg.solve(party_goers, collections)[1] ,\
" couples; and apparently some one cheated on their partner that night ;)")
There were 3333.333333333331 singles and 16666.666666666668 couples; and apparently some one cheated on their partner that night ;)
Don't go away as I explain and we learn together the following, we first do a recap so you refresh your thoughts again on: