Dimensionality Reduction with PCA (SVD)


In [1]:
%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

In [2]:
iris = datasets.load_iris()
pca = decomposition.PCA(n_components=2)
new_dim = pca.fit_transform(iris.data)

In [3]:
df = pd.DataFrame(new_dim, columns=['X', 'Y'])
df['label'] = iris.target
df.head()


Out[3]:
X Y label
0 -2.684207 0.326607 0
1 -2.715391 -0.169557 0
2 -2.889820 -0.137346 0
3 -2.746437 -0.311124 0
4 -2.728593 0.333925 0

In [4]:
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))


Out[4]:
<matplotlib.legend.Legend at 0x10b807e48>