As Quiz 3 PCA Solution PDF
As Quiz 3 PCA Solution PDF
import pandas as pd
from sklearn.decomposition import PCA
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
Out[5]: status_id num_reactions num_comments num_shares num_likes num_loves num_wows num_hahas num_sads
Q5) Which of the variables in the dataset is not significant for doing Principal
Component Analysis?
Now, let us go ahead and drop the column 'status_id' as that variable is of no use to us when we are doing
Principal Component Analysis.
In [6]: df_new = df.drop(['status_id'],axis = 1)
df_new.head()
Out[6]: num_reactions num_comments num_shares num_likes num_loves num_wows num_hahas num_sads num_angrys status_link status_phot
1 150 0 0 150 0 0 0 0 0 0
3 111 0 0 111 0 0 0 0 0 0
4 213 0 0 204 9 0 0 0 0 0
In [ ]:
Q6) After doing z-score scaling on the dataset, what is the value of the 2nd observation of the
variable ‘ num_hahas’?
In [7]: from scipy.stats import zscore
df_new=df_new.apply(zscore)
df_new.head()
Out[7]: num_reactions num_comments num_shares num_likes num_loves num_wows num_hahas num_sads num_angrys status_link status_phot
0 0.646104 0.323350 1.686879 0.482727 1.983266 0.196196 0.076713 0.473570 -0.155748 -0.094957 -1.24599
1 -0.173192 -0.252206 -0.304144 -0.144720 -0.318454 -0.147879 -0.176010 -0.152587 -0.155748 -0.094957 0.80257
2 -0.006738 0.013089 0.129017 -0.024571 0.206938 -0.033187 0.076713 -0.152587 -0.155748 -0.094957 -1.24599
3 -0.257499 -0.252206 -0.304144 -0.231495 -0.318454 -0.147879 -0.176010 -0.152587 -0.155748 -0.094957 0.80257
4 -0.037003 -0.252206 -0.304144 -0.024571 -0.093286 -0.147879 -0.176010 -0.152587 -0.155748 -0.094957 0.80257
ANS - The value of the 2nd observation of the variable ‘ num_hahas’ is -0.176010 .
In [ ]:
Q7) Apply PCA taking all features and extract 6 components and Find out the eigenvector of
the 5th component
In [9]: #Apply PCA taking all features
from sklearn.decomposition import PCA
pca = PCA(n_components=6, random_state=123)
pca_transformed = pca.fit_transform(df_new)
In [ ]:
In [ ]:
In [ ]:
In [ ]: