降维分析3:稀疏主成分分析-python代码-R代码

目录

总体还是感觉R语言比较好用

1.python代码

2.R代码

下面是官网给出的介绍

官网还给出了两个实例:

得到的结果包括


总体还是感觉R语言比较好用

1.python代码

先记录一下,主要参考 https://scikit-learn.org/stable/modules/generated/sklearn.decomposition.SparsePCA.html#sklearn.decomposition.SparsePCA

输入的数据为n*m的矩阵,m为变量的个数,n为多少组变量值

下面为官方稀疏主成分分析的代码

>>> import numpy as np
>>> from sklearn.datasets import make_friedman1
>>> from sklearn.decomposition import SparsePCA
>>> X, _ = make_friedman1(n_samples=200, n_features=30, random_state=0)
>>> transformer = SparsePCA(n_components=5, random_state=0)
>>> transformer.fit(X)
SparsePCA(...)
>>> X_transformed = transformer.transform(X)
>>> X_transformed.shape
(200, 5)
>>> # most values in the components_ are zero (sparsity)
>>> np.mean(transformer.components_ == 0)
0.9666...

下面为自己微调后运行成功的代码


import numpy as np
from sklearn.datasets import make_friedman1
from sklearn.decomposition import SparsePCA
data_spca = np.loadtxt('C:\\Users\\ASUS\\shuju.txt')#导入数据

transformer = SparsePCA(n_components=5, random_state=0)
transformer.fit(data_spca1)#稀疏主成分分析
X_transformed = transformer.transform(data_spca1)#原始数据转换后的矩阵
a = (transformer.components_)#稀疏主成分分析后得到的loading matrix
print(transformer.components_)

 

2.R代码

采用的是elasticnet包中的spca函数

下面是官网给出的介绍

spca(x, K, para, type=c("predictor","Gram"),sparse=c("penalty","varnum"), use.corr=FALSE, lambda=1e-6,max.iter=200, trace=FALSE, eps.conv=1e-3)

x: A matrix. It can be the predictor matrix or the sample covariance/correlation matrix.预测矩阵???这个我还不懂是什么意思??   或者原数据集的协方差或相关系数矩阵

K:Number of components 得到的稀疏主元的个数,如果是5,则会得到5个稀疏主成分,每个稀疏主成分均包含各个变量的载荷。

para:A vector of length K. All elements should be positive. If sparse="varnum", the elements integers.

item{type}{If type="predictor", x is the predictor matrix.If type="Gram", the function asks the user to provide the sample covariance or correlation matrix. }
item{sparse}{If sparse="penalty", para is a vector of 1-norm
  penalty parameters. 如果sparse=“penalty”,就给出一个数组,里面包含k个惩罚系数,

If sparse="varnum", para defines the number of
  sparse loadings to be obtained. 如果sparse=“varnum”,就给出一个数组,里面包含每个稀疏主元所得到的非零载荷的数量}

\item{lambda}{Quadratic penalty parameter. Default value is 1e-6.}
\item{use.corr}{Perform PCA on the correlation matrix? This option is
  only effective when the argument type is set "data".}
\item{max.iter}{Maximum number of iterations.}
\item{trace}{If TRUE, prints out its progress.}
\item{eps.conv}{Convergence criterion.}

官网还给出了两个实例:

spca(pitprops,K=6,type="Gram",sparse="penalty",trace=TRUE,para=c(0.06,0.16,0.1,0.5,0.5,0.5))###sparse=“penalty”,就给出一个数组,里面包含k个惩罚系数,

pitprops为原始数据的相关系数矩阵,因为type=“Gram”,也可以使用原始数据的协方差矩阵

spca(pitprops,K=6,type="Gram",sparse="varnum",trace=TRUE,para=c(7,4,4,1,1,1)) ###sparse=“varnum”,就给出一个数组,里面包含每个稀疏主元所得到的非零载荷的数量}

 

得到的结果包括

The below are some quantities which the user may be interested in: 
1.\item{loadings}{

The loadings of the sparse PCs载荷矩阵
}
2.\item{pev}{ 
Percentage of explained variance每个稀疏主成分的贡献率
}
3.\item{var.all}{ 
Total variance of the predictors预测变量的贡献率

你可能感兴趣的:(降维分析)