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§
source§impl 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.
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
source§fn eq(&self, other: &LinearRegression) -> bool
fn eq(&self, other: &LinearRegression) -> bool
self
and other
values to be equal, and is used
by ==
.impl Eq for LinearRegression
impl StructuralEq for LinearRegression
impl StructuralPartialEq 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§
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<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.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.