pub struct LinearRegression { /* private fields */ }
Expand description

An ordinary least squares linear regression model.

LinearRegression fits a linear model to minimize the residual sum of squares between the observed targets in the dataset, and the targets predicted by the linear approximation.

Ordinary least squares regression solves the overconstrainted model

y = Ax + b

by finding x and b which minimize the L_2 norm ||y - Ax - b||_2.

It currently uses the Moore-Penrose pseudo-inverse to solve y - b = Ax.

/// ## Examples

Here’s an example on how to train a linear regression model on the diabetes dataset

use linfa::traits::{Fit, Predict};
use linfa_linear::LinearRegression;
use linfa::prelude::SingleTargetRegression;

let dataset = linfa_datasets::diabetes();
let model = LinearRegression::default().fit(&dataset).unwrap();
let pred = model.predict(&dataset);
let r2 = pred.r2(&dataset).unwrap();
println!("r2 from prediction: {}", r2);

Implementations

Configure and fit a linear regression model

Create a default linear regression model. By default, an intercept will be fitted.

Configure the linear regression model to fit an intercept.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Deserialize this value from the given Serde deserializer. Read more

Fit a linear regression model given a feature matrix X and a target variable y.

The feature matrix X must have shape (n_samples, n_features)

The target variable y must have shape (n_samples)

Returns a FittedLinearRegression object which contains the fitted parameters and can be used to predict values of the target variable for new feature values.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.