Struct linfa_logistic::LogisticRegression [−][src]
pub struct LogisticRegression<F: Float> { /* fields omitted */ }
A two-class logistic regression model.
Logistic regression combines linear models with
the sigmoid function sigm(x) = 1/(1+exp(-x))
to learn a family of functions that map the feature space to [0,1]
.
Logistic regression is used in binary classification
by interpreting the predicted value as the probability that the sample
has label 1
. A threshold can be set in the fitted model to decide the minimum
probability needed to classify a sample as 1
, which defaults to 0.5
.
In this implementation any binary set of labels can be used, not necessarily 0
and 1
.
l2 regularization is used by this algorithm and is weighted by parameter alpha
. Setting alpha
close to zero removes regularization and the problem solved minimizes only the
empirical risk. On the other hand, setting alpha
to a high value increases
the weight of the l2 norm of the linear model coefficients in the cost function.
Examples
Here’s an example on how to train a logistic regression model on the winequality
dataset
use linfa::traits::{Fit, Predict}; use linfa_logistic::LogisticRegression; // Example on using binary labels different from 0 and 1 let dataset = linfa_datasets::winequality().map_targets(|x| if *x > 6 { "good" } else { "bad" }); let model = LogisticRegression::default().fit(&dataset).unwrap(); let prediction = model.predict(&dataset);
Implementations
impl<F: Float> LogisticRegression<F>
[src]
impl<F: Float> LogisticRegression<F>
[src]pub fn new() -> LogisticRegression<F>
[src]
Creates a new LogisticRegression with default configuration.
pub fn alpha(self, alpha: F) -> LogisticRegression<F>
[src]
Set the normalization parameter alpha
used for L2 normalization,
defaults to 1.0
.
pub fn with_intercept(self, fit_intercept: bool) -> LogisticRegression<F>
[src]
Configure if an intercept should be fitted, defaults to true
.
pub fn max_iterations(self, max_iterations: u64) -> LogisticRegression<F>
[src]
Configure the maximum number of iterations that the solver should perform,
defaults to 100
.
pub fn gradient_tolerance(self, gradient_tolerance: F) -> LogisticRegression<F>
[src]
Configure the minimum change to the gradient to continue the solver,
defaults to 1e-4
.
pub fn initial_params(
self,
params: Array1<F>,
intercept: F
) -> LogisticRegression<F>
[src]
self,
params: Array1<F>,
intercept: F
) -> LogisticRegression<F>
Configure the initial parameters from where the optimization starts.
The params
array must have the same size as the number of columns of
the feature matrix x
passed to the fit
method
Trait Implementations
impl<F: Float> Default for LogisticRegression<F>
[src]
impl<F: Float> Default for LogisticRegression<F>
[src]fn default() -> LogisticRegression<F>
[src]
impl<'a, C: 'a + PartialOrd + Clone, F: Float, D: Data<Elem = F>, T: AsTargets<Elem = C>> Fit<ArrayBase<D, Dim<[usize; 2]>>, T, Error> for LogisticRegression<F>
[src]
impl<'a, C: 'a + PartialOrd + Clone, F: Float, D: Data<Elem = F>, T: AsTargets<Elem = C>> Fit<ArrayBase<D, Dim<[usize; 2]>>, T, Error> for LogisticRegression<F>
[src]type Object = FittedLogisticRegression<F, C>
fn fit(
&self,
dataset: &DatasetBase<ArrayBase<D, Ix2>, T>
) -> Result<Self::Object>
[src]
&self,
dataset: &DatasetBase<ArrayBase<D, Ix2>, T>
) -> Result<Self::Object>
Given a 2-dimensional feature matrix array x
with shape
(n_samples, n_features) and an array of target classes to predict,
create a FittedLinearRegression
object which allows making
predictions.
The array of target classes y
must have exactly two distinct
values, (e.g. 0.0 and 1.0, 0 and 1, “cat” and “dog”, …), which
represent the two different classes the model is supposed to predict.
The array y
must also have exactly n_samples
items, i.e.
exactly as many items as there are rows in the feature matrix x
.
This method returns an error if any of the preconditions are violated,
i.e. any values are Inf
or NaN
, y
doesn’t have as many items as
x
has rows, or if other parameters (gradient_tolerance, alpha) have
been set to inalid values.
Auto Trait Implementations
impl<F> RefUnwindSafe for LogisticRegression<F> where
F: RefUnwindSafe,
impl<F> RefUnwindSafe for LogisticRegression<F> where
F: RefUnwindSafe,
impl<F> Send for LogisticRegression<F>
impl<F> Send for LogisticRegression<F>
impl<F> Sync for LogisticRegression<F>
impl<F> Sync for LogisticRegression<F>
impl<F> Unpin for LogisticRegression<F> where
F: Unpin,
impl<F> Unpin for LogisticRegression<F> where
F: Unpin,
impl<F> UnwindSafe for LogisticRegression<F> where
F: RefUnwindSafe + UnwindSafe,
impl<F> UnwindSafe for LogisticRegression<F> where
F: RefUnwindSafe + UnwindSafe,