LogisticRegression Class |
Namespace: Accord.Statistics.Models.Regression
The LogisticRegression type exposes the following members.
Name | Description | |
---|---|---|
LogisticRegression |
Creates a new Logistic Regression Model.
| |
LogisticRegression(Int32) | Obsolete.
Creates a new Logistic Regression Model.
| |
LogisticRegression(Int32, Double) | Obsolete.
Creates a new Logistic Regression Model.
|
Name | Description | |
---|---|---|
Coefficients | Obsolete.
Obsolete. For quick compatibility fixes in the short term, use
GetCoefficient(Int32) and SetCoefficient(Int32, Double).
(Inherited from GeneralizedLinearRegression.) | |
Inputs | Obsolete.
Gets the number of inputs handled by this model.
(Inherited from GeneralizedLinearRegression.) | |
Intercept |
Gets or sets the intercept term. This is always the
first value of the Coefficients array.
(Inherited from GeneralizedLinearRegression.) | |
Linear |
Gets the underlying linear regression.
(Inherited from GeneralizedLinearRegression.) | |
Link |
Gets the link function used by
this generalized linear model.
(Inherited from GeneralizedLinearRegression.) | |
NumberOfClasses |
Gets the number of classes expected and recognized by the classifier.
(Inherited from ClassifierBaseTInput, TClasses.) | |
NumberOfInputs |
Gets the number of inputs accepted by the model.
(Inherited from GeneralizedLinearRegression.) | |
NumberOfOutputs |
Gets the number of outputs generated by the model.
(Inherited from TransformBaseTInput, TOutput.) | |
NumberOfParameters |
Gets the number of parameters in this model (equals the NumberOfInputs + 1).
(Inherited from GeneralizedLinearRegression.) | |
StandardErrors |
Gets the standard errors associated with each
coefficient during the model estimation phase.
(Inherited from GeneralizedLinearRegression.) | |
Weights |
Gets or sets the linear weights of the regression model. The
intercept term is not stored in this vector, but is instead
available through the Intercept property.
(Inherited from GeneralizedLinearRegression.) |
Name | Description | |
---|---|---|
ChiSquare(Double, Double) |
The likelihood ratio test of the overall model, also called the model chi-square test.
(Inherited from GeneralizedLinearRegression.) | |
ChiSquare(Double, Double, Double) |
The likelihood ratio test of the overall model, also called the model chi-square test.
(Inherited from GeneralizedLinearRegression.) | |
Clone |
Creates a new GeneralizedLinearRegression that is a copy of the current instance.
(Inherited from GeneralizedLinearRegression.) | |
Compute(Double) | Obsolete.
Computes the model output for the given input vector.
(Inherited from GeneralizedLinearRegression.) | |
Compute(Double) | Obsolete.
Computes the model output for each of the given input vectors.
(Inherited from GeneralizedLinearRegression.) | |
Decide(TInput) |
Computes a class-label decision for a given input.
(Inherited from BinaryScoreClassifierBaseTInput.) | |
Decide(TInput) |
Computes class-label decisions for a given set of input vectors.
(Inherited from ClassifierBaseTInput, TClasses.) | |
Decide(TInput, Boolean) |
Computes class-label decisions for the given input.
(Inherited from BinaryClassifierBaseTInput.) | |
Decide(TInput, Boolean) |
Computes a class-label decision for a given input.
(Inherited from BinaryScoreClassifierBaseTInput.) | |
Equals | Determines whether the specified object is equal to the current object. (Inherited from Object.) | |
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) | |
FromWeights(Double) |
Constructs a new LogisticRegression from
an array of weights (linear coefficients). The first
weight is interpreted as the intercept value.
| |
FromWeights(Double, Double) |
Constructs a new LogisticRegression from
an array of weights (linear coefficients). The first
weight is interpreted as the intercept value.
| |
GetCoefficient |
Gets a coefficient value, where 0 is the intercept term
and the other coefficients are indexed starting at 1.
(Inherited from GeneralizedLinearRegression.) | |
GetConfidenceInterval(Int32) |
Gets the 95% confidence interval for the
Odds Ratio for a given coefficient.
| |
GetConfidenceInterval(Double, Int32, Double, Double) |
Gets the confidence interval for an input point.
(Inherited from GeneralizedLinearRegression.) | |
GetDegreesOfFreedom |
Gets the degrees of freedom when fitting the regression.
(Inherited from GeneralizedLinearRegression.) | |
GetDeviance(Double, Double) |
Gets the Deviance for the model.
(Inherited from GeneralizedLinearRegression.) | |
GetDeviance(Double, Double, Double) |
Gets the Deviance for the model.
(Inherited from GeneralizedLinearRegression.) | |
GetHashCode | Serves as the default hash function. (Inherited from Object.) | |
GetLogLikelihood(Double, Double) |
Gets the Log-Likelihood for the model.
(Inherited from GeneralizedLinearRegression.) | |
GetLogLikelihood(Double, Double, Double) |
Gets the Log-Likelihood for the model.
(Inherited from GeneralizedLinearRegression.) | |
GetLogLikelihoodRatio(Double, Double, GeneralizedLinearRegression) |
Gets the Log-Likelihood Ratio between two models.
(Inherited from GeneralizedLinearRegression.) | |
GetLogLikelihoodRatio(Double, Double, Double, GeneralizedLinearRegression) |
Gets the Log-Likelihood Ratio between two models.
(Inherited from GeneralizedLinearRegression.) | |
GetOddsRatio |
Gets the Odds Ratio for a given coefficient.
| |
GetPredictionInterval |
Gets the prediction interval for an input point.
(Inherited from GeneralizedLinearRegression.) | |
GetPredictionStandardError |
Gets the standard error of the prediction for a particular input vector.
(Inherited from GeneralizedLinearRegression.) | |
GetStandardError |
Gets the standard error of the fit for a particular input vector.
(Inherited from GeneralizedLinearRegression.) | |
GetStandardErrors |
Gets the standard error for each coefficient.
(Inherited from GeneralizedLinearRegression.) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
GetWaldTest |
Gets the Wald Test for a given coefficient.
(Inherited from GeneralizedLinearRegression.) | |
LogLikelihood(TInput) |
Predicts a class label vector for the given input vector, returning the
log-likelihood that the input vector belongs to its predicted class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.) | |
LogLikelihood(TInput) |
Predicts a class label vector for the given input vector, returning the
log-likelihood that the input vector belongs to its predicted class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.) | |
LogLikelihood(TInput, Boolean) |
Predicts a class label vector for the given input vector, returning the
log-likelihood that the input vector belongs to its predicted class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.) | |
LogLikelihood(TInput, Int32) |
Predicts a class label for each input vector, returning the
log-likelihood that each vector belongs to its predicted class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.) | |
LogLikelihood(Double, Double) |
Predicts a class label vector for the given input vectors, returning the
log-likelihood that the input vector belongs to its predicted class.
(Inherited from GeneralizedLinearRegression.) | |
LogLikelihood(TInput, Boolean, Double) |
Predicts a class label for each input vector, returning the
log-likelihood that each vector belongs to its predicted class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.) | |
LogLikelihoods(TInput) |
Computes the log-likelihood that the given input
vector belongs to each of the possible classes.
(Inherited from BinaryLikelihoodClassifierBaseTInput.) | |
LogLikelihoods(TInput) |
Computes the log-likelihoods that the given input
vectors belongs to each of the possible classes.
(Inherited from BinaryLikelihoodClassifierBaseTInput.) | |
LogLikelihoods(TInput, Boolean) |
Predicts a class label vector for the given input vector, returning the
log-likelihoods of the input vector belonging to each possible class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.) | |
LogLikelihoods(TInput, Double) |
Computes the log-likelihood that the given input
vector belongs to each of the possible classes.
(Inherited from BinaryLikelihoodClassifierBaseTInput.) | |
LogLikelihoods(TInput, Double) |
Computes the log-likelihoods that the given input
vectors belongs to each of the possible classes.
(Inherited from BinaryLikelihoodClassifierBaseTInput.) | |
LogLikelihoods(TInput, Int32) |
Predicts a class label vector for each input vector, returning the
log-likelihoods of the input vector belonging to each possible class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.) | |
LogLikelihoods(TInput, Boolean, Double) |
Predicts a class label vector for the given input vector, returning the
log-likelihoods of the input vector belonging to each possible class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.) | |
LogLikelihoods(TInput, Boolean, Double) |
Predicts a class label vector for each input vector, returning the
log-likelihoods of the input vector belonging to each possible class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.) | |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
Probabilities(TInput) |
Computes the probabilities that the given input
vector belongs to each of the possible classes.
(Inherited from BinaryLikelihoodClassifierBaseTInput.) | |
Probabilities(TInput) |
Computes the probabilities that the given input
vectors belongs to each of the possible classes.
(Inherited from BinaryLikelihoodClassifierBaseTInput.) | |
Probabilities(TInput, Boolean) |
Predicts a class label vector for the given input vector, returning the
probabilities of the input vector belonging to each possible class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.) | |
Probabilities(TInput, Double) |
Computes the probabilities that the given input
vector belongs to each of the possible classes.
(Inherited from BinaryLikelihoodClassifierBaseTInput.) | |
Probabilities(TInput, Double) |
Computes the probabilities that the given input
vectors belongs to each of the possible classes.
(Inherited from BinaryLikelihoodClassifierBaseTInput.) | |
Probabilities(TInput, Int32) |
Predicts a class label vector for each input vector, returning the
probabilities of the input vector belonging to each possible class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.) | |
Probabilities(TInput, Boolean, Double) |
Predicts a class label vector for the given input vector, returning the
probabilities of the input vector belonging to each possible class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.) | |
Probabilities(TInput, Boolean, Double) |
Predicts a class label vector for each input vector, returning the
probabilities of the input vector belonging to each possible class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.) | |
Probability(TInput) |
Predicts a class label for the given input vector, returning the
probability that the input vector belongs to its predicted class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.) | |
Probability(TInput) |
Predicts a class label for the given input vector, returning the
probability that the input vector belongs to its predicted class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.) | |
Probability(TInput, Boolean) |
Predicts a class label for the given input vector, returning the
probability that the input vector belongs to its predicted class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.) | |
Probability(TInput, Int32) |
Predicts a class label for each input vector, returning the
probability that each vector belongs to its predicted class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.) | |
Probability(Double, Double) |
Predicts a class label for the given input vector, returning the
probability that the input vector belongs to its predicted class.
(Inherited from GeneralizedLinearRegression.) | |
Probability(TInput, Boolean, Double) |
Predicts a class label for each input vector, returning the
probability that each vector belongs to its predicted class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.) | |
Score(TInput) |
Computes a numerical score measuring the association between
the given input vector and its most strongly
associated class (as predicted by the classifier).
(Inherited from BinaryScoreClassifierBaseTInput.) | |
Score(TInput) |
Computes a numerical score measuring the association between
the given input vector and its most strongly
associated class (as predicted by the classifier).
(Inherited from BinaryScoreClassifierBaseTInput.) | |
Score(TInput, Boolean) |
Predicts a class label for the input vector, returning a
numerical score measuring the strength of association of the
input vector to its most strongly related class.
(Inherited from BinaryScoreClassifierBaseTInput.) | |
Score(TInput, Boolean) |
Predicts a class label for each input vector, returning a
numerical score measuring the strength of association of the
input vector to the most strongly related class.
(Inherited from BinaryScoreClassifierBaseTInput.) | |
Score(Double, Double) |
Computes a numerical score measuring the association between
the given input vector and each class.
(Inherited from GeneralizedLinearRegression.) | |
Score(TInput, Boolean, Double) |
Predicts a class label for each input vector, returning a
numerical score measuring the strength of association of the
input vector to the most strongly related class.
(Inherited from BinaryScoreClassifierBaseTInput.) | |
Scores(TInput) |
Computes a numerical score measuring the association between
the given input vector and each class.
(Inherited from BinaryScoreClassifierBaseTInput.) | |
Scores(TInput) |
Computes a numerical score measuring the association between
the given input vector and each class.
(Inherited from BinaryScoreClassifierBaseTInput.) | |
Scores(TInput, Boolean) |
Predicts a class label vector for the given input vector, returning a
numerical score measuring the strength of association of the input vector
to each of the possible classes.
(Inherited from BinaryScoreClassifierBaseTInput.) | |
Scores(TInput, Double) |
Computes a numerical score measuring the association between
the given input vector and each class.
(Inherited from BinaryScoreClassifierBaseTInput.) | |
Scores(TInput, Boolean) |
Predicts a class label vector for each input vector, returning a
numerical score measuring the strength of association of the input vector
to each of the possible classes.
(Inherited from BinaryScoreClassifierBaseTInput.) | |
Scores(TInput, Double) |
Computes a numerical score measuring the association between
the given input vector and each class.
(Inherited from BinaryScoreClassifierBaseTInput.) | |
Scores(TInput, Boolean, Double) |
Predicts a class label vector for the given input vector, returning a
numerical score measuring the strength of association of the input vector
to each of the possible classes.
(Inherited from BinaryScoreClassifierBaseTInput.) | |
Scores(TInput, Boolean, Double) |
Predicts a class label vector for each input vector, returning a
numerical score measuring the strength of association of the input vector
to each of the possible classes.
(Inherited from BinaryScoreClassifierBaseTInput.) | |
SetCoefficient |
Sets a coefficient value, where 0 is the intercept term
and the other coefficients are indexed starting at 1.
(Inherited from GeneralizedLinearRegression.) | |
ToMulticlass |
Views this instance as a multi-class generative classifier,
giving access to more advanced methods, such as the prediction
of integer labels.
(Inherited from BinaryLikelihoodClassifierBaseTInput.) | |
ToMultilabel |
Views this instance as a multi-label generative classifier,
giving access to more advanced methods, such as the prediction
of one-hot vectors.
(Inherited from BinaryLikelihoodClassifierBaseTInput.) | |
ToString | Returns a string that represents the current object. (Inherited from Object.) | |
Transform(TInput) |
Applies the transformation to an input, producing an associated output.
(Inherited from ClassifierBaseTInput, TClasses.) | |
Transform(TInput) |
Applies the transformation to a set of input vectors,
producing an associated set of output vectors.
(Inherited from TransformBaseTInput, TOutput.) | |
Transform(TInput, Boolean) |
Applies the transformation to an input, producing an associated output.
(Inherited from BinaryClassifierBaseTInput.) | |
Transform(TInput, Int32) |
Applies the transformation to an input, producing an associated output.
(Inherited from BinaryClassifierBaseTInput.) | |
Transform(TInput, Boolean) |
Applies the transformation to an input, producing an associated output.
(Inherited from BinaryClassifierBaseTInput.) | |
Transform(TInput, Int32) |
Applies the transformation to an input, producing an associated output.
(Inherited from BinaryClassifierBaseTInput.) | |
Transform(TInput, Int32) |
Applies the transformation to an input, producing an associated output.
(Inherited from BinaryClassifierBaseTInput.) | |
Transform(TInput, Double) |
Applies the transformation to a set of input vectors,
producing an associated set of output vectors.
(Inherited from BinaryLikelihoodClassifierBaseTInput.) | |
Transform(TInput, Double) |
Applies the transformation to a set of input vectors,
producing an associated set of output vectors.
(Inherited from BinaryLikelihoodClassifierBaseTInput.) | |
Transform(TInput, Double) |
Applies the transformation to a set of input vectors,
producing an associated set of output vectors.
(Inherited from BinaryLikelihoodClassifierBaseTInput.) | |
Transform(TInput, TClasses) |
Applies the transformation to an input, producing an associated output.
(Inherited from ClassifierBaseTInput, TClasses.) |
Name | Description | |
---|---|---|
HasMethod |
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.) | |
IsEqual |
Compares two objects for equality, performing an elementwise
comparison if the elements are vectors or matrices.
(Defined by Matrix.) | |
To(Type) | Overloaded.
Converts an object into another type, irrespective of whether
the conversion can be done at compile time or not. This can be
used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.) | |
ToT | Overloaded.
Converts an object into another type, irrespective of whether
the conversion can be done at compile time or not. This can be
used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.) |
In statistics, logistic regression (sometimes called the logistic model or Logit model) is used for prediction of the probability of occurrence of an event by fitting data to a logistic curve. It is a generalized linear model used for binomial regression.
Like many forms of regression analysis, it makes use of several predictor variables that may be either numerical or categorical. For example, the probability that a person has a heart attack within a specified time period might be predicted from knowledge of the person's age, sex and body mass index.
Logistic regression is used extensively in the medical and social sciences as well as marketing applications such as prediction of a customer's propensity to purchase a product or cease a subscription.
References:
The following example shows how to learn a logistic regression using the standard IterativeReweightedLeastSquares algorithm.
// Suppose we have the following data about some patients. // The first variable is continuous and represent patient // age. The second variable is dichotomic and give whether // they smoke or not (This is completely fictional data). // We also know if they have had lung cancer or not, and // we would like to know whether smoking has any connection // with lung cancer (This is completely fictional data). double[][] input = { // age, smokes?, had cancer? new double[] { 55, 0 }, // false - no cancer new double[] { 28, 0 }, // false new double[] { 65, 1 }, // false new double[] { 46, 0 }, // true - had cancer new double[] { 86, 1 }, // true new double[] { 56, 1 }, // true new double[] { 85, 0 }, // false new double[] { 33, 0 }, // false new double[] { 21, 1 }, // false new double[] { 42, 1 }, // true }; bool[] output = // Whether each patient had lung cancer or not { false, false, false, true, true, true, false, false, false, true }; // To verify this hypothesis, we are going to create a logistic // regression model for those two inputs (age and smoking), learned // using a method called "Iteratively Reweighted Least Squares": var learner = new IterativeReweightedLeastSquares<LogisticRegression>() { Tolerance = 1e-4, // Let's set some convergence parameters Iterations = 100, // maximum number of iterations to perform Regularization = 0 }; // Now, we can use the learner to finally estimate our model: LogisticRegression regression = learner.Learn(input, output); // At this point, we can compute the odds ratio of our variables. // In the model, the variable at 0 is always the intercept term, // with the other following in the sequence. Index 1 is the age // and index 2 is whether the patient smokes or not. // For the age variable, we have that individuals with // higher age have 1.021 greater odds of getting lung // cancer controlling for cigarette smoking. double ageOdds = regression.GetOddsRatio(1); // 1.0208597028836701 // For the smoking/non smoking category variable, however, we // have that individuals who smoke have 5.858 greater odds // of developing lung cancer compared to those who do not // smoke, controlling for age (remember, this is completely // fictional and for demonstration purposes only). double smokeOdds = regression.GetOddsRatio(2); // 5.8584748789881331 // If we would like to use the model to predict a probability for // each patient regarding whether they are at risk of cancer or not, // we can use the Probability function: double[] scores = regression.Probability(input); // Finally, if we would like to arrive at a conclusion regarding // each patient, we can use the Decide method, which will transform // the probabilities (from 0 to 1) into actual true/false values: bool[] actual = regression.Decide(input);
Please note that it is also possible to train logistic regression models using large-margin algorithms. With those algorithms, it is possible to train using different regularization options, such as L1 (with ProbabilisticCoordinateDescent) or L2 (with ProbabilisticDualCoordinateDescent). The following example shows how to obtain L1-regularized regression from a probabilistic linear Support Vector Machine:
// Declare some training data. This is exactly the same // data used in the LogisticRegression documentation page // Suppose we have the following data about some patients. // The first variable is continuous and represent patient // age. The second variable is dichotomic and give whether // they smoke or not (This is completely fictional data). // We also know if they have had lung cancer or not, and // we would like to know whether smoking has any connection // with lung cancer (This is completely fictional data). double[][] input = { // age, smokes?, had cancer? new double[] { 55, 0 }, // false - no cancer new double[] { 28, 0 }, // false new double[] { 65, 1 }, // false new double[] { 46, 0 }, // true - had cancer new double[] { 86, 1 }, // true new double[] { 56, 1 }, // true new double[] { 85, 0 }, // false new double[] { 33, 0 }, // false new double[] { 21, 1 }, // false new double[] { 42, 1 }, // true }; double[] output = // Whether each patient had lung cancer or not { 0, 0, 0, 1, 1, 1, 0, 0, 0, 1 }; // Create the L1-regularization learning algorithm var teacher = new ProbabilisticCoordinateDescent() { Tolerance = 1e-10, Complexity = 1e+10, // learn a hard-margin model }; // Learn the L1-regularized machine var svm = teacher.Learn(input, output); // Convert the svm to logistic regression var regression = (LogisticRegression)svm; // Compute the predicted outcome for inputs bool[] predicted = regression.Decide(input); // Compute log-likelihood scores for the outputs double[] scores = regression.Score(input); // Compute odds-ratio as in the LogisticRegression example double ageOdds = regression.GetOddsRatio(1); // 1.0208597029158772 double smokeOdds = regression.GetOddsRatio(2); // 5.8584748789881331 // Compute the classification error as in SVM example double error = new ZeroOneLoss(output).Loss(predicted);