add_logistic_regression_constr#

gurobi_ml.sklearn.logistic_regression.add_logistic_regression_constr(gp_model, logistic_regression, input_vars, output_vars=None, output_type='classification', epsilon=0.0, pwl_attributes=None, **kwargs)#

Formulate logistic_regression in gp_model.

The formulation predicts the values of output_vars using input_vars according to logistic_regression.

For users of Gurobi ≥ 11.0, the attribute FuncNonLinear is set to 1 to deal directly with the logistic function in an algorithmic fashion.

For older versions, Gurobi makes a piecewise linear approximation of the logistic function. The quality of the approximation can be controlled with the parameter pwl_attributes. By default, it is parametrized so that the maximal error of the approximation is 1e-2.

See our Users Guide for details on the mip formulation used.

Parameters:
  • gp_model (gurobipy model) – The gurobipy model where the predictor should be inserted.

  • logistic_regression (sklearn.linear_model.LogisticRegression) – The logistic regression to insert.

  • input_vars (mvar_array_like) – Decision variables used as input for logistic regression in model.

  • output_vars (mvar_array_like, optional) – Decision variables used as output for logistic regression in model.

  • output_type ({'classification', 'probability_1'}, default='classification') – If the option chosen is ‘classification’ the output is the class label of either 0 or 1 given by the logistic regression. If the option ‘probability_1’ is chosen the output is the probability of the class 1.

  • epsilon (float, default=0.0) –

    When the output_type is ‘classification’, this tolerance can be set to enforce that class 1 is chosen when the result of the logistic function is greater or equal to 0.5 + epsilon.

    By default, with the value of 0.0, if the result of the logistic function is very close to 0.5 (up to Gurobi tolerances) in the solution of the optimization model, the output of the regression can be either 0 or 1. The optimization model doesn’t make a distinction between the two values.

    Setting esilon to a small value will remove this ambiguity on the output but may also make the model infeasible if the problem is very constrained: the open interval (0.5, 0.5 + epsilon) is excluded from the feasible set of the optimization problem.

  • pwl_attributes (dict, optional) – Dictionary for non-default attributes for Gurobi to build the piecewise linear approximation of the logistic function. The default values for those attributes set in the package can be obtained with LogisticRegressionConstr.default_pwl_attributes(). The dictionary keys should be the attributes for modeling piece wise linear functions and the values the corresponding value the users wants to pass to Gurobi.

Returns:

Object containing information about what was added to gp_model to formulate logistic_regression.

Return type:

LogisticRegressionConstr

Raises:
  • NoModel – If the logistic regression is not a binary label regression

  • ParameterError – If the value of output_type is set to a non-conforming value (see above).

Notes

See add_predictor_constr for acceptable values for input_vars and output_vars