我正在尝试使用spark MLlib - kmeans来聚类产品数据集。但我的第一列,即ID以Y400sX902开头,当我运行以下代码时,它会抛出一个错误,如NumberFormatException。我是这项技术的新手,如果有任何指示可以帮助那么它会很棒。谢谢。
object KMeansExmp {
def main(args: Array[String]) {
val conf = new SparkConf().setMaster("local[1]").setAppName("KmeansApp");
val sc = new SparkContext(conf);
val rawData = sc.textFile("/Users/Downloads/data.csv")
val header = rawData.first
val rows = rawData.filter(l => l != header)
val extractedFeatureVector = rows.map { row => Vectors.dense(row.split(',').map(_.toDouble).slice(2, 5)) }
val numberOfClusters = 3
val numberOfInterations = 50
val model = KMeans.train(extractedFeatureVector, numberOfClusters, numberOfInterations)
model.clusterCenters.foreach(println)
}
错误:
java.lang.NumberFormatException: For input string: ""Y400sX902""
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1250)
at java.lang.Double.parseDouble(Double.java:540)
at scala.collection.immutable.StringLike$class.toDouble(StringLike.scala:232)
at scala.collection.immutable.StringOps.toDouble(StringOps.scala:31)
at KMeansExmp$$anonfun$2$$anonfun$apply$1.apply(KMeansExmp.scala:22)
at KMeansExmp$$anonfun$2$$anonfun$apply$1.apply(KMeansExmp.scala:22)
at ........
答案 0 :(得分:0)
尝试
val rows = rawData.drop(1)
而不是过滤整个RDD
以删除第一行(即非数字标题)。