python transform_python-sklearn中的“ transform”和“ fit_transform”有什么区别

这里的区别仅当您已经在矩阵上计算了PCA时,才可以使用pca.transform

In [12]: pc2 = RandomizedPCA(n_components=3)

In [13]: pc2.transform(X) # can't transform because it does not know how to do it.

---------------------------------------------------------------------------

AttributeError Traceback (most recent call last)

in ()

----> 1 pc2.transform(X)

/usr/local/lib/python3.4/dist-packages/sklearn/decomposition/pca.py in transform(self, X, y)

714 # XXX remove scipy.sparse support here in 0.16

715 X = atleast2d_or_csr(X)

--> 716 if self.mean_ is not None:

717 X = X - self.mean_

718

AttributeError: 'RandomizedPCA' object has no attribute 'mean_'

In [14]: pc2.ftransform(X)

pc2.fit pc2.fit_transform

In [14]: pc2.fit_transform(X)

Out[14]:

array([[-1.38340578, -0.2935787 ],

[-2.22189802, 0.25133484],

[-3.6053038 , -0.04224385],

[ 1.38340578, 0.2935787 ],

[ 2.22189802, -0.25133484],

[ 3.6053038 , 0.04224385]])

如果要使用.transform,则需要向PCA教授转换规则

In [20]: pca = RandomizedPCA(n_components=3)

In [21]: pca.fit(X)

Out[21]:

RandomizedPCA(copy=True, iterated_power=3, n_components=3, random_state=None,

whiten=False)

In [22]: pca.transform(z)

Out[22]:

array([[ 2.76681156, 0.58715739],

[ 1.92831932, 1.13207093],

[ 0.54491354, 0.83849224],

[ 5.53362311, 1.17431479],

[ 6.37211535, 0.62940125],

[ 7.75552113, 0.92297994]])

In [23]:

特别地,PCA变换将通过矩阵X的PCA分解获得的基数更改应用于矩阵Z。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值