Skip to content

aurora: Linear Regression

Clouke edited this page May 4, 2023 · 1 revision

Build your trainable Linear Regression

LinearRegression lr = new LinearRegressionBuilder()
  .learningRate(0.01) // define the learning rate
  .epochs(100_000) // define the epochs
  .printing(Bar.CLASSIC) // define the progress bar (optional) - Bar.CLASSIC by default
  .build();

Train your Linear Regression

Create a Data Set:

double[] x = new double[] {1, 2, 3, 4, 5};
double[] y = new double[] {2, 4, 6, 8, 10};

Fit the model with the data set:

lr.fit(x, y);

Printing: Printing comes with attributes, providing information about the training process:

  • Loss: Represents the decreasing error which means the model is improving
  • Stage: Represents the current stage in the training process, which goes to 100 when it is completed
  • Accuracy: Represents the accuracy score of the model, whereas you may use HyperparameterTuning for the best score
  • Epoch: Represents the current iteration
[###############################] Loss: 2.8398992587956425E-29 | Stage: 100 | Accuracy: 0.9 | Epoch: 99500 (—)

Predict

double[] output = lr.predict(new double[]{6, 7, 8, 9, 10});

Deploy your model

Save your Linear Regression Model:

Model model = lr.toModel();
model.save("my_directory");

Load a pre-trained model

Load from file:

LinearRegressionModel model = null;
try (ModelLoader loader = new ModelLoader(new File("my_directory"))) {
  model = loader.load(LinearRegressionModel.class);
}

Load from URL:

LinearRegressionModel model = null;
try (ModelLoader loader = new ModelLoader(new URL("my_model_url"))) {
  model = loader.load(LinearRegressionModel.class);
} catch (MalformedURLException e) {
  throw new RuntimeException(e);
}