import matplotlib.pyplot as plt import numpy as np #import pandas as pd from array import * #vectors x1 and x2 drawn independently from Gaussian distribution with zero mean #and unit variance row = np.arange(0,1,0.001) #CREATE AN ARRAY FOR EACH COMPONENT for i in row: A = np.random.normal(loc=0.0, scale=1.0, size=100) x1 = np.array([A]) for i in row: B = np.random.normal(loc=0.0, scale=1.0, size=100) x2 = np.array([B]) #print x2.shape for i in row: C = A + B x3 = np.array([C]) #print x3.shape array = np.array([[x1], [x2], [x3]]) #print array #print array.shape #CALCULATE COVARIANCE #matrix = np.asmatrix(array) #print matrix #matrix has to be square to calculate covar #totalcovar = np.cov(matrix) #print '\n', "the complete covariance matrix is:", '\n', totalcovar #find covariance matrix between each dimension covar_x1_x2 = np.cov(x1, x2) covar_x1_x3 = np.cov(x1, x3) covar_x2_x3 = np.cov(x2, x3) print '\n',"covariances for x1 & x2 are:", '\n',covar_x1_x2 print '\n',"covariances for x1 & x3 are:", '\n',covar_x1_x3 print '\n',"covariances for x2 & x3 are:", '\n',covar_x2_x3 #check that covar is an array: #print "covar shape =", covar_x1_x2.shape #FIND EIGEN VALUES AND EIGEN VECTORS evalx1x2, evecx1x2 = np.linalg.eig(covar_x1_x2) evalx1x3, evecx1x3 = np.linalg.eig(covar_x1_x3) evalx2x3, evecx2x3 = np.linalg.eig(covar_x2_x3) print '\n',"eigen values for x1 & x2 are:", '\n',evalx1x2 print '\n',"eigen vectors for x1 & x2 are:", '\n',evecx1x2 print '\n',"eigen values for x1 & x3 are:", '\n',evalx1x3 print '\n',"eigen vectors for x1 & x3 are:", '\n',evecx1x3 print '\n',"eigen values for x2 & x3 are:", '\n',evalx2x3 print '\n',"eigen vectors for x2 & x3 are:", '\n',evecx2x3 #CREATE A FEATURE VECTOR FROM 3D EIGEN VECTORS #sort eigen vectors by eigen value #right now I have an array for each eigen value and eigen vector for each component #columns in evecxx correspond to i in eval. #sort dimension x1,x2 index = evalx1x2.argsort() evalx1x2 = evalx1x2[index] evecx1x2 = evecx1x2[:,index] print '\n',"sorted eigen values & vectors for x1,x2:" print '\n', evalx1x2 print '\n', evecx1x2 #sort dimesion x1, x3 index = evalx1x3.argsort() evalx1x2 = evalx1x3[index] evecx1x2 = evecx1x3[:,index] print '\n',"sorted eigen values & vectors for x1,x3:" print '\n', evalx1x3 print '\n', evecx1x3 #sort dimension x2, x3 index = evalx2x3.argsort() evalx1x2 = evalx2x3[index] evecx1x2 = evecx2x3[:,index] print '\n',"sorted eigen values & vectors for x2,x3:" print '\n', evalx2x3 print '\n', evecx2x3 #select highest values as PC F print '\n',"Here are my Feature Vectors:" PC1 = np.delete(evecx1x2, 1, 1) print '\n', PC1 PC2 = np.delete(evecx1x3, 1, 1) print '\n', PC2 PC3 = np.delete(evecx2x3, 1, 1) print '\n', PC3 #RECONSTRUCT DATA #FinalData = RowFeatureVector x RowDataAdjust #RowFeatureVector = matrix of eigen vectors transposed #RowDataAdjust = mean-adjusted data transposed RowFeatVec1 = PC1.transpose RowFeatVec2 = PC2.transpose RowFeatVec3 = PC3.transpose #print PC1.shape #RowDataAdjust1 = x1.transpose #RowDataAdjust2 = x2.transpose #RowDataAdjust3 = x3.transpose print "this runs" FinalData1 = np.multiply(PC1, x1) FinalData2 = np.multiply(PC2, x2) FinalData3 = np.multiply(PC3, x3) print '\n',"Final Data 1, 2 & 3:" print '\n', FinalData1, '\n', FinalData2, '\n', FinalData3 #PLOT plt.plot(FinalData1, 'r--') plt.show() #write output to a txt and csv file #location = "/Users/eroyall/Documents/Modules/vectorsgen.txt" #location2= "/Users/eroyall/Documents/Modules/vectorsgen.csv" #np.savetxt(location, vector, fmt='%2.5f', delimiter=' ') #np.savetxt(location2, vector, fmt='%2.5f', delimiter=' ')