编辑:
这是数据 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(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.
请参阅docs of pca ,和fit_transform
关于python - 类型错误 : unsupported operand type(s) for *: 'PCA' and 'float' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39839310/