Principle Component Analysis (PCA)

 Principle Component Analysis (PCA)

This is Machine Learning based algorithm lies under the Unsupervised Learning. This is the simplest code using python


import matplotlib.pyplot as plt
from sklearn import datasets
import numpy as np
iris=datasets.load_iris()
data=np.array(iris.data[:10,:2])
xdata=data[:,0]
ydata=data[:,1]
n=len(xdata)
print(n)
x=np.mean(xdata)
y=np.mean(ydata)
c=np.cov(xdata,ydata)
values, vector = np.linalg.eig(c)
values[::-1].sort() 
vector_subset = vector[:,0:2]
values_subset = np.dot(vector_subset.transpose(),values.transpose()).transpose()
print("Vector Subset is ",vector_subset)
print("Values Subset ",values_subset)
plt.plot(values,vector[:,0])
plt.title('After PCA')
plt.xlabel('PC1')
plt.ylabel('PC2')
plt.legend()
plt.show()
plt.plot(xdata,ydata)
plt.title('Before PCA')
plt.xlabel('PC1')
plt.ylabel('PC2')
plt.legend()
plt.show()


Comments

Popular posts from this blog

Multiple inheritance,friend function and multiple file in oop(object oriented programming)

Concepts of OOP (object oriented programming)

Concepts in c++........