Class: 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.
Constructors
new LogisticRegression()
new LogisticRegression(
opts
?):LogisticRegression
Parameters
Parameter | 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. |
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. |
opts.fit_intercept ? | boolean | Specifies if a constant (a.k.a. bias or intercept) should be added to the decision function. |
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. |
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. |
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’. |
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 context. \-1 means using all processors. See Glossary for more details. |
opts.penalty ? | "l1" | "l2" | "elasticnet" | Specify the norm of the penalty: |
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: |
opts.tol ? | number | Tolerance for stopping criteria. |
opts.verbose ? | number | For the liblinear and lbfgs solvers set verbose to any positive number for verbosity. |
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. |
Returns LogisticRegression
Defined in generated/linear_model/LogisticRegression.ts:29
Properties
Property | Type | Default value | Defined in |
---|---|---|---|
_isDisposed | boolean | false | generated/linear_model/LogisticRegression.ts:27 |
_isInitialized | boolean | false | generated/linear_model/LogisticRegression.ts:26 |
_py | PythonBridge | undefined | generated/linear_model/LogisticRegression.ts:25 |
id | string | undefined | generated/linear_model/LogisticRegression.ts:22 |
opts | any | undefined | generated/linear_model/LogisticRegression.ts:23 |
Accessors
classes_
Get Signature
get classes_():
Promise
<ArrayLike
>
A list of class labels known to the classifier.
Returns Promise
<ArrayLike
>
Defined in generated/linear_model/LogisticRegression.ts:636
coef_
Get Signature
get coef_():
Promise
<ArrayLike
[]>
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
).
Returns Promise
<ArrayLike
[]>
Defined in generated/linear_model/LogisticRegression.ts:665
feature_names_in_
Get Signature
get feature_names_in_():
Promise
<ArrayLike
>
Names of features seen during fit. Defined only when X
has feature names that are all strings.
Returns Promise
<ArrayLike
>
Defined in generated/linear_model/LogisticRegression.ts:748
intercept_
Get Signature
get intercept_():
Promise
<ArrayLike
>
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
).
Returns Promise
<ArrayLike
>
Defined in generated/linear_model/LogisticRegression.ts:694
n_features_in_
Get Signature
get n_features_in_():
Promise
<number
>
Number of features seen during fit.
Returns Promise
<number
>
Defined in generated/linear_model/LogisticRegression.ts:721
n_iter_
Get Signature
get n_iter_():
Promise
<ArrayLike
>
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.
Returns Promise
<ArrayLike
>
Defined in generated/linear_model/LogisticRegression.ts:775
py
Get Signature
get py():
PythonBridge
Returns PythonBridge
Set Signature
set py(
pythonBridge
):void
Parameters
Parameter | Type |
---|---|
pythonBridge | PythonBridge |
Returns void
Defined in generated/linear_model/LogisticRegression.ts:143
Methods
decision_function()
decision_function(
opts
):Promise
<ArrayLike
>
Predict confidence scores for samples.
The confidence score for a sample is proportional to the signed distance of that sample to the hyperplane.
Parameters
Parameter | 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:218
densify()
densify(
opts
):Promise
<any
>
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.
Parameters
Parameter | Type |
---|---|
opts | object |
Returns Promise
<any
>
Defined in generated/linear_model/LogisticRegression.ts:256
dispose()
dispose():
Promise
<void
>
Disposes of the underlying Python resources.
Once dispose()
is called, the instance is no longer usable.
Returns Promise
<void
>
Defined in generated/linear_model/LogisticRegression.ts:199
fit()
fit(
opts
):Promise
<any
>
Fit the model according to the given training data.
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.sample_weight ? | any | Array of weights that are assigned to individual samples. If not provided, then each sample is given unit weight. |
opts.X ? | ArrayLike | Training vector, where n_samples is the number of samples and n_features is the number of features. |
opts.y ? | ArrayLike | Target vector relative to X. |
Returns Promise
<any
>
Defined in generated/linear_model/LogisticRegression.ts:284
get_metadata_routing()
get_metadata_routing(
opts
):Promise
<any
>
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.routing ? | any | A MetadataRequest encapsulating routing information. |
Returns Promise
<any
>
Defined in generated/linear_model/LogisticRegression.ts:330
init()
init(
py
):Promise
<void
>
Initializes the underlying Python resources.
This instance is not usable until the Promise
returned by init()
resolves.
Parameters
Parameter | Type |
---|---|
py | PythonBridge |
Returns Promise
<void
>
Defined in generated/linear_model/LogisticRegression.ts:156
predict()
predict(
opts
):Promise
<ArrayLike
>
Predict class labels for samples in X.
Parameters
Parameter | 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:366
predict_log_proba()
predict_log_proba(
opts
):Promise
<ArrayLike
[]>
Predict logarithm of probability estimates.
The returned estimates for all classes are ordered by the label of classes.
Parameters
Parameter | 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:402
predict_proba()
predict_proba(
opts
):Promise
<ArrayLike
[]>
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.
Parameters
Parameter | 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:442
score()
score(
opts
):Promise
<number
>
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.
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.sample_weight ? | ArrayLike | Sample weights. |
opts.X ? | ArrayLike [] | Test samples. |
opts.y ? | ArrayLike | True labels for X . |
Returns Promise
<number
>
Defined in generated/linear_model/LogisticRegression.ts:480
set_fit_request()
set_fit_request(
opts
):Promise
<any
>
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:
Parameters
Parameter | 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:528
set_score_request()
set_score_request(
opts
):Promise
<any
>
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:
Parameters
Parameter | 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:568
sparsify()
sparsify(
opts
):Promise
<any
>
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.
Parameters
Parameter | Type |
---|---|
opts | object |
Returns Promise
<any
>