Struct LogisticRegression

Source
pub struct LogisticRegression {
    pub weights: Vector,
    pub bias: Vector,
    pub activation_fn: ActivationFn,
    /* private fields */
}
Expand description

A logistic regression model for binary classification.

This model uses a linear combination of features and weights, followed by an activation function, to predict the probability of an input belonging to a positive class.

Fields§

§weights: Vector

Model weights for each feature

§bias: Vector

Bias term (intercept)

§activation_fn: ActivationFn

Activation function used for prediction

Implementations§

Source§

impl LogisticRegression

Source

pub fn new( n_features: usize, activation_fn: ActivationFn, threshold: f64, ) -> Self

Creates a new LogisticRegression model with the specified number of features and activation function.

§Arguments
  • n_features - The number of input features
  • activation_fn - The activation function to use
§Returns

A new LogisticRegression instance with weights initialized to zeros

Source

pub fn builder() -> LogisticRegressionBuilder

Returns a builder for creating LogisticRegression models with custom configurations.

§Returns

A new LogisticRegressionBuilder instance

Trait Implementations§

Source§

impl BaseModel<ArrayBase<OwnedRepr<f64>, Dim<[usize; 2]>>, ArrayBase<OwnedRepr<f64>, Dim<[usize; 1]>>> for LogisticRegression

Implementation of BaseModel trait for LogisticRegression

Source§

fn predict(&self, x: &Matrix) -> Result<Vector, ModelError>

Makes binary predictions for the given input data.

§Arguments
  • x - Input feature matrix
§Returns

A vector of predictions (0.0 or 1.0)

Source§

fn compute_cost(&self, x: &Matrix, y: &Vector) -> Result<f64, ModelError>

Computes the cost/loss for the given input and expected output.

§Arguments
  • x - Input feature matrix
  • y - Expected output vector
§Returns

The computed loss value

Source§

impl Builder<LogisticRegression, ArrayBase<OwnedRepr<f64>, Dim<[usize; 2]>>, ArrayBase<OwnedRepr<f64>, Dim<[usize; 1]>>> for LogisticRegressionBuilder

Source§

fn build(&self) -> Result<LogisticRegression, ModelError>

Builds and returns a new LogisticRegression model with the configured parameters.

§Returns
  • Result<LogisticRegression, ModelError> - A new LogisticRegression instance with the specified configuration, or an error if construction fails
Source§

impl ClassificationModel<ArrayBase<OwnedRepr<f64>, Dim<[usize; 2]>>, ArrayBase<OwnedRepr<f64>, Dim<[usize; 1]>>> for LogisticRegression

Implementation of ClassificationModel trait for LogisticRegression

Source§

fn accuracy(&self, x: &Matrix, y: &Vector) -> Result<f64, ModelError>

Calculates the accuracy of the model on the given data.

§Arguments
  • x - Input feature matrix
  • y - Expected output vector
§Returns

The accuracy as a value between 0.0 and 1.0

Source§

fn loss(&self, x: &Matrix, y: &Vector) -> Result<f64, ModelError>

Calculates the loss (binary cross-entropy) of the model on the given data.

§Arguments
  • x - Input feature matrix
  • y - Expected output vector
§Returns

The computed loss value

Source§

fn recall(&self, x: &Matrix, y: &Vector) -> Result<f64, ModelError>

Calculates the recall (sensitivity) of the model on the given data.

§Arguments
  • x - Input feature matrix
  • y - Expected output vector
§Returns

The recall as a value between 0.0 and 1.0

Source§

fn f1_score(&self, x: &Matrix, y: &Vector) -> Result<f64, ModelError>

Calculates the F1 score of the model on the given data.

§Arguments
  • x - Input feature matrix
  • y - Expected output vector
§Returns

The F1 score as a value between 0.0 and 1.0

Source§

fn compute_metrics( &self, x: &Matrix, y: &Vector, ) -> Result<ClassificationMetrics, ModelError>

Computes all classification metrics for the model on the given data.

§Arguments
  • x - Input feature matrix
  • y - Expected output vector
§Returns

A ClassificationMetrics struct containing accuracy, loss, precision, recall and F1 score

Source§

impl Debug for LogisticRegression

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl GradientCollection for LogisticRegression

Source§

fn get_gradient<D: Dimension>( &self, key: &str, ) -> Result<ArrayView<'_, f64, D>, ModelError>

Get a reference to a specific gradient with strong typing.
Source§

fn set_gradient<D: Dimension>( &mut self, key: &str, value: ArrayView<'_, f64, D>, ) -> Result<(), ModelError>

Set the value of a gradient.
Source§

impl OptimizableModel<ArrayBase<OwnedRepr<f64>, Dim<[usize; 2]>>, ArrayBase<OwnedRepr<f64>, Dim<[usize; 1]>>> for LogisticRegression

Source§

fn compute_output_gradient( &self, x: &Matrix, y: &Vector, ) -> Result<Vector, ModelError>

Computes the gradient of the loss function with regards to the prediction (dJ/dy)

§Arguments
  • x - Input feature matrix
  • y - Expected output vector
§Returns

The gradient vector

Source§

fn forward(&self, input: &Matrix) -> Result<Vector, ModelError>

Forward pass through the model.
Source§

fn backward(&mut self, input: &Matrix, dz: &Vector) -> Result<(), ModelError>

Backward pass to compute gradients.
Source§

impl ParamCollection for LogisticRegression

Source§

fn get<D: Dimension>( &self, key: &str, ) -> Result<ArrayView<'_, f64, D>, ModelError>

Get a reference to a specific parameter with strong typing.
Source§

fn get_mut<D: Dimension>( &mut self, key: &str, ) -> Result<ArrayViewMut<'_, f64, D>, ModelError>

Source§

fn set<D: Dimension>( &mut self, key: &str, value: ArrayView<'_, f64, D>, ) -> Result<(), ModelError>

Set the value of a parameter.
Source§

fn param_iter(&self) -> Vec<(&str, ArrayView<'_, f64, IxDyn>)>

Iterate over all parameters.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V