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]
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 Default for LinearRegression
[src]impl<'de> Deserialize<'de> for LinearRegression
[src]
impl<'de> Deserialize<'de> for LinearRegression
[src]fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
[src]
__D: Deserializer<'de>,
impl<F: Float, D: Data<Elem = F>, T: AsTargets<Elem = F>> Fit<ArrayBase<D, Dim<[usize; 2]>>, T, LinearError> 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]
&self,
dataset: &DatasetBase<ArrayBase<D, Ix2>, T>
) -> Result<Self::Object>
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]
impl Serialize for LinearRegression
[src]Auto Trait Implementations
impl RefUnwindSafe for LinearRegression
impl RefUnwindSafe for LinearRegression
impl Send for LinearRegression
impl Send for LinearRegression
impl Sync for LinearRegression
impl Sync for LinearRegression
impl Unpin for LinearRegression
impl Unpin for LinearRegression
impl UnwindSafe for LinearRegression
impl UnwindSafe for LinearRegression
Blanket Implementations
impl<T> DeserializeOwned for T where
T: for<'de> Deserialize<'de>,
[src]
impl<T> DeserializeOwned for T where
T: for<'de> Deserialize<'de>,
[src]impl<T> SendSyncUnwindSafe for T where
T: Send + Sync + UnwindSafe + ?Sized,
impl<T> SendSyncUnwindSafe for T where
T: Send + Sync + UnwindSafe + ?Sized,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,