加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 字段“功能”不存在. SparkML

发布时间:2020-12-16 19:16:13 所属栏目:安全 来源:网络整理
导读:我试图用Zeppelin在Spark ML中建立一个模型. 我是这个领域的新手,想要一些帮助.我想我需要将正确的数据类型设置为列并将第一列设置为标签.非常感谢任何帮助,谢谢 val training = sc.textFile("hdfs:///ford/fordTrain.csv")val header = training.firstval i
我试图用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}")

我正在使用的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,

解决方法

如您所述,您缺少功能列.它是包含所有预测变量的向量.您必须使用VectorAssembler创建它.

IsAlert是标签,所有其他变量(p1,p2,…)都是预测变量,您可以通过以下方式创建要素列(实际上您可以将其命名为任何名称而不是要素):

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读