我使用Spark ML方法创建了一个KMeans模型。
val kmeans = new KMeans()
val model = kmeans.fit(df)
我准备好了我的模特。但是如何预测哪个群集中的新数据点会下降。在MLlib中,model.predict(Vector)
预测新数据点的集群。我在模型上看到了transform
方法,但它无效。
答案 0 :(得分:1)
感谢Jacek Laskowski澄清Oli。它现在对我来说很好。这是一个简单的错误。以下是整个代码。
val conf = new SparkConf().setMaster("local").setAppName("ml Kmeans")
val spark = SparkSession.builder().config(conf).getOrCreate()
import spark.implicits._
val trainingData = spark.read.json(spark.sparkContext.wholeTextFiles("file:/home/iot/data/traingJson.json").values)
val parsedData = trainingData.select("value.humidity", "value.speed", "value.temperature", "value.vibration")
val assembler = new VectorAssembler().setInputCols(Array("humidity", "speed", "temperature", "vibration")).setOutputCol("features")
val df = assembler.transform(parsedData)
val kmeans = new KMeans()
val model = kmeans.fit(df)
model.write.save("file:/home/iot/data/model1")
//--------------------------------Testing the Model------------------------
val uploadModel=KMeansModel.load("file:/home/iot/data/model1")
val testData = spark.read.json(spark.sparkContext.wholeTextFiles("file:/home/iot/data/testJson.json").values).select("value.humidity", "value.speed", "value.temperature", "value.vibration")
val assembler=new VectorAssembler().setInputCols(Array("humidity","speed","temperature","vibration")).setOutputCol("features")
val df = assembler.transform(testData)
model.transform(df).show(false)