Struct linfa_linear::LinearRegression[][src]

pub struct LinearRegression { /* fields omitted */ }

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

impl LinearRegression[src]

Configure and fit a linear regression model

pub fn new() -> LinearRegression[src]

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.

pub fn with_intercept(self, with_intercept: bool) -> Self[src]

Configure the linear regression model to fit an intercept. Defaults to true if not set.

pub fn with_intercept_and_normalize(self) -> Self[src]

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

impl Default for LinearRegression[src]

impl<'de> Deserialize<'de> for LinearRegression[src]

impl<F: Float, D: Data<Elem = F>, T: AsTargets<Elem = F>> Fit<ArrayBase<D, Dim<[usize; 2]>>, T, LinearError> for LinearRegression[src]

type Object = FittedLinearRegression<F>

fn fit(
    &self,
    dataset: &DatasetBase<ArrayBase<D, Ix2>, T>
) -> Result<Self::Object>
[src]

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.

impl Serialize for LinearRegression[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> DeserializeOwned for T where
    T: for<'de> Deserialize<'de>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> SendSyncUnwindSafe for T where
    T: Send + Sync + UnwindSafe + ?Sized

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,