LogisticRegression
Logistic Regression (aka logit, MaxEnt) classifier.
In the multiclass case, the training algorithm uses the one-vs-rest (OvR) scheme if the ‘multi_class’ option is set to ‘ovr’, and uses the cross-entropy loss if the ‘multi_class’ option is set to ‘multinomial’. (Currently the ‘multinomial’ option is supported only by the ‘lbfgs’, ‘sag’, ‘saga’ and ‘newton-cg’ solvers.)
This class implements regularized logistic regression using the ‘liblinear’ library, ‘newton-cg’, ‘sag’, ‘saga’ and ‘lbfgs’ solvers. Note that regularization is applied by default. It can handle both dense and sparse input. Use C-ordered arrays or CSR matrices containing 64-bit floats for optimal performance; any other input format will be converted (and copied).
The ‘newton-cg’, ‘sag’, and ‘lbfgs’ solvers support only L2 regularization with primal formulation, or no regularization. The ‘liblinear’ solver supports both L1 and L2 regularization, with a dual formulation only for the L2 penalty. The Elastic-Net regularization is only supported by the ‘saga’ solver.
Read more in the User Guide.
Python Reference (opens in a new tab)
Constructors
constructor()
Signature
new LogisticRegression(opts?: object): LogisticRegression;
Parameters
Name | Type | Description |
---|---|---|
opts? | object | - |
opts.C? | number | Inverse of regularization strength; must be a positive float. Like in support vector machines, smaller values specify stronger regularization. Default Value 1 |
opts.class_weight? | any | Weights associated with classes in the form {class\_label: weight} . If not given, all classes are supposed to have weight one. The “balanced” mode uses the values of y to automatically adjust weights inversely proportional to class frequencies in the input data as n\_samples / (n\_classes \* np.bincount(y)) . Note that these weights will be multiplied with sample_weight (passed through the fit method) if sample_weight is specified. |
opts.dual? | boolean | Dual (constrained) or primal (regularized, see also this equation) formulation. Dual formulation is only implemented for l2 penalty with liblinear solver. Prefer dual=false when n_samples > n_features. Default Value false |
opts.fit_intercept? | boolean | Specifies if a constant (a.k.a. bias or intercept) should be added to the decision function. Default Value true |
opts.intercept_scaling? | number | Useful only when the solver ‘liblinear’ is used and self.fit_intercept is set to true . In this case, x becomes [x, self.intercept_scaling], i.e. a “synthetic” feature with constant value equal to intercept_scaling is appended to the instance vector. The intercept becomes intercept\_scaling \* synthetic\_feature\_weight . Note! the synthetic feature weight is subject to l1/l2 regularization as all other features. To lessen the effect of regularization on synthetic feature weight (and therefore on the intercept) intercept_scaling has to be increased. Default Value 1 |
opts.l1_ratio? | number | The Elastic-Net mixing parameter, with 0 <= l1\_ratio <= 1 . Only used if penalty='elasticnet' . Setting l1\_ratio=0 is equivalent to using penalty='l2' , while setting l1\_ratio=1 is equivalent to using penalty='l1' . For 0 < l1\_ratio <1 , the penalty is a combination of L1 and L2. |
opts.max_iter? | number | Maximum number of iterations taken for the solvers to converge. Default Value 100 |
opts.multi_class? | "auto" | "ovr" | "multinomial" | If the option chosen is ‘ovr’, then a binary problem is fit for each label. For ‘multinomial’ the loss minimised is the multinomial loss fit across the entire probability distribution, even when the data is binary. ‘multinomial’ is unavailable when solver=’liblinear’. ‘auto’ selects ‘ovr’ if the data is binary, or if solver=’liblinear’, and otherwise selects ‘multinomial’. Default Value 'auto' |
opts.n_jobs? | number | Number of CPU cores used when parallelizing over classes if multi_class=’ovr’”. This parameter is ignored when the solver is set to ‘liblinear’ regardless of whether ‘multi_class’ is specified or not. undefined means 1 unless in a joblib.parallel\_backend (opens in a new tab) context. \-1 means using all processors. See Glossary for more details. |
opts.penalty? | "l1" | "l2" | "elasticnet" | Specify the norm of the penalty: Default Value 'l2' |
opts.random_state? | number | Used when solver == ‘sag’, ‘saga’ or ‘liblinear’ to shuffle the data. See Glossary for details. |
opts.solver? | "lbfgs" | "newton-cholesky" | "liblinear" | "newton-cg" | "sag" | "saga" | Algorithm to use in the optimization problem. Default is ‘lbfgs’. To choose a solver, you might want to consider the following aspects: Default Value 'lbfgs' |
opts.tol? | number | Tolerance for stopping criteria. Default Value 0.0001 |
opts.verbose? | number | For the liblinear and lbfgs solvers set verbose to any positive number for verbosity. Default Value 0 |
opts.warm_start? | boolean | When set to true , reuse the solution of the previous call to fit as initialization, otherwise, just erase the previous solution. Useless for liblinear solver. See the Glossary. Default Value false |
Returns
Defined in: generated/linear_model/LogisticRegression.ts:29 (opens in a new tab)
Methods
decision_function()
Predict confidence scores for samples.
The confidence score for a sample is proportional to the signed distance of that sample to the hyperplane.
Signature
decision_function(opts: object): Promise<ArrayLike>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
opts.X? | ArrayLike | The data matrix for which we want to get the confidence scores. |
Returns
Promise
<ArrayLike
>
Defined in: generated/linear_model/LogisticRegression.ts:235 (opens in a new tab)
densify()
Convert coefficient matrix to dense array format.
Converts the coef\_
member (back) to a numpy.ndarray. This is the default format of coef\_
and is required for fitting, so calling this method is only required on models that have previously been sparsified; otherwise, it is a no-op.
Signature
densify(opts: object): Promise<any>;
Parameters
Name | Type |
---|---|
opts | object |
Returns
Promise
<any
>
Defined in: generated/linear_model/LogisticRegression.ts:275 (opens in a new tab)
dispose()
Disposes of the underlying Python resources.
Once dispose()
is called, the instance is no longer usable.
Signature
dispose(): Promise<void>;
Returns
Promise
<void
>
Defined in: generated/linear_model/LogisticRegression.ts:216 (opens in a new tab)
fit()
Fit the model according to the given training data.
Signature
fit(opts: object): Promise<any>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
opts.X? | ArrayLike | Training vector, where n\_samples is the number of samples and n\_features is the number of features. |
opts.sample_weight? | any | Array of weights that are assigned to individual samples. If not provided, then each sample is given unit weight. |
opts.y? | ArrayLike | Target vector relative to X. |
Returns
Promise
<any
>
Defined in: generated/linear_model/LogisticRegression.ts:303 (opens in a new tab)
get_metadata_routing()
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
Signature
get_metadata_routing(opts: object): Promise<any>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
opts.routing? | any | A MetadataRequest encapsulating routing information. |
Returns
Promise
<any
>
Defined in: generated/linear_model/LogisticRegression.ts:354 (opens in a new tab)
init()
Initializes the underlying Python resources.
This instance is not usable until the Promise
returned by init()
resolves.
Signature
init(py: PythonBridge): Promise<void>;
Parameters
Name | Type |
---|---|
py | PythonBridge |
Returns
Promise
<void
>
Defined in: generated/linear_model/LogisticRegression.ts:156 (opens in a new tab)
predict()
Predict class labels for samples in X.
Signature
predict(opts: object): Promise<ArrayLike>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
opts.X? | ArrayLike | The data matrix for which we want to get the predictions. |
Returns
Promise
<ArrayLike
>
Defined in: generated/linear_model/LogisticRegression.ts:392 (opens in a new tab)
predict_log_proba()
Predict logarithm of probability estimates.
The returned estimates for all classes are ordered by the label of classes.
Signature
predict_log_proba(opts: object): Promise<ArrayLike[]>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
opts.X? | ArrayLike [] | Vector to be scored, where n\_samples is the number of samples and n\_features is the number of features. |
Returns
Promise
<ArrayLike
[]>
Defined in: generated/linear_model/LogisticRegression.ts:429 (opens in a new tab)
predict_proba()
Probability estimates.
The returned estimates for all classes are ordered by the label of classes.
For a multi_class problem, if multi_class is set to be “multinomial” the softmax function is used to find the predicted probability of each class. Else use a one-vs-rest approach, i.e calculate the probability of each class assuming it to be positive using the logistic function. and normalize these values across all the classes.
Signature
predict_proba(opts: object): Promise<ArrayLike[]>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
opts.X? | ArrayLike [] | Vector to be scored, where n\_samples is the number of samples and n\_features is the number of features. |
Returns
Promise
<ArrayLike
[]>
Defined in: generated/linear_model/LogisticRegression.ts:471 (opens in a new tab)
score()
Return the mean accuracy on the given test data and labels.
In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.
Signature
score(opts: object): Promise<number>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
opts.X? | ArrayLike [] | Test samples. |
opts.sample_weight? | ArrayLike | Sample weights. |
opts.y? | ArrayLike | True labels for X . |
Returns
Promise
<number
>
Defined in: generated/linear_model/LogisticRegression.ts:510 (opens in a new tab)
set_fit_request()
Request metadata passed to the fit
method.
Note that this method is only relevant if enable\_metadata\_routing=True
(see sklearn.set\_config
). Please see User Guide on how the routing mechanism works.
The options for each parameter are:
Signature
set_fit_request(opts: object): Promise<any>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
opts.sample_weight? | string | boolean | Metadata routing for sample\_weight parameter in fit . |
Returns
Promise
<any
>
Defined in: generated/linear_model/LogisticRegression.ts:563 (opens in a new tab)
set_score_request()
Request metadata passed to the score
method.
Note that this method is only relevant if enable\_metadata\_routing=True
(see sklearn.set\_config
). Please see User Guide on how the routing mechanism works.
The options for each parameter are:
Signature
set_score_request(opts: object): Promise<any>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
opts.sample_weight? | string | boolean | Metadata routing for sample\_weight parameter in score . |
Returns
Promise
<any
>
Defined in: generated/linear_model/LogisticRegression.ts:605 (opens in a new tab)
sparsify()
Convert coefficient matrix to sparse format.
Converts the coef\_
member to a scipy.sparse matrix, which for L1-regularized models can be much more memory- and storage-efficient than the usual numpy.ndarray representation.
The intercept\_
member is not converted.
Signature
sparsify(opts: object): Promise<any>;
Parameters
Name | Type |
---|---|
opts | object |
Returns
Promise
<any
>
Defined in: generated/linear_model/LogisticRegression.ts:647 (opens in a new tab)
Properties
_isDisposed
boolean
=false
Defined in: generated/linear_model/LogisticRegression.ts:27 (opens in a new tab)
_isInitialized
boolean
=false
Defined in: generated/linear_model/LogisticRegression.ts:26 (opens in a new tab)
_py
PythonBridge
Defined in: generated/linear_model/LogisticRegression.ts:25 (opens in a new tab)
id
string
Defined in: generated/linear_model/LogisticRegression.ts:22 (opens in a new tab)
opts
any
Defined in: generated/linear_model/LogisticRegression.ts:23 (opens in a new tab)
Accessors
classes_
A list of class labels known to the classifier.
Signature
classes_(): Promise<ArrayLike>;
Returns
Promise
<ArrayLike
>
Defined in: generated/linear_model/LogisticRegression.ts:675 (opens in a new tab)
coef_
Coefficient of the features in the decision function.
coef\_
is of shape (1, n_features) when the given problem is binary. In particular, when multi\_class='multinomial'
, coef\_
corresponds to outcome 1 (true
) and \-coef\_
corresponds to outcome 0 (false
).
Signature
coef_(): Promise<ArrayLike[]>;
Returns
Promise
<ArrayLike
[]>
Defined in: generated/linear_model/LogisticRegression.ts:704 (opens in a new tab)
feature_names_in_
Names of features seen during fit. Defined only when X
has feature names that are all strings.
Signature
feature_names_in_(): Promise<ArrayLike>;
Returns
Promise
<ArrayLike
>
Defined in: generated/linear_model/LogisticRegression.ts:787 (opens in a new tab)
intercept_
Intercept (a.k.a. bias) added to the decision function.
If fit\_intercept
is set to false
, the intercept is set to zero. intercept\_
is of shape (1,) when the given problem is binary. In particular, when multi\_class='multinomial'
, intercept\_
corresponds to outcome 1 (true
) and \-intercept\_
corresponds to outcome 0 (false
).
Signature
intercept_(): Promise<ArrayLike>;
Returns
Promise
<ArrayLike
>
Defined in: generated/linear_model/LogisticRegression.ts:733 (opens in a new tab)
n_features_in_
Number of features seen during fit.
Signature
n_features_in_(): Promise<number>;
Returns
Promise
<number
>
Defined in: generated/linear_model/LogisticRegression.ts:760 (opens in a new tab)
n_iter_
Actual number of iterations for all classes. If binary or multinomial, it returns only 1 element. For liblinear solver, only the maximum number of iteration across all classes is given.
Signature
n_iter_(): Promise<ArrayLike>;
Returns
Promise
<ArrayLike
>
Defined in: generated/linear_model/LogisticRegression.ts:814 (opens in a new tab)
py
Signature
py(): PythonBridge;
Returns
PythonBridge
Defined in: generated/linear_model/LogisticRegression.ts:143 (opens in a new tab)
Signature
py(pythonBridge: PythonBridge): void;
Parameters
Name | Type |
---|---|
pythonBridge | PythonBridge |
Returns
void
Defined in: generated/linear_model/LogisticRegression.ts:147 (opens in a new tab)