<jupyter_start><jupyter_text>Principal component Analysis in just 3 steps.... Again... we are using the iris data set<jupyter_code>from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
import pandas as pd
import numpy as np
iris = load_iris()

df = pd.DataFrame(iris.data, columns=iris.feature_names)


df['species'] = pd.Categorical.from_codes(iris.target, iris.target_names)
df.head()
# splitting into data (X) and class labels (Y)

X = df.iloc[:, 0:4].values
Y = df.iloc[:, 4].values<jupyter_output><empty_output><jupyter_text>Exploratory viz<jupyter_code>from matplotlib import pyplot as plt
%matplotlib notebook
plt.style.use('ggplot')
fig, AX = plt.subplots(2, 2, figsize =(10, 8))
ax = AX.flatten()
names = ['Setotsa', 'Virginica', 'Versicolor']
for i in range(4):
    col = df.columns[i]
    df.groupby("species")[col].hist(alpha=0.5, ax = ax[i])
    axi = ax[i]
    axi.set_xlabel(col);
    
    
plt.legend(labels = names, loc = 0);<jupyter_output><empty_output><jupyter_text>Satandardizing the data<jupyter_code>from sklearn.preprocessing import StandardScaler
X_std = StandardScaler().fit_transform(X)<jupyter_output><empty_output><jupyter_text>PCA model<jupyter_code>from sklearn.decomposition import PCA as sklearnPCA
clf = sklearnPCA(n_components=2)
Y_pred = clf.fit_transform(X_std)
fig, ax = plt.subplots(figsize = (8,6))
for lab, col in zip(('setosa', 'versicolor', 'virginica'), ('darkviolet', 'c', 'black')):
    ax.scatter(Y_pred[Y==lab, 0],
              Y_pred[Y==lab, 1], label =lab, color = col)
ax.set_xlabel('Principal component 1')
ax.set_ylabel('Principal component 2');<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14285714285714285, 'fraction_numerical': 0.012004801920768308, 'mean_word_length': 5.860082304526749, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '450391', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 656, 'n_tokens_neox': 625, 'n_words': 136, 'path': 'PCA.ipynb', 'repo_name': 'trallard/Job_reproducible', 'size': '1666'}