【问题标题】:AttributeError: 'KMeans' object has no attribute 'setK'AttributeError:“KMeans”对象没有属性“setK”
【发布时间】:2021-05-08 10:37:09
【问题描述】:

来自https://runawayhorse001.github.io/LearningApacheSpark/clustering.html 的示例 在我决定测试 Spark 的集群示例时导致了奇怪的错误。 示例:

from sklearn.cluster import KMeans

import numpy as np

cost = np.zeros(20)

for k in range(2,20):
    kmeans = KMeans()\
            .setK(k)\
            .setSeed(1) \
            .setFeaturesCol("indexedFeatures")\
            .setPredictionCol("cluster")
model = kmeans.fit(data)
cost[k] = model.computeCost(data)

尽管 fit 已经实现,但它导致 Kmeans 属性出现错误。

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-22-296a7d54514a> in <module>
      2 cost = np.zeros(20)
      3 for k in range(2,20):
----> 4     kmeans = KMeans()\
      5             .setK(k)\
      6             .setSeed(1) \

AttributeError: 'KMeans' object has no attribute 'setK'

我过去遇到过类似的问题,.fit() 解决了这些问题,但现在无法正常工作。

【问题讨论】:

    标签: python apache-spark pyspark apache-spark-ml


    【解决方案1】:

    您正在导入错误的 KMeans。我相信 KMeans 指的是 Spark ML 中的,而不是 scikit-learn 中的。

    from pyspark.ml.clustering import KMeans
    

    【讨论】:

    • 一切都一样。
    • @Alexey_ (1) 你是否删除了导入 scikit-learn 的行? (2) 可以显示print(KMeans)的输出吗?
    • computeCost 已弃用,如 this post 中所述。我认为您正在关注的教程已过时@Alexey_
    猜你喜欢
    • 2021-06-15
    • 2022-08-23
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 2021-04-19
    • 2021-11-22
    相关资源
    最近更新 更多