<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Using sklearn
Step1:  From scratch with eigenvalues
Create covariance matrix
Here, we're applying Bessel's correction (dividing by n-1 instead of n) because our dataset is a sample.
Step2:  Compute eigenvalues & eigenvectors
Step3:  Sort eigenvectors by DESC eigenvalues
Step4:  PC1 is a principal component that captures 0.92% of the data variance, using a combination of a and b (-0.71⋅a - 0.71⋅b). That means that a 1-D graph, using just PC1 would be a good approximation of the 2-D graph since it would account for 92% of the variation in the data. This can be used to identify clusters of data.
Get k features
Step5:  From scratch using Singular values
<ASSISTANT_TASK:>
Python Code:
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
res = pca.fit_transform(df_norm)
res
# Singular values
pca.singular_values_.round(2)
# Eigenvalues
pca.explained_variance_.round(2)
# Eigenvalues/eigenvalues.sum()
pca.explained_variance_ratio_.round(2)
# Eigenvectors
pca.components_
plt.bar(['PC1', 'PC2'], pca.explained_variance_ratio_)
k = 1
df_reduced = np.dot(pca.components_[:k], df_norm.T)
plt.scatter(df_reduced[0], np.ones_like(df_reduced[0]))
Explanation: Using sklearn
End of explanation
cov = np.cov(df_norm.T)
cov
(df_norm.T.dot(df_norm))/(len(df_norm)-1)
Explanation: From scratch with eigenvalues
Create covariance matrix
Here, we're applying Bessel's correction (dividing by n-1 instead of n) because our dataset is a sample.
End of explanation
eigenvalues, eigenvectors = np.linalg.eig(cov)
# Here the column v[:,i] is the eigenvector
# corresponding to the eigenvalue w[i]
# Make it the opposite: v[i, :]
eigenvectors = eigenvectors.T
print("Eigenvalues (explained variance):\n", eigenvalues, "\n")
print("Eigenvectors (components):\n", eigenvectors)
print("Explained variance ratio:\n", eigenvalues/eigenvalues.sum())
Explanation: Compute eigenvalues & eigenvectors
End of explanation
rsort_eigenvalues_idx = eigenvalues.argsort()[::-1]
rsort_eigenvalues_idx
eigenvalues[rsort_eigenvalues_idx]/eigenvalues.sum()
rsort_eigenvectors = eigenvectors[rsort_eigenvalues_idx]
rsort_eigenvectors
Explanation: Sort eigenvectors by DESC eigenvalues
End of explanation
k = 1
df_reduced = np.dot(rsort_eigenvectors[:k], df_norm.T)
df_reduced
plt.scatter(df_reduced[0], np.ones_like(df_reduced[0]))
Explanation: PC1 is a principal component that captures 0.92% of the data variance, using a combination of a and b (-0.71⋅a - 0.71⋅b). That means that a 1-D graph, using just PC1 would be a good approximation of the 2-D graph since it would account for 92% of the variation in the data. This can be used to identify clusters of data.
Get k features
End of explanation
U, s, V = np.linalg.svd(df_norm, full_matrices=False)
# Left singular vectors
U
# Singular values
s.round(2)
# Right singular vectors = eigenvectors
V
# Eigenvalues
n_sample = len(df_norm)
(s**2/(n_sample-1)).round(2)
# Transformed data
k = 2
U[:, :k]*s[:k]
Explanation: From scratch using Singular values
End of explanation
<END_TASK>