import numpy as np
X = np.array([[10,20,10],
[2,5,2],
[8,17,7],
[9,20,10],
[12,22,11]])
print(X)
X = np.mat(X)
meanVals = X.mean(axis=0)
A = X - meanVals # A is the zero-mean (centered) version of X
C = np.cov(A, rowvar=0) # C is the covarianvce matrix of X
print(C)
# Note that C = (1/(N-1)) A.T*A
N = np.shape(X)[0]
print(np.dot(A.T,A)/(N-1))
eigen_values, eigen_vectors = np.linalg.eig(C)
print("Eigen Values:", eigen_values, "\n")
print("Eigen Vectors:\n", eigen_vectors)
newFeatures = eigen_vectors.T
XTrans = np.dot(newFeatures, A.T)
print(XTrans.T)
reducedFeatures = eigen_vectors[:,0].T
reducedXTrans = np.dot(reducedFeatures, A.T)
print(reducedXTrans.T)
from sklearn import decomposition
pca = decomposition.PCA(svd_solver='randomized')
XTrans = pca.fit_transform(X)
np.set_printoptions(precision=3, suppress=True)
print(XTrans)
M = np.array([[2.5, 2.4],
[0.5, 0.7],
[2.2, 2.9],
[1.9, 2.2],
[3.1, 3.0],
[2.3, 2.7],
[2, 1.6],
[1, 1.1],
[1.5, 1.6],
[1.1, 0.9]])
meanM = M.mean(axis=0)
MC = M - meanM # MC is the zero-mean (centered) version of M
CovM = np.cov(MC, rowvar=0) # CovM is the covarianvce matrix of M
print("Zero Mean Matrix:\n", MC,"\n")
print("Covariance Matrix:\n", CovM,"\n")
eigVals, eigVecs = np.linalg.eig(CovM)
print("Eigenvalues:\n", eigVals,"\n")
print("Eigenvectors:\n", eigVecs,"\n")
newFeatures = eigVecs[:,1].T
print(newFeatures)
MTrans = np.dot(newFeatures, MC.T)
print(np.mat(MTrans).T)
# Instead we can use scikit-learn's decomposition.PCA and specifiy the number of components
pca2 = decomposition.PCA(n_components=1)
MTrans2 = pca2.fit_transform(M)
print(MTrans2)