0% found this document useful (0 votes)
5 views2 pages

PCA Implementation on Iris Dataset

Uploaded by

Ruchi Rewri
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

PCA Implementation on Iris Dataset

Uploaded by

Ruchi Rewri
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import numpy as np

def PCA(X , num_components):

#Step-1
X_meaned = X - [Link](X , axis = 0)

#Step-2
cov_mat = [Link](X_meaned , rowvar = False)

#Step-3
eigen_values , eigen_vectors = [Link](cov_mat)

#Step-4
sorted_index = [Link](eigen_values)[::-1]
sorted_eigenvalue = eigen_values[sorted_index]
sorted_eigenvectors = eigen_vectors[:,sorted_index]

#Step-5
eigenvector_subset = sorted_eigenvectors[:,0:num_components]

#Step-6
X_reduced = [Link](eigenvector_subset.transpose() , X_meaned.transp
ose() ).transpose()

return X_reduced

#Get the IRIS dataset


import pandas as pd

url = "[Link]
[Link]"
data = pd.read_csv(url, names=['sepal length','sepal width','petal leng
th','petal width','target'])

#prepare the data


x = [Link][:,0:4]

#prepare the target


target = [Link][:,4]

#Applying it to PCA function


mat_reduced = PCA(x , 2)

#Creating a Pandas DataFrame of reduced Dataset


principal_df = [Link](mat_reduced , columns = ['PC1','PC2'])
#Concat it with target variable to create a complete Dataset
principal_df = [Link]([principal_df , [Link](target)] , axis =
1)

import seaborn as sb
import [Link] as plt

[Link](figsize = (6,6))
[Link](data = principal_df , x = 'PC1',y = 'PC2' , hue = 'targe
t' , s = 60 , palette= 'icefire')

You might also like