Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
87 views3 pages

Aim: To Implement Principal Component Analysis (PCA) Algorithm A Compare The Results With Available Library Modules

This document describes an experiment implementing Principal Component Analysis (PCA) on iris data. It loads the iris data, standardizes the features, calculates the covariance matrix and eigenvectors/values. It projects the data onto the first two principal components and prints the results.

Uploaded by

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

Aim: To Implement Principal Component Analysis (PCA) Algorithm A Compare The Results With Available Library Modules

This document describes an experiment implementing Principal Component Analysis (PCA) on iris data. It loads the iris data, standardizes the features, calculates the covariance matrix and eigenvectors/values. It projects the data onto the first two principal components and prints the results.

Uploaded by

BSSudani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Enrollment No.

: 201803100920017

Experiment 5

Aim : To implement Principal Component Analysis (PCA) algorithm a compare the


results with available library modules.

import numpy as np

import pandas as pd

A = pd.read_csv('iris.csv')

print(A)

X = A.iloc[:,0:4]

y = A.iloc[:,-1]

M = np.mean(X.T, axis=1)

S = np.std(X.T, axis=1)

C = (X - M) / S

print("Standardized Data: \n")

print('\n',C[:5])

V = np.cov(C.T)

print("Covariance Matrix: \n")

print('\n',V[:5])

values, vectors = np.linalg.eig(V)

print("Eigen Values: \n")

print('\n',values[:5])

print("Eigen Vectors: \n")

print('\n',vectors[:5])

variances = []

for i in range(len(values)):

variances.append(values[i] / np.sum(values))

print("Sum Of Variances: \n")

print('\n',np.sum(variances))
CGPIT/B.TECH (CE)/D2D/SEM-7/Machine Intelligence
Enrollment No.: 201803100920017

print("Variances: \n")

print('\n', variances)

proj_1 = np.dot(C,(vectors.T[0]))

proj_2 = np.dot(C,(vectors.T[1]))

result = pd.DataFrame(proj_1, columns=['PC1'])

result['PC2'] = proj_2

result['Y'] = y

print("Final Result After PCA: \n")

print(result.head(149))

Output:

CGPIT/B.TECH (CE)/D2D/SEM-7/Machine Intelligence


Enrollment No.: 201803100920017

CGPIT/B.TECH (CE)/D2D/SEM-7/Machine Intelligence

You might also like