我哪里错了?我试图通过prcomp和我自己进行PCA,我得到不同的结果,你能帮助我吗?
由我自己做的:
>database <- read.csv("E:/R/database.csv", sep=";", dec=",") #it's a 105 rows x 8 columns, each column is a variable
>matrix.cor<-cor(database)
>standardize<-function(x) {(x-mean(x))/sd(x)}
>values.standard<-apply(database, MARGIN=2, FUN=standardize)
>my.eigen<-eigen(matrix.cor)
>loadings<-my.eigen$vectors
>scores<-values.standard %*% loadings
>head (scores, n=10) # I m just posting here the first row scores for the first 6 pc
[,1] [,2] [,3] [,4] [,5] [,6]
2.3342586 2.3426398 -0.9169527 0.80711713 1.1409138 -0.25832090
>sd <-sqrt (my.eigen$values)
>sd
[1] 1.5586078 1.1577093 1.1168477 0.9562853 0.8793033 0.8094500 0.6574788
0.4560247
Run Code Online (Sandbox Code Playgroud)
与PRCOMP一起做:
>database.pca<-prcomp(database, retx=TRUE, center= TRUE, scale=TRUE)
>sd1<-database.pca$sdev …Run Code Online (Sandbox Code Playgroud) 假设我有一个像这样的数据框
Fruit Color Weight
apple red 50
apple red 75
apple green 45
orange orange 80
orange orange 90
orange red 90
Run Code Online (Sandbox Code Playgroud)
我想根据 x 行的 Fruit 和 Color 等于 x+1 行的 Fruit 和 Color 的事实添加 True 或 False 列,如下所示:
Fruit Color Weight Validity
apple red 50 True
apple red 75 False
apple green 45 False
orange orange 80 True
orange orange 90 False
orange red 90 False
Run Code Online (Sandbox Code Playgroud)
我已尝试以下操作,但我猜有一些错误,我得到了错误的结果:
g['Validity'] = (g[['Fruit', 'Color']] == g[['Fruit', 'Color']].shift()).any(axis=1)
Run Code Online (Sandbox Code Playgroud)