You*_*844 12 scala apache-zeppelin apache-spark-ml
我试图用Zeppelin在Spark 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}")
Run Code Online (Sandbox Code Playgroud)
我正在使用的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,
Run Code Online (Sandbox Code Playgroud)
vde*_*dep 12
正如您所提到的,您缺少该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)
Run Code Online (Sandbox Code Playgroud)
请参阅:https://spark.apache.org/docs/latest/ml-features.html#vectorassembler.
| 归档时间: |
|
| 查看次数: |
6384 次 |
| 最近记录: |