我正在尝试用齐柏林飞艇在火花ML中建立一个模型。我是这方面的新手,想要一些帮助。我认为我需要为该列设置正确的数据类型,并将第一列设置为标签。任何帮助都将不胜感激,谢谢!
val training = sc.textFile("hdfs:///ford/fordTrain.csv")
val header = training.first
val inferSchema = true
val df = training.toDF
val lr = new LogisticRegression()
.setMaxIter(10)
.setRegParam(0.3)
.setElasticNetParam(0.8)
val lrModel = lr.fit(df)
// Print the coefficients and intercept for multinomial logistic regression
println(s"Coefficients: \n${lrModel.coefficientMatrix}")
println(s"Intercepts: ${lrModel.interceptVector}")我正在使用的csv文件的一个片段是:
IsAlert,P1,P2,P3,P4,P5,P6,P7,P8,E1,E2
0,34.7406,9.84593,1400,42.8571,0.290601,572,104.895,0,0,0,发布于 2017-07-07 14:06:22
正如您所提到的,您缺少features列。它是一个包含所有预测变量的向量。您必须使用VectorAssembler创建它。
IsAlert是标签和所有其他变量(p1,p2,...)都是预测变量,您可以通过以下方式创建features列(实际上,您可以将其命名为任何名称,而不是features):
import org.apache.spark.ml.feature.VectorAssembler
import org.apache.spark.ml.linalg.Vectors
//creating features column
val assembler = new VectorAssembler()
.setInputCols(Array("P1","P2","P3","P4","P5","P6","P7","P8","E1","E2"))
.setOutputCol("features")
val lr = new LogisticRegression()
.setMaxIter(10)
.setRegParam(0.3)
.setElasticNetParam(0.8)
.setFeaturesCol("features") // setting features column
.setLabelCol("IsAlert") // setting label column
//creating pipeline
val pipeline = new Pipeline().setStages(Array(assembler,lr))
//fitting the model
val lrModel = pipeline.fit(df)参考:https://spark.apache.org/docs/latest/ml-features.html#vectorassembler。
https://stackoverflow.com/questions/44950897
复制相似问题