Program - 3
Develop a program to implement Principal Component Analysis (PCA) for reducing the
dimensionality of the Iris dataset from 4 features to 2.
import numpy as np
import pandas as pd
from sklearn import datasets
from [Link] import PCA
import [Link] as plt
# Load the Iris dataset
iris = datasets.load_iris()
X = [Link]
y = [Link]
# Create a DataFrame for better visualization
df = [Link](X, columns=iris.feature_names)
#df to array
X = df.to_numpy()
np.set_printoptions(linewidth=[Link])
print('original data top 3 rows')
print(X[:3])
mean = [Link](X,axis=0)
print('mean value =',[Link](mean,2))
std_dev=[Link](X,axis=0)
print('Standard deviation = ',[Link](std_dev,2))
#step 1 standardization of x matrix
X_standardized = (X - [Link](X, axis=0)) / [Link](X, axis=0)
print(' Standardization matrix top 3 rows \n', [Link](X_standardized[:3],2))
#step 2 take the tanspose of matrix X_stadradized by .T i.m x*xT
cov_matrix = [Link](X_standardized.T)
print('covarnce = \n', [Link](cov_matrix,2))
# Step 3: Compute the eigenvalues and eigenvectors
eigenvalues, eigenvectors = [Link](cov_matrix)
print('eigen values =',[Link](eigenvalues,2))
print('eigen vector =\n',[Link](eigenvectors,2))
# Step 4: Sort eigenvalues and select principal components
sorted_index = [Link](eigenvalues)[::-1]
sorted_eigenvectors = eigenvectors[:, sorted_index]
print('sorted eigen values =',[Link](sorted_index,2))
print('soted eigen vector =\n',[Link](sorted_eigenvectors,2))
# Select the top 2 eigenvectors
eigenvectors_subset = sorted_eigenvectors[:, :2]
print('soted eigen vector =\n',[Link](eigenvectors_subset,2))
# Step 5: Transform the data
X_reduced = [Link](X_standardized, eigenvectors_subset)
print("X_reduced \n",X_reduced [:3])
df_pca = [Link](X_reduced, columns=['PCA1', 'PCA2'])
df_pca['target'] = y
print(df_pca.sample(5))
# Plot the PCA-transformed data
[Link](figsize=(10, 7))
colors = ['r', 'g', 'b']
for target, color in zip(df_pca['target'].unique(), colors):
subset = df_pca[df_pca['target'] == target]
[Link](subset['PCA1'], subset['PCA2'], color=color, label=iris.target_names[target])
[Link]('Principal Component 1')
[Link]('Principal Component 2')
[Link]('PCA of Iris Dataset')
[Link]()
[Link]()
ouput:
original data top 3 rows
[[5.1 3.5 1.4 0.2]
[4.9 3. 1.4 0.2]
[4.7 3.2 1.3 0.2]]
mean value = [5.84 3.06 3.76 1.2 ]
Standard deviation = [0.83 0.43 1.76 0.76]
Standardization matrix top 3 rows
[[-0.9 1.02 -1.34 -1.32]
[-1.14 -0.13 -1.34 -1.32]
[-1.39 0.33 -1.4 -1.32]
covarnce =
[[ 1.01 -0.12 0.88 0.82]
[-0.12 1.01 -0.43 -0.37]
[ 0.88 -0.43 1.01 0.97]
[ 0.82 -0.37 0.97 1.01]]
eigen values = [2.94 0.92 0.15 0.02]
eigen vector =
[[ 0.52 -0.38 -0.72 0.26]
[-0.27 -0.92 0.24 -0.12]
[ 0.58 -0.02 0.14 -0.8 ]
[ 0.56 -0.07 0.63 0.52]]
sorted eigen values = [0 1 2 3]
soted eigen vector =
[[ 0.52 -0.38 -0.72 0.26]
[-0.27 -0.92 0.24 -0.12]
[ 0.58 -0.02 0.14 -0.8 ]
[ 0.56 -0.07 0.63 0.52]]
soted eigen vector =
[[ 0.52 -0.38]
[-0.27 -0.92]
[ 0.58 -0.02]
[ 0.56 -0.07]]
X_reduced
[[-2.26470281 -0.4800266 ]
[-2.08096115 0.67413356]
[-2.36422905 0.34190802]]
PCA1 PCA2 target
70 0.737683 -0.396572 1
118 3.310696 -0.017781 2
9 -2.184328 0.469014 0
149 0.960656 0.024332 2
25 -1.951846 0.625619 0