<jupyter_start><jupyter_text>Principel Component Analysis (PCA)PCA is a mathematical technique to reduce redundancy in the dataPCA is one of the well-known algorithm for Dimensionality Reduction  PCA Visualizationhttp://setosa.io/ev/principal-component-analysis/  Activity: are the countries in great UK different in terms of food?In the table is the average consumption of 17 types of food in grams per person per week for every country in the UK.It would be great if we represent diffrence among UK countries based on the food they eat visually<jupyter_code>import pandas as pd
from sklearn.decomposition import PCA
from sklearn import preprocessing


df = pd.read_excel('pca_uk.xlsx')

X = np.array([df[i].values for i in df.columns if i != 'Features'])

# print(X)

pca = PCA(n_components=2)
X_r = pca.fit_transform(X)
print(X_r)
for k, (i,j) in enumerate(zip(X_r[:, 0], X_r[:, 1])):
    plt.scatter(i, j)
    plt.text(i+0.3, j+0.3, df.columns[:-1][k])

plt.show()<jupyter_output>[[-144.99315218   -2.53299944]
 [ 477.39163882  -58.90186182]
 [ -91.869339    286.08178613]
 [-240.52914764 -224.64692488]]<jupyter_text>Activity: PCA StepsFollow the steps here and write a function that computes the principle component for iris dataset https://sebastianraschka.com/Articles/2014_pca_step_by_step.htmlSteps: 1- Subtract column mean from feature matrix2- Calculate the covariance of centered matrix3- Calculate the eigenvalue and eigenvector of covariance matrix4- Return the first K (two for example) column of matrix multiplication of centerned matrix with eigenvector matrixCompare the result of custom function with PCA in sklearnCalculate the correlation of the first two principle component How much of the dataset information is preserved in the first two components?Hint: use `pca.explained_variance_ratio_`<jupyter_code>from sklearn import datasets
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats


iris = datasets.load_iris()

X = iris.data
y = iris.target

# PCA computation by sklearn
pca = PCA(n_components=2)
X_r = pca.fit_transform(X)
print(pca.explained_variance_)
print(pca.explained_variance_ratio_)
print(pca.explained_variance_ratio_.cumsum())

print('Correlation of PCA Component:')
print(scipy.stats.pearsonr(X_r[:, 0], X_r[:, 1]))

# our function to comapre 
def PCA_calculation(data, n_comp=2):
    M = np.mean(data, axis=0)
    # center columns by subtracting column means
    C = X - M
    # calculate covariance matrix of centered matrix
    V = np.cov(C.T)
    # eigendecomposition of covariance matrix
    eig_value, eig_vector = np.linalg.eig(V)
    print(eig_vector)
    print(eig_value)
    P = np.dot(C, eig_vector)[:, :n_comp]
    return P


PCA_calculation(X, 2)<jupyter_output>[4.22484077 0.24224357]
[0.92461621 0.05301557]
[0.92461621 0.97763178]
Correlation of PCA Component:
(-2.3569080325843714e-17, 1.0)
[[ 0.36158968 -0.65653988 -0.58099728  0.31725455]
 [-0.08226889 -0.72971237  0.59641809 -0.32409435]
 [ 0.85657211  0.1757674   0.07252408 -0.47971899]
 [ 0.35884393  0.07470647  0.54906091  0.75112056]]
[4.22484077 0.24224357 0.07852391 0.02368303]
{'fraction_non_alphanumeric': 0.09629394995248654, 'fraction_numerical': 0.11403230915426038, 'mean_word_length': 4.721014492753623, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 1, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '198020', 'license': 'MIT', 'n_tokens_mistral': 1347, 'n_tokens_neox': 1109, 'n_words': 331, 'path': 'site/public/courses/DS-2.1/Notebooks/PCA_slides.ipynb', 'repo_name': 'KitsuneNoctus/makeschool', 'size': '3157'}