python - 类型错误 : unsupported operand type(s) for *: 'PCA' and 'float'

标签 python scikit-learn sklearn-pandas

编辑:

这是数据 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/

相关文章:

python - 如何使用 apscheduler 在特定时间触发作业?

python - 如何在事件循环之外运行协程?

python - 如何在 scikit-learn 中使用随机 Logistic 回归找到最低的正则化参数 (C)?

python - GridsearchSV 可以包含 train_test_split 的随机化吗

python - Scikit 中数据集的初始可视化 - head() 命令

python - 如何从opencv中的捕获设备(相机)获取最新帧

python - Shell 脚本打开 Flask 支持的网页会打开两个窗口

python - Python 质心中的 KMeans 位置不正确,我该如何将它们设为 "unscale"?

apache-spark - 将 scikit-learn 与 pyspark 集成

python - 安装包 'sklearn' 失败