类型错误:对于*不支持的操作数类型(S): 'PCA' 和 '浮动'
问题描述:
编辑:类型错误:对于*不支持的操作数类型(S): 'PCA' 和 '浮动'
下面是数据CSV的头:
Fresh Milk Grocery Frozen Detergents_Paper Delicatessen
0 12669 9656 7561 214 2674 1338
1 7057 9810 9568 1762 3293 1776
2 6353 8808 7684 2405 3516 7844
3 13265 1196 4221 6404 507 1788
4 22615 5410 7198 3915 1777 5185
错误我看到:
TypeError: unsupported operand type(s) for *: 'PCA' and 'float'
代码:
from sklearn.decomposition import PCA
log_data = np.log(data)
# TODO: Apply PCA to the good data with the same number of dimensions as features
pca = PCA(n_components=4)
# TODO: Apply a PCA transformation to the sample log-data
pca_samples = pca.fit(log_data)
# Generate PCA results plot
pca_results = rs.pca_results(good_data, pca)
display(pd.DataFrame(np.round(pca_samples, 4), columns = pca_results.index.values))
它抱怨最后一行
数据来自已显示工作正常的csv。
答
PCA.fit()
tansforms模型就地并返回self
,以便您可以链接其他模型操作。所以,
pca_samples = pca.fit(log_data)
pca_samples
只是另一种参考pca
。
答
pca.fit(X[, y])
只适合与X的模型,并返回self
,这是pca本身。
既然要与
pd.DataFrame(np.round(pca_samples, 4), columns = pca_results.index.values))
因此,要获得转换后的数据,你应该叫pca.fit_transform()
fit_transform(X[, y]) Fit the model with X and apply the dimensionality reduction on X.
加CSV的代表性部分文件的顶部作为'StringIO'对象加载并作为示例的一部分加载。 –
请包括堆栈跟踪,以便我们知道问题所在。 – tdelaney
什么是rs?不会显示引发错误的'np.log'行。这个脚本还有更多吗? – tdelaney