简体   繁体   中英

How to obtain coefficient values from Spark-MLlib Linear Regression model (Scala)?

I'd like to obtain coefficient values of Linear Regression(LR) model in Spark-MLlib. Here I use the 'LinearRegressionWithSGD' to build the model and you can find the sample from the following link:

https://spark.apache.org/docs/2.1.0/mllib-linear-methods.html#regression

I could get the coefficient values from Spark-ML Linear Regression. Please find the reference link from below.

https://spark.apache.org/docs/2.1.0/ml-classification-regression.html#linear-regression

Please help me with this. Thanks in advance !!

Took first lines of model creation from the first link you sent:

val model: LinearRegressionModel = LinearRegressionWithSGD.train(parsedData, numIterations, stepSize)
.run(training)
// Here are the coefficient and intercept
val weights: org.apache.spark.mllib.linalg.Vector = model.weights
val intercept = model.intercept
val weightsData: Array[Double] = weights.asInstanceOf[DenseVector].values

The last 3 lines are the coefficient and intercept

The type of weights is : org.apache.spark.mllib.linalg.Vector

That is a wrapper around the Breeze DenseVector

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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