如何从火花管道物流模型中提取可变权重?

时间:2016-03-10 09:03:35

标签: scala apache-spark pipeline

我目前正在尝试学习Spark Pipeline(Spark 1.6.0)。我将数据集(训练和测试)导入为oas.sql.DataFrame对象。执行以下代码后,生成的模型为oas.ml.tuning.CrossValidatorModel

您可以使用model.transform(测试)根据Spark中的测试数据进行预测。但是,我想将模型用于预测的权重与来自R的权重进行比较。如何提取预测变量的权重和拦截(如果有的话)模型? Scala代码是:

import sqlContext.implicits._
import org.apache.spark.mllib.linalg.{Vectors, Vector}
import org.apache.spark.SparkContext
import org.apache.spark.mllib.regression.LabeledPoint
import org.apache.spark.ml.Pipeline
import org.apache.spark.ml.classification.{LogisticRegression, LogisticRegressionModel}
import org.apache.spark.ml.evaluation.BinaryClassificationEvaluator
import org.apache.spark.ml.tuning.{ParamGridBuilder, CrossValidator}

val conTrain = sc.textFile("AbsolutePath2Train.txt")
val conTest = sc.textFile("AbsolutePath2Test.txt")

// parse text and convert to sql.DataFrame
val train = conTrain.map { line =>
val parts = line.split(",")
LabeledPoint(parts(0).toDouble, Vectors.dense(parts(1).split(" +").map(_.toDouble)))
}.toDF()
val test =conTest.map{ line =>
val parts = line.split(",")
LabeledPoint(parts(0).toDouble, Vectors.dense(parts(1).split(" +").map(_.toDouble)))
}.toDF()

// set parameter space and evaluation method
val lr = new LogisticRegression().setMaxIter(400)
val pipeline = new Pipeline().setStages(Array(lr))
val paramGrid = new ParamGridBuilder().addGrid(lr.regParam, Array(0.1, 0.01)).addGrid(lr.fitIntercept).addGrid(lr.elasticNetParam, Array(0.0, 0.5, 1.0)).build()
val cv = new CrossValidator().setEstimator(pipeline).setEvaluator(new BinaryClassificationEvaluator).setEstimatorParamMaps(paramGrid).setNumFolds(2)

// fit logistic model
val model = cv.fit(train)

// If you want to predict with test
val pred = model.transform(test)

我的火花环境无法访问。因此,这些代码被重新输入并重新检查。我希望他们是对的。到目前为止,我已尝试在网上搜索,询问其他人。关于我的编码,欢迎建议和批评。

3 个答案:

答案 0 :(得分:2)

// set parameter space and evaluation method
val lr = new LogisticRegression().setMaxIter(400)
val pipeline = new Pipeline().setStages(Array(lr))
val paramGrid = new ParamGridBuilder().addGrid(lr.regParam, Array(0.1, 0.01)).addGrid(lr.fitIntercept).addGrid(lr.elasticNetParam, Array(0.0, 0.5, 1.0)).build()
val cv = new CrossValidator().setEstimator(pipeline).setEvaluator(new BinaryClassificationEvaluator).setEstimatorParamMaps(paramGrid).setNumFolds(2)
// you can print lr model coefficients as below
val model = cv.bestModel.asInstanceOf[PipelineModel]
val lrModel = model.stages(0).asInstanceOf[LogisticRegressionModel]
println(s"LR Model coefficients:\n${lrModel.coefficients.toArray.mkString("\n")}")

两个步骤:

  1. 从交叉验证结果中获取最佳管道。
  2. 从最佳管道中获取LR模型。这是代码示例中的第一个阶段。

答案 1 :(得分:0)

我一直在寻找完全相同的东西。你可能已经有了答案,但无论如何,这里是。

import org.apache.spark.ml.classification.LogisticRegressionModel
val lrmodel = model.bestModel.asInstanceOf[LogisticRegressionModel]
print(model.weight, model.intercept)

答案 2 :(得分:0)

我仍然不确定如何从上面的“模型”中提取权重。但是通过将流程重组为the official tutorial,以下内容适用于spark-1.6.0:

import org.apache.spark.ml.evaluation.BinaryClassificationEvaluator
import org.apache.spark.ml.tuning.{ParamGridBuilder, TrainValidationSplit}
val lr = new LogisticRegression().setMaxIter(400)
val paramGrid = new ParamGridBuilder().addGrid(lr.regParam, Array(0.1, 0.01)).addGrid(lr.fitIntercept).addGrid(lr.elasticNetParam, Array(0.0, 0.5, 1.0)).build()
val trainValidationSplit = new TrainValidationSplit().setEstimator(lr).setEvaluator(new BinaryClassificationEvaluator).setEstimatorParamMaps(paramGrid).setTrainRatio(0.8)
val restructuredModel = trainValidationSplit.fit(train)
val lrmodel = restructuredModel.bestModel.asInstanceOf[LogisticRegressionModel]
lrmodel.weigths
lrmodel.intercept

我注意到这里“lrmodel”和上面生成的“model”之间存在差异:

model.bestModel - >给oas.ml.Model [_] =管道_ ****

restructuredModel.bestModel - >给出oas.ml.Model [_] = logreg _ ****

这就是为什么我们可以将resturcturedModel.bestModel转换为LogisticRegressionModel而不是model.bestModel。当我理解差异的原因时,我会添加更多。