简体   繁体   English

在多类分类上使用Spark ML的Logistic回归模型会给出错误:列预测已存在

[英]Using Spark ML's Logistic Regression model on MultiClass Classification giving error : Column prediction already exists

I am using Spark ML's Logistic Regression model for classification problem having 100 categories (0-99). 我正在使用Spark ML的Logistic回归模型来解决具有100个类别(0-99)的分类问题。 My columns in dataset are - "_c0,_c1,_c2,_c3,_c4,_c5" where _c5 is a target variable and rest are the features. 我在数据集中的列是-“ _c0,_c1,_c2,_c3,_c4,_c5”,其中_c5是目标变量,其余是要素。 My code is following : 我的代码如下:

import org.apache.spark.ml.feature.{StringIndexer, VectorAssembler}
import org.apache.spark.ml.classification.LogisticRegression
import org.apache.spark.ml.classification.OneVsRest
val _c0Indexer = new StringIndexer().setInputCol("_c0").setOutputCol("_c0Index")
val _c1Indexer = new StringIndexer().setInputCol("_c1").setOutputCol("_c1Index")
val _c2Indexer = new StringIndexer().setInputCol("_c2").setOutputCol("_c2Index")
val _c3Indexer = new StringIndexer().setInputCol("_c3").setOutputCol("_c3Index")
val _c4Indexer = new StringIndexer().setInputCol("_c4").setOutputCol("_c4Index")
val _c5Indexer = new StringIndexer().setInputCol("_c5").setOutputCol("_c5Index")
val assembler = new VectorAssembler().setInputCols(Array("_c0Index", "_c1Index", "_c2Index", "_c3Index","_c4Index")).setOutputCol("features")
val lr = new LogisticRegression()
  .setMaxIter(10)
  .setRegParam(0.3)
  .setElasticNetParam(0.8).setLabelCol("_c5Index").setFeaturesCol("features")
val ovr = new OneVsRest().setClassifier(lr)
val pipeline = new Pipeline().setStages(Array(_c0Indexer, _c1Indexer, _c2Indexer, _c3Indexer, _c4Indexer,assembler, _c5Indexer, ovr,lr))
val model = pipeline.fit(data)
val predictions = model.transform(testdf)
println(predictions.select("features", "_c5Index", "probability","prediction").show(5))

But it is showing an error : 但是它显示了一个错误:

requirement failed: Column prediction already exists.

Can someone please guide why I am getting this error? 有人可以指导我为什么会出现此错误吗? Thanks in advance. 提前致谢。

Try taking out the "lr" at the end of your pipeline. 尝试在管道末端取出“ lr”。 I think it's unnecessary since ovr uses lr. 我认为这是不必要的,因为ovr使用lr。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM