讀的https://spark.apache.org/docs/1.5.2/ml-ann.html在src:如何在apache模型訓練後對新的訓練樣例進行分類?
import org.apache.spark.ml.classification.MultilayerPerceptronClassifier
import org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator
import org.apache.spark.mllib.util.MLUtils
import org.apache.spark.sql.Row
// Load training data
val data = MLUtils.loadLibSVMFile(sc, "data/mllib/sample_multiclass_classification_data.txt").toDF()
// Split the data into train and test
val splits = data.randomSplit(Array(0.6, 0.4), seed = 1234L)
val train = splits(0)
val test = splits(1)
// specify layers for the neural network:
// input layer of size 4 (features), two intermediate of size 5 and 4 and output of size 3 (classes)
val layers = Array[Int](4, 5, 4, 3)
// create the trainer and set its parameters
val trainer = new MultilayerPerceptronClassifier()
.setLayers(layers)
.setBlockSize(128)
.setSeed(1234L)
.setMaxIter(100)
// train the model
val model = trainer.fit(train)
// compute precision on the test set
val result = model.transform(test)
val predictionAndLabels = result.select("prediction", "label")
val evaluator = new MulticlassClassificationEvaluator()
.setMetricName("precision")
println("Precision:" + evaluator.evaluate(predictionAndLabels))
一旦模型已經被訓練如何可以在新的訓練樣本進行分類?
是否可以在model
中添加一個新的訓練示例,其中標籤未設置,分類器將嘗試根據訓練數據對此訓練示例進行分類?
爲什麼要求數據幀標籤是Double類型的?
謝謝,請隨時不同意我對你答案的任何評論。每次添加新數據框時,我們都不應該重新訓練,因爲新數據框未被分類(不包含標籤),重新訓練的重點是什麼。 「梯度下降算法廣泛用於擬合大多數模型,它使用數字而不是字母或類來計算每次迭代中的誤差」這是不正確的,因爲它使用的數字是基於特徵而不是標籤。 –
@ blue-sky我說過,增加另一個觀察(但是我將它指定爲一個完整的觀察),你的問題在這個主題中有點混亂,這就是爲什麼我談論創建另一個DF來重新訓練模型。在最後一點,它使用了兩個模型,該模型使用特徵來計算2類(0.0 - 1.0)中的標籤,通常使用sigmonoid函數進行計算。 –
這一行:'val result = model.transform(test)''test'數據中的標籤是沒有意義的?作爲使用val提取的「預測」列predictionAndLabels = result.select(「prediction」,「label」)包含標籤,「prediction」是預測的標籤? –