Skip to content

Latest commit

 

History

History
100 lines (75 loc) · 2.09 KB

File metadata and controls

100 lines (75 loc) · 2.09 KB

Note: This is a generated markdown export from the Jupyter notebook file dimensionality_reduction_pca.ipynb. You can also view the notebook with the nbviewer from Jupyter.

Dimensionality Reduction with PCA (SVD)

%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

from sklearn import decomposition, datasets
from matplotlib.colors import ListedColormap
iris = datasets.load_iris()
pca = decomposition.PCA(n_components=2)
new_dim = pca.fit_transform(iris.data)
df = pd.DataFrame(new_dim, columns=['X', 'Y'])
df['label'] = iris.target
df.head()
X Y label
0 -2.684126 0.319397 0
1 -2.714142 -0.177001 0
2 -2.888991 -0.144949 0
3 -2.745343 -0.318299 0
4 -2.728717 0.326755 0
fig = plt.figure()
fig.suptitle('PCA (SVD)', fontsize=14, fontweight='bold')
ax = fig.add_subplot(111)

plt.scatter(df[df.label == 0].X, df[df.label == 0].Y, color='red', label=iris.target_names[0])
plt.scatter(df[df.label == 1].X, df[df.label == 1].Y, color='blue', label=iris.target_names[1])
plt.scatter(df[df.label == 2].X, df[df.label == 2].Y, color='green', label=iris.target_names[2])

_ = plt.legend(bbox_to_anchor=(1.25, 1))

png