k-means使用Spark / Scala对地理定位数据进行聚类

时间:2017-06-18 14:37:13

标签: scala k-means

如何使用k-means聚类算法处理地理定位数据,有人可以在此分享您的意见,提前致谢。

 Project_2_Dataset.txt file entries look like this 
 =================================================

            33.68947543 -117.5433083
            37.43210889 -121.4850296
            39.43789083 -120.9389785
            39.36351868 -119.4003347
            33.19135811 -116.4482426
            33.83435437 -117.3300009

    Please review my Code here:
    ============================         
    import org.apache.spark.mllib.linalg.Vectors
    import org.apache.spark.mllib.clustering.KMeans
    val data = sc.textFile("Project_2_Dataset.txt")             
    val parsedData = data.map( line => Vectors.dense(line.split(',').map(_.toDouble)))
    val kmmodel= KMeans.train(parsedData,3,5) --- 3 clusters,4 Iterations.
    17/06/17 13:12:20 ERROR Executor: Exception in task 0.0 in stage 1.0 (TID 2)
    java.lang.NumberFormatException: For input string: "33.68947543 -117.5433083"
            at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
            at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
            at java.lang.Double.parseDouble(Double.java:538)
            at scala.collection.immutable.StringLike$class.toDouble(StringLike.scala:232)

由于 Amit K

2 个答案:

答案 0 :(得分:0)

我认为这是因为您尝试在char ','而不是' '拆分每一行。

@ "33.19135811 -116.4482426".toDouble 
java.lang.NumberFormatException: For input string: "33.19135811 -116.4482426"
  ...

@ "33.19135811 -116.4482426".split(',').map(_.toDouble) 
java.lang.NumberFormatException: For input string: "33.19135811 -116.4482426"
  ...

@ "33.19135811 -116.4482426".split(' ').map(_.toDouble) 
res3: Array[Double] = Array(33.19135811, -116.4482426)

答案 1 :(得分:0)

In the previous case where were able to apply the split on a set of data("33.19135811 -116.4482426".split(' ').map(_.toDouble)) , But it seems that when we are applying the same split on multiple set of data, Am getting this error: 

                33.68947543 -117.5433083
                37.43210889 -121.4850296
                39.43789083 -120.9389785
                39.36351868 -119.4003347

    scala> val kmmodel= KMeans.train(parsedData,3,5)
    17/06/29 19:14:36 ERROR Executor: Exception in task 1.0 in stage 6.0 (TID 8)
    java.lang.NumberFormatException: empty String