【发布时间】: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