pub struct LinearRegression<T: RealNumber> { /* private fields */ }Expand description
Represents a linear regression model.
The LinearRegression struct implements a linear regression model for predicting a target variable based on one or more input features.
It uses the least squares method to estimate the weights of the linear model.
§Type Parameters
T: The numeric type used for calculations. Must implement theRealNumbertrait.
§Fields
weights: The weights of the logistic regression model, with the first being the bias weight.
§Examples
use rusty_ai::regression::linear::LinearRegression;
use rusty_ai::data::dataset::Dataset;
use nalgebra::{DMatrix, DVector};
// Create a new linear regression model
let mut model = LinearRegression::<f64>::new();
// Fit the model to a dataset
let x = DMatrix::from_row_slice(3, 2, &[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
let y = DVector::from_vec(vec![1.5, 2.5, 3.5]);
let dataset = Dataset::new(x, y);
let learning_rate = 0.01;
let max_steps = 1000;
let epsilon = Some(0.001);
let progress = Some(100);
let result = model.fit(&dataset, learning_rate, max_steps, epsilon, progress);
// Make predictions using the trained model
let x_test = DMatrix::from_row_slice(2, 2, &[1.0, 2.0, 3.0, 4.0]);
let predictions = model.predict(&x_test);
assert!(predictions.is_ok());Implementations§
Source§impl<T: RealNumber> LinearRegression<T>
impl<T: RealNumber> LinearRegression<T>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new LinearRegression model with default weights.
The default weights are initialized to 1.0 for each feature, including the bias weight.
Sourcepub fn with_params(
dimension: Option<usize>,
weights: Option<DVector<T>>,
) -> Result<Self, Box<dyn Error>>
pub fn with_params( dimension: Option<usize>, weights: Option<DVector<T>>, ) -> Result<Self, Box<dyn Error>>
Creates a new LinearRegression model with custom parameters.
§Arguments
dimension: The dimension of the input features. IfNone, the dimension will be inferred from the provided weights.weights: The initial weights for the linear regression model. IfNone, default weights will be used.
§Returns
A Result containing the LinearRegression model if the parameters are valid, or an error message if the parameters are invalid.
§Errors
An error will be returned if:
- Both
dimensionandweightsareNone. - The length of
weightsis not equal todimension + 1to account for the bias weight.
Sourcepub fn weights(&self) -> &DVector<T>
pub fn weights(&self) -> &DVector<T>
A reference to the weights of the linear regression model.
Sourcepub fn fit(
&mut self,
dataset: &Dataset<T, T>,
lr: T,
max_steps: usize,
epsilon: Option<T>,
progress: Option<usize>,
) -> Result<String, Box<dyn Error>>
pub fn fit( &mut self, dataset: &Dataset<T, T>, lr: T, max_steps: usize, epsilon: Option<T>, progress: Option<usize>, ) -> Result<String, Box<dyn Error>>
Fits the linear regression model to a dataset.
§Arguments
dataset: The dataset containing the input features and target values.lr: The learning rate for gradient descent.max_steps: The maximum number of steps to perform during training.epsilon: The convergence threshold. If the change in weights is below this threshold, training will stop.progress: The number of steps at which to display progress information. IfNone, no progress information will be displayed.
§Returns
A Result containing a success message if training is successful, or an error message if an error occurs during training.
§Errors
An error will be returned if:
- The number of steps for progress visualization is 0.
- The gradient turns to NaN during training.
Trait Implementations§
Source§impl<T: Clone + RealNumber> Clone for LinearRegression<T>
impl<T: Clone + RealNumber> Clone for LinearRegression<T>
Source§fn clone(&self) -> LinearRegression<T>
fn clone(&self) -> LinearRegression<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: Debug + RealNumber> Debug for LinearRegression<T>
impl<T: Debug + RealNumber> Debug for LinearRegression<T>
Source§impl<T: RealNumber> Default for LinearRegression<T>
impl<T: RealNumber> Default for LinearRegression<T>
Source§impl<T: RealNumber> RegressionMetrics<T> for LinearRegression<T>
impl<T: RealNumber> RegressionMetrics<T> for LinearRegression<T>
Source§fn mse(
&self,
y_true: &DVector<T>,
y_pred: &DVector<T>,
) -> Result<T, Box<dyn Error>>
fn mse( &self, y_true: &DVector<T>, y_pred: &DVector<T>, ) -> Result<T, Box<dyn Error>>
Auto Trait Implementations§
impl<T> Freeze for LinearRegression<T>
impl<T> RefUnwindSafe for LinearRegression<T>where
T: RefUnwindSafe,
impl<T> Send for LinearRegression<T>
impl<T> Sync for LinearRegression<T>
impl<T> Unpin for LinearRegression<T>where
T: Unpin,
impl<T> UnwindSafe for LinearRegression<T>where
T: UnwindSafe,
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<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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.