Struct linfa_linear::LinearRegression
source · [−]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
sourceimpl LinearRegression
impl LinearRegression
Configure and fit a linear regression model
sourcepub fn new() -> LinearRegression
pub fn new() -> LinearRegression
Create a default linear regression model.
By default, an intercept will be fitted. To disable fitting an
intercept, call .with_intercept(false)
before calling .fit()
.
To additionally normalize the feature matrix before fitting, call
fit_intercept_and_normalize()
before calling fit()
. The feature
matrix will not be normalized by default.
sourcepub fn with_intercept(self, with_intercept: bool) -> Self
pub fn with_intercept(self, with_intercept: bool) -> Self
Configure the linear regression model to fit an intercept.
Defaults to true
if not set.
sourcepub fn with_intercept_and_normalize(self) -> Self
pub fn with_intercept_and_normalize(self) -> Self
Configure the linear regression model to fit an intercept and to normalize the feature matrix before fitting it.
Normalizing the feature matrix is generally recommended to improve numeric stability unless features have already been normalized or are all within in a small range and all features are of similar size.
Normalization implies fitting an intercept.
Trait Implementations
sourceimpl Default for LinearRegression
impl Default for LinearRegression
sourceimpl<'de> Deserialize<'de> for LinearRegression
impl<'de> Deserialize<'de> for LinearRegression
sourcefn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl<F: Float, D: Data<Elem = F>, T: AsTargets<Elem = F>> Fit<ArrayBase<D, Dim<[usize; 2]>>, T, LinearError<F>> for LinearRegression
impl<F: Float, D: Data<Elem = F>, T: AsTargets<Elem = F>> Fit<ArrayBase<D, Dim<[usize; 2]>>, T, LinearError<F>> for LinearRegression
sourcefn fit(
&self,
dataset: &DatasetBase<ArrayBase<D, Ix2>, T>
) -> Result<Self::Object, F>
fn fit(
&self,
dataset: &DatasetBase<ArrayBase<D, Ix2>, T>
) -> Result<Self::Object, F>
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.
type Object = FittedLinearRegression<F>
sourceimpl Serialize for LinearRegression
impl Serialize for LinearRegression
Auto Trait Implementations
impl RefUnwindSafe for LinearRegression
impl Send for LinearRegression
impl Sync for LinearRegression
impl Unpin for LinearRegression
impl UnwindSafe for LinearRegression
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more