Question
In this question, you will be using the eigenanalysis to perform PCA. (1 point) Standardize df using the StandardScaler package overwrite it as df (1
In this question, you will be using the eigenanalysis to perform PCA. (1 point) Standardize df using the StandardScaler package overwrite it as df (1 point) The standardized data frame is not a data frame. So, convert it as a data frame and keep using the same name df. Notice that the column labels have been reset. So, rename the column names as colnames. (1 point) Save the covariance matrix of df as cov. Then, print out the covariance matrix of df where each matrix entry must be rounded to two decimal places. (1 point) Save the eigenvalues and eigenvectors of cov as eval and evec, respectively. (5 points) Print out the first five principal components and their variances of the following form: The PC__ is _______________________________________________, its variance is ___________, % variance is ___________%, % cumulative variance is ___________% where the first blank is for the numbers of principal components, the second blank is for the principal components as a horizontal array, the third blank is for the variances of principal components, the fourth blank is for the proportions of variances explained by the principal components. For example, the proportion of variance explained by 3 ( %(3) ) can be calculated as %(3)=eval(3)The sum of all the eigenvalues100(%) the fifth blank is for the cumulative proportions of variances explained by the pricipal components. For example, the cumulative proportion for 3 ( %(3) ) can be calculated as %(3)=eval(1)+eval(2)+eval(3)The sum of all the eigenvalues100(%) Round the entries of the principal components and variances to two decimal place. Answer the proportions as percentages with one decimal place. Preliminaries: Lecture Notebook Part 1, Lecture Notebook Part 2, Printing Variables within a Text, for Loops, Frequently-Used Dataframe Methods Hint: Replace the ????? part with your solution. import numpy as np from numpy import linalg as LA from sklearn.preprocessing import StandardScaler sc = ????? df = sc.?????(df) df = pd.?????(df) df.????? = colnames cov = pd.?????.?????(df) print('Covariance matrix: ', np.?????(?????,?????)) eval, evec = ?????.?????(cov) eval = np.?????(?????,?????) evec = np.?????(?????,?????) k = 5 for i in ?????(?????): print(f' The PC{?????} is {?????}, its variance is {?????}, % variance is {np.round(100*?????/?????,1)}%, and , % cumulative variance is {np.round(100*?????/?????,1)}%')
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