pub struct LinearRegression { /* private fields */ }
Expand description
An ordinary least squares univariate linear regression model.
Given predictors x
and responses y
ordinary least squares linear
regression estimates a model of the form
y = xW + b
by finding a matrix W
and a vector b
which minimize the sum of the
squared L_2 norms ||y_j - x_jW - b||_2^2
for a dataset
{(x_j, y_j) for j in 1..=n_samples}
.
The algorithm is only implemented for univariate regression. This means
that b
and y
are scalars and W
is just one column.
§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§
Source§impl LinearRegression
Configure and fit a linear regression model
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.
Sourcepub fn with_intercept(self, intercept: bool) -> Self
pub fn with_intercept(self, intercept: bool) -> Self
Configure the linear regression model to fit an intercept.
Trait Implementations§
Source§impl Clone for LinearRegression
impl Clone for LinearRegression
Source§fn clone(&self) -> LinearRegression
fn clone(&self) -> LinearRegression
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for LinearRegression
impl Debug for LinearRegression
Source§impl Default for LinearRegression
impl Default for LinearRegression
Source§impl<F: Float, D: Data<Elem = F>, T: AsSingleTargets<Elem = F>> Fit<ArrayBase<D, Dim<[usize; 2]>>, T, LinearError<F>> for LinearRegression
impl<F: Float, D: Data<Elem = F>, T: AsSingleTargets<Elem = F>> Fit<ArrayBase<D, Dim<[usize; 2]>>, T, LinearError<F>> for LinearRegression
Source§fn 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>
Source§impl PartialEq for LinearRegression
impl PartialEq for LinearRegression
impl Eq for LinearRegression
impl StructuralPartialEq for LinearRegression
Auto Trait Implementations§
impl Freeze for LinearRegression
impl RefUnwindSafe for LinearRegression
impl Send for LinearRegression
impl Sync for LinearRegression
impl Unpin for LinearRegression
impl UnwindSafe for LinearRegression
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more