pub struct LogisticRegression<XT: RealNumber, YT: WholeNumber> { /* private fields */ }Expand description
Logistic regression model for binary classification.
This struct represents a logistic regression model for binary classification. It uses the sigmoid function to map the input features to a probability between 0 and 1, and makes predictions based on a threshold of 0.5.
§Type Parameters
XT: The type of the input features.YT: The type of the target labels.
§Fields
weights: The weights of the logistic regression model, with the first being the bias weight._marker: A marker field to indicate the target label type.
§Examples
use rusty_ai::regression::logistic::LogisticRegression;
use rusty_ai::data::dataset::Dataset;
use nalgebra::{DMatrix, DVector};
// Create a new logistic regression model
let mut model: LogisticRegression<f64, u8> = LogisticRegression::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![0, 1, 0]);
let dataset = Dataset::new(x, y);
let lr = 0.01;
let max_steps = 1000;
let epsilon = Some(0.001);
let progress = Some(100);
let result = model.fit(&dataset, lr, max_steps, epsilon, progress);
// Make predictions using the trained model
let x_pred = DMatrix::from_row_slice(2, 2, &[1.0, 2.0, 3.0, 4.0]);
let predictions = model.predict(&x_pred);Implementations§
Source§impl<XT: RealNumber, YT: WholeNumber> LogisticRegression<XT, YT>
impl<XT: RealNumber, YT: WholeNumber> LogisticRegression<XT, YT>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new instance of LogisticRegression with default values.
§Returns
A new LogisticRegression instance.
Sourcepub fn with_params(
dimension: Option<usize>,
weights: Option<DVector<XT>>,
) -> Result<Self, Box<dyn Error>>
pub fn with_params( dimension: Option<usize>, weights: Option<DVector<XT>>, ) -> Result<Self, Box<dyn Error>>
Creates a new instance of LogisticRegression with custom parameters.
§Parameters
dimension: The dimension of the input features. IfNone, it will be inferred from the starting weights.weights: The starting weights for the logistic regression model. IfNone, default weights will be used.
§Returns
A new LogisticRegression instance.
§Errors
An error is returned if the dimension and weights are incompatible.
Sourcepub fn fit(
&mut self,
dataset: &Dataset<XT, YT>,
lr: XT,
max_steps: usize,
epsilon: Option<XT>,
progress: Option<usize>,
) -> Result<String, Box<dyn Error>>
pub fn fit( &mut self, dataset: &Dataset<XT, YT>, lr: XT, max_steps: usize, epsilon: Option<XT>, progress: Option<usize>, ) -> Result<String, Box<dyn Error>>
Fits the logistic regression model to a dataset.
§Parameters
dataset: The dataset to fit the model to.lr: The learning rate for gradient descent.max_steps: The maximum number of steps for gradient descent.epsilon: The convergence threshold for gradient descent. IfNone, a default value is used.progress: The number of steps to display progress information. IfNone, no progress is displayed.
§Returns
A string indicating the result of the training process.
§Errors
An error is returned if the progress steps value is 0.
pub fn weights(&self) -> &DVector<XT>
pub fn cross_entropy( &self, x: &DMatrix<XT>, y: &DVector<YT>, testing: bool, ) -> Result<XT, Box<dyn Error>>
Trait Implementations§
Source§impl<XT: RealNumber, YT: WholeNumber> ClassificationMetrics<YT> for LogisticRegression<XT, YT>
impl<XT: RealNumber, YT: WholeNumber> ClassificationMetrics<YT> for LogisticRegression<XT, YT>
Source§fn confusion_matrix(
&self,
y_true: &DVector<T>,
y_pred: &DVector<T>,
) -> Result<DMatrix<usize>, Box<dyn Error>>
fn confusion_matrix( &self, y_true: &DVector<T>, y_pred: &DVector<T>, ) -> Result<DMatrix<usize>, Box<dyn Error>>
Computes the confusion matrix based on the true labels and predicted labels. Read more
Source§fn accuracy(
&self,
y_true: &DVector<T>,
y_pred: &DVector<T>,
) -> Result<f64, Box<dyn Error>>
fn accuracy( &self, y_true: &DVector<T>, y_pred: &DVector<T>, ) -> Result<f64, Box<dyn Error>>
Computes the accuracy based on the true labels and predicted labels. Read more
Source§fn precision(
&self,
y_true: &DVector<T>,
y_pred: &DVector<T>,
) -> Result<f64, Box<dyn Error>>
fn precision( &self, y_true: &DVector<T>, y_pred: &DVector<T>, ) -> Result<f64, Box<dyn Error>>
Computes the precision based on the true labels and predicted labels. Read more
Source§impl<XT: Clone + RealNumber, YT: Clone + WholeNumber> Clone for LogisticRegression<XT, YT>
impl<XT: Clone + RealNumber, YT: Clone + WholeNumber> Clone for LogisticRegression<XT, YT>
Source§fn clone(&self) -> LogisticRegression<XT, YT>
fn clone(&self) -> LogisticRegression<XT, YT>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<XT: Debug + RealNumber, YT: Debug + WholeNumber> Debug for LogisticRegression<XT, YT>
impl<XT: Debug + RealNumber, YT: Debug + WholeNumber> Debug for LogisticRegression<XT, YT>
Source§impl<XT: RealNumber, YT: WholeNumber> Default for LogisticRegression<XT, YT>
impl<XT: RealNumber, YT: WholeNumber> Default for LogisticRegression<XT, YT>
Auto Trait Implementations§
impl<XT, YT> Freeze for LogisticRegression<XT, YT>
impl<XT, YT> RefUnwindSafe for LogisticRegression<XT, YT>where
YT: RefUnwindSafe,
XT: RefUnwindSafe,
impl<XT, YT> Send for LogisticRegression<XT, YT>
impl<XT, YT> Sync for LogisticRegression<XT, YT>
impl<XT, YT> Unpin for LogisticRegression<XT, YT>
impl<XT, YT> UnwindSafe for LogisticRegression<XT, YT>where
YT: UnwindSafe,
XT: 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
Mutably borrows from an owned value. Read more
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>
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 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>
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 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>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
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
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.