Answered step by step
Verified Expert Solution
Question
1 Approved Answer
solve q 3 for me and import pandas as pd import numpy as np import seaborn as sb import matplotlib.pyplot as plt def PCA (
solve q for me and import pandas as pd
import numpy as np
import seaborn as sb
import matplotlib.pyplot as plt
def PCAX numcomponents:
X: D x N matrix to be consistant with the description in the textbook
#Step
Xmean npmeanX axis keepdimsTrue #treat X as a DxN matrix treat each column as a sample and each row as a feature
### Fill in ################################
#Step
covmat
############################################
#Step
eigenvalues eigenvectors nplinalg.eighcovmat
### sort from highest to lowest eigenvalues
eigenvectors eigenvectors: npargsorteigenvalues
eigenvalues eigenvaluesnpargsorteigenvalues
### Fill in ################################
Z
############################################
return Z
if namemain:
#Get the IRIS dataset
url https:archiveics.uci.edumlmachinelearningdatabasesirisirisdata"
data pdreadcsvurl namessepal length','sepal width','petal length','petal width','target'
#prepare the data
x npasarraydatailoc::T #change dimension from NxD DxN
#prepare the target
target data.iloc:
#Applying it to PCA function
z PCAx
#Creating a Pandas DataFrame of latent codes
latentcode pdDataFramezT columns PCPC
#Concat it with target variable to create a complete Dataset
latentcode pdconcatlatentcode pdDataFrametarget axis
### Visualize data in PCA dimensions
pltfigurefigsize
sbscatterplotdata latentcode x PCy PC hue 'target' s palette 'icefire'
pltgrid
pltshow
pltclose this is python code
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started