There's more...

  1. We can also change the default intercept behavior using the constructor to create a new regression model and then using setIntercept(true) accordingly:

The sample code is as follows:

  val myModel = new LinearRegressionWithSGD().setIntercept(true)
  1. If the weight for the model is computed as NaN, you must change the model parameters (SGD steps or number of iterations) until you get convergence. An example is model weights that are not computed correctly (there is a convergence problem with SGD in general) due to poor parameter selection. The first move should be to use a more fine-grain step parameter for SGD:
(Model Weights:,[NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN]) 

Get Apache Spark 2.x Machine Learning Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.