【问题标题】:infinite centroid for kmeans spark scalakmeans spark scala的无限质心
【发布时间】:2018-09-25 09:54:23
【问题描述】:

(我想我几乎可以确定答案是什么)

这是我的代码:

val fileName = """file:///home/user/data/csv/sessions_sample.csv"""
val df = spark.read.format("csv").option("header", "true").option("inferSchema", "true").load(fileName)

// calculate input for kmeans
val input1 = df.select("id", "duration", "ip_dist", "txr1", "txr2", "txr3", "txr4").na.fill(3.0)
val input2 = input1.map(r => (r.getInt(0), Vectors.dense((1 until r.size - 1).map{ i =>  r.getDouble(i)}.toArray[Double])))
val input3 = input2.toDF("id", "features")

// initiate kmeans
val kmeans = new KMeans().setK(100).setSeed(1L).setFeaturesCol("features").setPredictionCol("prediction")
val model = kmeans.fit(input3)
val model = kmeans.fit(input3.select("features"))

// Make predictions
val predictions = model.transform(input3.select("features"))
val predictions = model.transform(input3)
val evaluator = new ClusteringEvaluator()

// i get an error when i run this line
val silhouette = evaluator.evaluate(predictions)

java.lang.AssertionError: 断言失败: 簇数必须是 大于一。在 scala.Predef$.assert(Predef.scala:170) 在 org.apache.spark.ml.evaluation.SquaredEuclideanSilhouette$.computeSilhouetteScore(ClusteringEvaluator.scala:416) 在 org.apache.spark.ml.evaluation.ClusteringEvaluator.evaluate(ClusteringEvaluator.scala:96) ... 49 省略

但我的质心是这样的:

model.clusterCenters.foreach(println)

[3217567.1300936914,145.06533614203505,无穷大,无穷大,无穷大]

我认为因为一些中心是无限的 => kmeans 是不稳定的 => 轮廓测量出错了。 但它仍然没有回答为什么,如果我尝试更改 k,到目前为止任何 k > 1,我都会收到错误消息“集群数必须大于一”。

请指教。

【问题讨论】:

    标签: scala apache-spark infinite centroid


    【解决方案1】:

    我曾经看到过同样的消息。根本原因是每个数据都是相同的(我的数据是由程序生成的)所以当然只有一个集群。顺便说一句,我没有检查它的中心,所以我不确定我的情况是否和你的一样。

    【讨论】:

      猜你喜欢
      • 2014-10-29
      • 2013-04-23
      • 2020-06-13
      • 2021-06-13
      • 2016-11-16
      • 2012-02-05
      • 2020-05-03
      • 2021-03-03
      • 2015-09-17
      相关资源
      最近更新 更多