-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrixMath.py
More file actions
38 lines (27 loc) · 1.16 KB
/
Copy pathmatrixMath.py
File metadata and controls
38 lines (27 loc) · 1.16 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
import numpy as np
#calculating Eigen values, Eigen vectors, and the Coefficient Matrix for a matrix
def matrixMath(matrix):
eigenValues, eigenVectors = np.linalg.eig(matrix)
print('The eigenvalues of the covariance matrix are: ')
print(eigenValues)
print('The eigenvectors for covariance matrix are: ')
print(eigenVectors)
coefficentMatrix = eigenVectors.transpose()
print('The coefficient matrix is: ')
print(coefficentMatrix)
return eigenValues, eigenVectors, coefficentMatrix
#calculate mean of matrix
def matrixMean(matrix):
mean = np.mean(matrix)
return mean
#calculate principal components of matrix values and arrays of x,y coordinates
def principalComponents(matrix, array1, array2):
mean = matrixMean(matrix)
eigenVal, eigenVectors, coefficientmatrix = matrixMath(matrix)
pcArray1 = np.matmul(coefficientmatrix, (array1-mean))
pcArray2 = np.matmul(coefficientmatrix, (array2-mean))
print('The Principal Component transformed value for array1 is: ');
print(pcArray1)
print('The Principal Component transformed value for array2 is: ');
print(pcArray2)
return pcArray1, pcArray2