pub struct LogisticRegression {
pub learning_rate: f32,
pub bias: f32,
pub weights: Vec<f32>,
pub l1: f32,
pub l2: f32,
pub features: HashMap<Bytes, usize>,
pub trained: bool,
pub original_ngrams: u32,
pub file_type: FileType,
/* private fields */
}Expand description
Machine learning model using logistic regression
Fields§
§learning_rate: f32Learning rate
bias: f32Bias term
weights: Vec<f32>Model’s weights
l1: f32L1 LASSO regularization
l2: f32L2 Ridge regularization
features: HashMap<Bytes, usize>N-grams used to train the model
trained: boolIf the model has been trained
original_ngrams: u32Amount of n-grams originally used
file_type: FileTypeThe type of file this model is trained on
Implementations§
Source§impl LogisticRegression
impl LogisticRegression
Sourcepub fn new(input_size: usize, learning_rate: f32, l1: f32, l2: f32) -> Self
pub fn new(input_size: usize, learning_rate: f32, l1: f32, l2: f32) -> Self
New logistic regression model with random initial weights from given parameters.
Sourcepub fn new_from_dataset_and_train(
dataset: &Dataset,
epochs: u32,
learning_rate: f32,
l1: f32,
l2: f32,
) -> (Self, f32)
pub fn new_from_dataset_and_train( dataset: &Dataset, epochs: u32, learning_rate: f32, l1: f32, l2: f32, ) -> (Self, f32)
Returns a trained logistic regression model from given parameters and dataset. Returns the model and the error value from the last epoch.
§Panics
This code won’t panic, but LogisticRegression::train would panic if the data sizes were
different, but that can’t happen in this case since the same object is used in both places.
Sourcepub fn predict(&self, input: &[f32]) -> f32
pub fn predict(&self, input: &[f32]) -> f32
Predicts the output for a given input vector. This will fail if the input vector isn’t the same length as the weights vector.
Sourcepub fn train(
&mut self,
epochs: u32,
dataset: &Dataset,
) -> Result<f32, &'static str>
pub fn train( &mut self, epochs: u32, dataset: &Dataset, ) -> Result<f32, &'static str>
Trains the classifier once with the given inputs and outputs.
§Errors
Returns an error if the data isn’t the correct size or if labels are missing.
Sourcepub fn evaluate_dataset<'a>(
&self,
dataset: &'a Dataset,
) -> Result<ConfusionMatrix<'a>>
pub fn evaluate_dataset<'a>( &self, dataset: &'a Dataset, ) -> Result<ConfusionMatrix<'a>>
Evaluate a dataset
§Errors
If the dataset doesn’t match the model weight length or doesn’t have labels
Sourcepub fn evaluate_file<P: AsRef<Path>>(
&self,
path: P,
) -> Result<(&'static str, f32, u32)>
pub fn evaluate_file<P: AsRef<Path>>( &self, path: P, ) -> Result<(&'static str, f32, u32)>
Evaluate a file
§Errors
Errors will result if the model doesn’t have features or if the sample file can’t be read
Sourcepub fn reduce(&mut self)
pub fn reduce(&mut self)
Remove small weights (from regularization)
TODO: Ensure the filtered weight comparison value is sane
Trait Implementations§
Source§impl Clone for LogisticRegression
impl Clone for LogisticRegression
Source§fn clone(&self) -> LogisticRegression
fn clone(&self) -> LogisticRegression
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for LogisticRegression
impl Debug for LogisticRegression
Source§impl<'de> Deserialize<'de> for LogisticRegression
impl<'de> Deserialize<'de> for LogisticRegression
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl PartialEq for LogisticRegression
impl PartialEq for LogisticRegression
Source§impl Serialize for LogisticRegression
impl Serialize for LogisticRegression
impl StructuralPartialEq for LogisticRegression
Auto Trait Implementations§
impl Freeze for LogisticRegression
impl RefUnwindSafe for LogisticRegression
impl Send for LogisticRegression
impl Sync for LogisticRegression
impl Unpin for LogisticRegression
impl UnwindSafe for LogisticRegression
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> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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