pub struct LinearRegression {
pub w: Vector,
pub b: Scalar,
/* private fields */
}
Expand description
A Linear Regression model that fits the equation y = W.T @ x + b
This model predicts a continuous value output based on input features using a linear relationship where W is the weight vector and b is the bias. Linear regression is one of the most fundamental machine learning algorithms used for predicting numerical values by establishing a linear relationship between the independent variables (features) and the dependent variable (target).
The model minimizes the Mean Squared Error (MSE) between predictions and actual values.
Fields§
§w: Vector
The weights vector (W) for the linear model representing the coefficients for each feature in the input data
b: Scalar
The bias term (b) for the linear model representing the y-intercept of the linear equation
Implementations§
Source§impl LinearRegression
impl LinearRegression
Sourcepub fn new(n_x: usize) -> Result<Self, ModelError>
pub fn new(n_x: usize) -> Result<Self, ModelError>
Creates a new LinearRegression model with zero-initialized weights and bias
Initializes a linear regression model with all weights set to zero and bias set to zero. This creates an untrained model that can be later optimized using various training algorithms.
§Arguments
n_x
- Number of input features in the dataset
§Returns
Result<Self, ModelError>
- A new LinearRegression instance or an error if the initialization fails
Sourcepub fn builder() -> LinearRegressionBuilder
pub fn builder() -> LinearRegressionBuilder
Returns a builder for creating a LinearRegression with custom configuration
The builder pattern allows for more flexible initialization of the model with various optional parameters and configurations.
§Returns
LinearRegressionBuilder
- A builder for LinearRegression with fluent API
Trait Implementations§
Source§impl BaseModel<ArrayBase<OwnedRepr<f64>, Dim<[usize; 2]>>, ArrayBase<OwnedRepr<f64>, Dim<[usize; 1]>>> for LinearRegression
impl BaseModel<ArrayBase<OwnedRepr<f64>, Dim<[usize; 2]>>, ArrayBase<OwnedRepr<f64>, Dim<[usize; 1]>>> for LinearRegression
Source§fn compute_cost(&self, x: &Matrix, y: &Vector) -> Result<f64, ModelError>
fn compute_cost(&self, x: &Matrix, y: &Vector) -> Result<f64, ModelError>
Computes the Mean Squared Error cost between predictions and target values
Calculates the cost using the formula: J = (1/2m) * Σ(y_hat - y)² where m is the number of examples
§Arguments
x
- Input features of shape (n_x, m)y
- Target values of shape (m, )
§Returns
Result<f64, ModelError>
- The computed cost value
Source§impl Builder<LinearRegression, ArrayBase<OwnedRepr<f64>, Dim<[usize; 2]>>, ArrayBase<OwnedRepr<f64>, Dim<[usize; 1]>>> for LinearRegressionBuilder
impl Builder<LinearRegression, ArrayBase<OwnedRepr<f64>, Dim<[usize; 2]>>, ArrayBase<OwnedRepr<f64>, Dim<[usize; 1]>>> for LinearRegressionBuilder
Source§fn build(&self) -> Result<LinearRegression, ModelError>
fn build(&self) -> Result<LinearRegression, ModelError>
Builds and returns a new LinearRegression model with the configured parameters.
§Returns
Result<LinearRegression, ModelError>
- A new LinearRegression instance with the specified number of input features, or an error if construction fails
Source§impl Debug for LinearRegression
impl Debug for LinearRegression
Source§impl GradientCollection for LinearRegression
impl GradientCollection for LinearRegression
Source§fn get_gradient<D: Dimension>(
&self,
key: &str,
) -> Result<ArrayView<'_, f64, D>, ModelError>
fn get_gradient<D: Dimension>( &self, key: &str, ) -> Result<ArrayView<'_, f64, D>, ModelError>
Source§fn set_gradient<D: Dimension>(
&mut self,
key: &str,
value: ArrayView<'_, f64, D>,
) -> Result<(), ModelError>
fn set_gradient<D: Dimension>( &mut self, key: &str, value: ArrayView<'_, f64, D>, ) -> Result<(), ModelError>
Source§impl OptimizableModel<ArrayBase<OwnedRepr<f64>, Dim<[usize; 2]>>, ArrayBase<OwnedRepr<f64>, Dim<[usize; 1]>>> for LinearRegression
Implementation of the OptimizableModel
trait for LinearRegression
impl OptimizableModel<ArrayBase<OwnedRepr<f64>, Dim<[usize; 2]>>, ArrayBase<OwnedRepr<f64>, Dim<[usize; 1]>>> for LinearRegression
Implementation of the OptimizableModel
trait for LinearRegression
Source§fn forward(&self, input: &Matrix) -> Result<Vector, ModelError>
fn forward(&self, input: &Matrix) -> Result<Vector, ModelError>
Performs forward propagation to compute predictions
Calculates the linear function y_hat = W.T @ x + b for a batch of examples.
§Arguments
input
- Input features of shape (n_x, m) where n_x is the number of features and m is the batch size
§Returns
Result<Vector, ModelError>
- The predicted values as a vector of shape (m, )
Source§fn backward(
&mut self,
input: &Matrix,
output_grad: &Vector,
) -> Result<(), ModelError>
fn backward( &mut self, input: &Matrix, output_grad: &Vector, ) -> Result<(), ModelError>
Performs backward propagation to compute gradients
Calculates the gradients of the cost function with respect to the model parameters (weights and bias). For linear regression with MSE loss, the gradients are: dw = (1/m) * X @ (y_hat - y).T db = (1/m) * sum(y_hat - y)
§Arguments
input
- Input features of shape (n_x, m)output_grad
- Gradient of the cost with respect to the output, typically (y_hat - y)
§Returns
Result<(), ModelError>
- Success or error status of the operation
Source§fn compute_output_gradient(
&self,
x: &Matrix,
y: &Vector,
) -> Result<Vector, ModelError>
fn compute_output_gradient( &self, x: &Matrix, y: &Vector, ) -> Result<Vector, ModelError>
Computes the gradient of the cost with respect to the output predictions
For Mean Squared Error, the gradient dJ/dy_hat is: (y_hat - y) where y_hat is the model’s prediction and y is the ground truth.
§Arguments
x
- Input features of shape (n_x, m)y
- Target values of shape (m, )
§Returns
Result<Vector, ModelError>
- The gradient of the cost function with respect to outputs
Source§impl ParamCollection for LinearRegression
impl ParamCollection for LinearRegression
Source§fn get<D: Dimension>(
&self,
key: &str,
) -> Result<ArrayView<'_, f64, D>, ModelError>
fn get<D: Dimension>( &self, key: &str, ) -> Result<ArrayView<'_, f64, D>, ModelError>
fn get_mut<D: Dimension>( &mut self, key: &str, ) -> Result<ArrayViewMut<'_, f64, D>, ModelError>
Source§impl RegressionModel<ArrayBase<OwnedRepr<f64>, Dim<[usize; 2]>>, ArrayBase<OwnedRepr<f64>, Dim<[usize; 1]>>> for LinearRegression
impl RegressionModel<ArrayBase<OwnedRepr<f64>, Dim<[usize; 2]>>, ArrayBase<OwnedRepr<f64>, Dim<[usize; 1]>>> for LinearRegression
Source§fn r2(&self, x: &Matrix, y: &Vector) -> Result<f64, ModelError>
fn r2(&self, x: &Matrix, y: &Vector) -> Result<f64, ModelError>
Calculates the R-squared (coefficient of determination) for the model
R² represents the proportion of variance in the dependent variable that is predictable from the independent variables. R² = 1 - (MSE / Variance of y)
§Arguments
x
- Input features of shape (n_x, m)y
- Target values of shape (m, )
§Returns
Result<f64, ModelError>
- The R² value between 0 and 1
Source§fn compute_metrics(
&self,
x: &Matrix,
y: &Vector,
) -> Result<RegressionMetrics, ModelError>
fn compute_metrics( &self, x: &Matrix, y: &Vector, ) -> Result<RegressionMetrics, ModelError>
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> 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