Title :¶
Exercise: PCA 1
Description :¶
Goals: To produce the plot below after PCA transformation (Using Scikit Learn)
Data Description:¶
Instructions:¶
Perform (i.e fit and transform) Principal Components Analysis (PCA) on the given dataset X
Hints:¶
PCA.fit() -Note that this is unsupervised learning method.
Note: This exercise is auto-graded and you can try multiple attempts.
PCA Exercise 1¶
In [1]:
import numpy as np
import matplotlib.pylab as plt
%matplotlib inline
Loading data¶
In [2]:
X = np.loadtxt('PCA1.csv')
Standardize your $X$ matrix.
In [0]:
from sklearn.preprocessing import StandardScaler
X = ___
In [3]:
plt.plot(X[:,0], X[:,1], "+", alpha=0.8)
plt.xticks(fontsize=15)
plt.yticks(fontsize=15)
plt.xlabel('X1', fontsize=15)
plt.ylabel('X2', fontsize=15)
plt.grid()
plt.show()
PCA¶
Transform your $X$ matrix using PCA with 2 components.
In [0]:
### edTest(test_PCA_fit) ###
from sklearn.decomposition import PCA
pca = PCA(___).fit(___)
X_pca = pca.transform(___)
print("PCA shape:", X_pca.shape)
Visualization¶
In [7]:
plt.figure(figsize=(6, 5))
plt.plot(X[:,0], X[:,1], "+", alpha=0.8, label='Original')
plt.plot(X_pca[:,0], X_pca[:,1], "+", alpha=0.8, label='Transformed')
plt.xticks(fontsize=15)
plt.yticks(fontsize=15)
plt.xlabel('X1', fontsize=15)
plt.ylabel('X2', fontsize=15)
plt.legend(fontsize=12)
plt.grid()
plt.show()
In [0]: