text_plain_output_1.png
from scipy.linalg import eigh

from sklearn import decomposition

from sklearn.preprocessing import StandardScaler

import matplotlib.pyplot as plt

import matplotlib.pyplot as plt

import numpy as np

import numpy as np # linear algebra

import pandas as pd

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import seaborn as sn

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
d0 = pd.read_csv('../input/train.csv')

l = d0['label']
d = d0.drop('label', axis=1)

idx = 150
grid_data = d.iloc[idx].as_matrix().reshape(28, 28)

#Pick first 15k data-points to work on for time-efficiency.
#Exercise: Perform the same analysis on all of 42K data-point

labels = l.head(15000)
data = d.head(15000)

print("the shape of sample data = ", data.shape)

from sklearn.preprocessing import StandardScaler
standardized_data = StandardScaler().fit_transform(data)

sample_data = standardized_data
covar_matrix = np.matmul(sample_data.T, sample_data)

from scipy.linalg import eigh
values, vectors = eigh(covar_matrix, eigvals=(782, 783))
vectors = vectors.T

import matplotlib.pyplot as plt
new_coordinates = np.matmul(vectors, sample_data.T)

import pandas as pd
new_coordinates = np.vstack((new_coordinates, labels)).T
dataframe = pd.DataFrame(data=new_coordinates, columns=('1st_principal', '2nd_principal', 'label'))

import seaborn as sn
sn.FacetGrid(dataframe, hue='label', size=6).map(plt.scatter, '1st_principal', '2nd_principal').add_legend()

from sklearn import decomposition
pca = decomposition.PCA()

pca.n_components = 2
pca_data = pca.fit_transform(sample_data)

pca_data = np.vstack((pca_data.T, labels)).T
pca_df = pd.DataFrame(data=pca_data, columns=('1st_principal', '2nd_principal', 'label'))
sn.FacetGrid(pca_df, hue='label', size=6).map(plt.scatter, '1st_principal', '2nd_principal').add_legend()

pca.n_components = 784
pca_data = pca.fit_transform(sample_data)
percentage_var_explained = pca.explained_variance_ / np.sum(pca.explained_variance_)
cum_var_explained = np.cumsum(percentage_var_explained)
plt.figure(1, figsize=(6, 4))
plt.clf()
plt.plot(cum_var_explained, linewidth=2)
plt.axis('tight')
plt.grid()
plt.xlabel('n_components')
plt.ylabel('Cumulative_explained_variance')
plt.show()