How to access parameters of the underlying model in ML Pipeline?

会有一股神秘感。 提交于 2020-05-30 03:22:06

问题


I have a DataFrame that is processed with LinearRegression. If I do it directly, like below, I can display the details of the model:

val lr = new LinearRegression()
val lrModel = lr.fit(df)

lrModel: org.apache.spark.ml.regression.LinearRegressionModel = linReg_b22a7bb88404

println(s"Coefficients: ${lrModel.coefficients} Intercept: ${lrModel.intercept}")
Coefficients: [0.9705748115939526] Intercept: 0.31041486689532866

However, if I use it inside a pipeline (like in the simplified example below),

val pipeline = new Pipeline().setStages(Array(lr))
val lrModel = pipeline.fit(df)

then I get the following error.

scala> lrModel
res9: org.apache.spark.ml.PipelineModel = pipeline_99ca9cba48f8

scala> println(s"Coefficients: ${lrModel.coefficients} Intercept: ${lrModel.intercept}")
<console>:68: error: value coefficients is not a member of org.apache.spark.ml.PipelineModel
       println(s"Coefficients: ${lrModel.coefficients} Intercept: ${lrModel.intercept}")
                                         ^
<console>:68: error: value intercept is not a member of org.apache.spark.ml.PipelineModel
       println(s"Coefficients: ${lrModel.coefficients} Intercept: ${lrModel.intercept}")

I understand what it means (it's obvious I got a different class, because of the pipeline), but don't know how to get to the real underlying model.


回答1:


LinearRegressionModel should be inside stages at the exact same index as its corresponding LinearRegression.

import org.apache.spark.ml.regressio‌​n.LinearRegressionMo‌​del
lrModel.stages(0).asInstanceOf[LinearRegressionMo‌​del]


来源:https://stackoverflow.com/questions/45225246/how-to-access-parameters-of-the-underlying-model-in-ml-pipeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!