sklearn库中PCA返回矩阵与其它PCA自编程序返回矩阵不一样

查看了sklearn的参数设置,使用,都没有遇到问题,但是有关细节的说明,例如输入矩阵的行是特征维还是列?归一化后的不同?这方面的细节通过几个例题验算了一下。有几点要分享一下:

#1 人工智能程序,例如调用PCA,一定是每一行是一个样本的特征
#2 其它程序如Python,MATLAB,每一列是一个样本的特征,列数是样本数
另外,用两种方法计算了两道例题。例1,两种方法结果一样。下面例2的结果不一样。百思不得其解?

// An highlighted block
#例2 自编PCA,得转换后矩阵xsub11
#例2 自编PCA,得转换后矩阵xsub11
import numpy as np
X = [[2, 0, -1.4],
    [2.2, 0.2, -1.5],
    [2.4, 0.1, -1],
    [1.9, 0, -1.2]] #3维特征
xt = np.array(X).T #特征维数就是行数,n个特征就是n行
print(xt)
#去均值得yt
xbar = xt.sum(axis=1) / xt.shape[1]
print('xbar=',xbar)
yt = np.zeros((3,4))
for j in range(4):
    yt[:,j] = xt[:,j] - xbar
print(yt)
#计算去均值后的协方差矩阵
y = np.cov(yt)    
print('协方差矩阵',y)
w, v = np.linalg.eig(y)
print('特征值:{}\n特征向量:{}'.format(w,v))
#print(v[:,0])
#z = np.dot(y,v[:,0])
#print(z)
#s = np.dot(w[0],v[:,0])
#print(s)
xsub11 = np.dot(v[:,0:2].T,xt)
print('前两个大特征值对应的特征向量v[:,0:2]\n',v[:,0:2])
print('xsub11=',xsub11)
// An highlighted block
[[ 2.   2.2  2.4  1.9]
 [ 0.   0.2  0.1  0. ]
 [-1.4 -1.5 -1.  -1.2]]
xbar= [ 2.125  0.075 -1.275]
[[-0.125  0.075  0.275 -0.225]
 [-0.075  0.125  0.025 -0.075]
 [-0.125 -0.225  0.275  0.075]]
协方差矩阵 [[ 0.04916667  0.01416667  0.01916667]
 [ 0.01416667  0.00916667 -0.00583333]
 [ 0.01916667 -0.00583333  0.04916667]]
特征值:[0.06896846 0.03692827 0.00160326]
特征向量:[[-0.72998323 -0.57467514  0.36996347]
 [-0.10708354 -0.43845355 -0.89235172]
 [-0.67502415  0.69101879 -0.25852549]]
前两个大特征值对应的特征向量v[:,0:2]
 [[-0.72998323 -0.57467514]
 [-0.10708354 -0.43845355]
 [-0.67502415  0.69101879]]
xsub11= [[-0.51493265 -0.61484358 -1.08764395 -0.57693915]
 [-2.11677659 -2.3885042  -2.11408448 -1.92110531]]

// An highlighted block
#调用PCA,得转换后矩阵xt_pca,不等于xtsub1,非常奇怪。
import sklearn
from sklearn.decomposition import PCA
X = [[2, 0, -1.4],
    [2.2, 0.2, -1.5],
    [2.4, 0.1, -1],
    [1.9, 0, -1.2]]
xt = np.array(X)
print(xt)#4样本4行
xt_pca = PCA(n_components=2).fit_transform(xt)
print('降维后剩2个特征:',xt_pca.shape)
print('xt_pca=',xt_pca)
xt1=PCA(n_components=2).fit(xt)
xt12=xt1.components_#行向量,是特征向量
print(xt1.components_)
print('xt1.explained_variance_ratio_',xt1.explained_variance_ratio_)
xtsub1 = np.dot(xt,xt12.T)
print('xtsub1 = ',xtsub1)#
#????????????????????为什么PCA算出的转换后的矩阵xt_pca不等于xsub1
#输出
[[ 2.   0.  -1.4]
 [ 2.2  0.2 -1.5]
 [ 2.4  0.1 -1. ]
 [ 1.9  0.  -1.2]]
降维后剩2个特征: (4, 2)
xt_pca= [[-0.6993308  -0.09544319]
 [-0.3188894   1.3185727 ]
 [ 1.4814423  -0.10945236]
 [-0.4632221  -1.11367715]]
[[ 0.72998323  0.10708354  0.67502415]
 [ 0.57467514  0.43845355 -0.69101879]]
xt1.explained_variance_ratio_ [0.6415671 0.3435188]
xtsub1 = [[0.51493265 2.11677659]
 [0.61484358 2.3885042 ]
 [1.08764395 2.11408448]
 [0.57693915 1.92110531]]

#归一化之后也不一致

// An highlighted block
#例3 调用PCA,得转换后矩阵xt_pca,不等于xtsub1,非常奇怪。
X = [[1, -1, 0, 2., 0],
    [-2., 0., 0, -1, 1]]
xt = np.array(X).T
print(xt)#4样本4行
xt_pca = PCA(n_components=1,whiten='True').fit_transform(xt)
print('降维后剩1个特征:',xt_pca.shape)
print('xt_pca=',xt_pca)
xt1=PCA(n_components=1,whiten='True').fit(xt)
xt12=xt1.components_#行向量,是特征向量
print(xt1.components_)
print('xt1.explained_variance_ratio_',xt1.explained_variance_ratio_)
xtsub1 = np.dot(xt,xt12.T)
print(xtsub1)#

谁能解答一下?

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值