PCA using build in function
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.preprocessing import MinMaxScaler
import numpy as np
iris=datasets.load_iris()
data=np.array(iris.data[:10,:2])
scaler=MinMaxScaler()
scaler.fit(data)
data=scaler.transform(data)
print("old data ",data)
plt.plot(data[:,0],data[:,1])
plt.title('Before PCA')
plt.show()
from sklearn.decomposition import PCA
pca=PCA(n_components=2)
pca.fit(data)
newData=pca.transform(data)
print(data.shape,newData.shape)
print("new Data ",newData)
plt.plot(data[:,0],data[:,1])
plt.title('After PCA')
plt.show()
Comments
Post a Comment