<jupyter_start><jupyter_text>Importing numpy and pandas<jupyter_code>import numpy as np
import pandas as pd<jupyter_output><empty_output><jupyter_text>STEP1: Lets make a small dataset to explain<jupyter_code>B = np.matrix([[1,2,3,4],[5,5,6,7],[1,4,2,3],[5,3,2,1],[8,1,2,2]])
#Converting the matrix into a dataframe using pandas
df= pd.DataFrame(B, columns= ['a1','a2','a3','a4'])
df<jupyter_output><empty_output><jupyter_text>Step 2: Standardize the dataset<jupyter_code>df_std= (df-df.mean())/(df.std())
df_std<jupyter_output><empty_output><jupyter_text>Step 3: Find the covariance matrix<jupyter_code>#Using the covariance sample formula

cov_mat=np.cov(df_std.T,bias=0)
cov_mat<jupyter_output><empty_output><jupyter_text>verify variance (a1) is as expectedprint('var(a1) (sample formula) :', ((df_std.a1)**2).sum()/4)<jupyter_code>##verify covariance (a1,a2) is as expected
print('covar(a1,a2) (sample formula)):', ((df_std.a1)*(df_std.a2)).sum()/4)<jupyter_output>covar(a1,a2) (sample formula)): -0.3162277660168379<jupyter_text>Step 4: Calculate the Eigen value and Eigen vector<jupyter_code>eigen_val, eigen_vectors= np.linalg.eig(cov_mat)
print(eigen_val)
print(eigen_vectors)
#Sort the eigen values and their corresponding eigen vectors
#Since the eigen values are already sorted in our case we dont need to take that step

n_components= 3
#pick top k (n_components) eigen values and their corresponding eigen vectors\
top_eigen_vectors = eigen_vectors[:,: n_components]
top_eigen_vectors
top_eigen_vectors.shape
np.array(df_std).shape
#(df_std.shape)(n_eigenvectors.shape) = transformed_data.shape
#(5,4)(4,3) = (5,3)


transformed_data= np.matmul(np.array(df_std), top_eigen_vectors)
pd.DataFrame(data= transformed_data, columns= ['principal component ' +str(i+1) for i in range(n_components)])<jupyter_output><empty_output><jupyter_text>Now lets see the result using the sklearn library<jupyter_code>from sklearn.decomposition import PCA
pca= PCA(n_components = n_components)
principalcomponents = pca.fit_transform(df_std)
principalDf= pd.DataFrame(data= principalcomponents, columns = ['principal component' + str(i+1) for i in range(n_components)])
principalDf<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13430127041742287, 'fraction_numerical': 0.03130671506352087, 'mean_word_length': 9.161290322580646, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '258695', 'license': 'Apache-2.0', 'n_tokens_mistral': 866, 'n_tokens_neox': 821, 'n_words': 171, 'path': 'Machinelearningalgorithms/PCAfromScratch.ipynb', 'repo_name': 'Lonakshi/Data-Analysis', 'size': '2204'}