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.
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
sourceimpl Clone for LinearRegression
impl Clone for LinearRegression
sourcefn clone(&self) -> LinearRegression
fn clone(&self) -> LinearRegression
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for LinearRegression
impl Debug for LinearRegression
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: 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
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 PartialEq<LinearRegression> for LinearRegression
impl PartialEq<LinearRegression> for LinearRegression
sourcefn eq(&self, other: &LinearRegression) -> bool
fn eq(&self, other: &LinearRegression) -> bool
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
sourcefn ne(&self, other: &LinearRegression) -> bool
fn ne(&self, other: &LinearRegression) -> bool
This method tests for !=
.
sourceimpl Serialize for LinearRegression
impl Serialize for LinearRegression
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more