[docs]classBinaryClassificationPipelineMixin:"""Binary classification pipeline mix-in class."""_threshold=None@propertydefthreshold(self):"""Threshold used to make a prediction. Defaults to None."""returnself._threshold@threshold.setterdefthreshold(self,value):self._threshold=valuedef_predict_with_objective(self,X,ypred_proba,objective):ypred_proba=ypred_proba.iloc[:,1]ifobjectiveisNone:returnypred_proba>self.thresholdreturnobjective.decision_function(ypred_proba,threshold=self.threshold,X=X)def_compute_predictions(self,X,y,objectives,time_series=False):"""Compute predictions/probabilities based on objectives."""y_predicted=Noney_predicted_proba=Noneifany(o.score_needs_probaforoinobjectives)orself.thresholdisnotNone:y_predicted_proba=(self.predict_proba(X,y)iftime_serieselseself.predict_proba(X))ifany(noto.score_needs_probaforoinobjectives)andself.thresholdisNone:y_predicted=(self._predict(X,y,pad=True)iftime_serieselseself._predict(X))returny_predicted,y_predicted_probadef_select_y_pred_for_score(self,X,y,y_pred,y_pred_proba,objective):y_pred_to_use=y_predifself.thresholdisnotNoneandnotobjective.score_needs_proba:y_pred_to_use=self._predict_with_objective(X,y_pred_proba,objective)returny_pred_to_use
[docs]defoptimize_threshold(self,X,y,y_pred_proba,objective):"""Optimize the pipeline threshold given the objective to use. Only used for binary problems with objectives whose thresholds can be tuned. Args: X (pd.DataFrame): Input features. y (pd.Series): Input target values. y_pred_proba (pd.Series): The predicted probabilities of the target outputted by the pipeline. objective (ObjectiveBase): The objective to threshold with. Must have a tunable threshold. Raises: ValueError: If objective is not optimizable. """ifself.can_tune_threshold_with_objective(objective):ifself._encoderisnotNone:y=self._encode_targets(y)self.threshold=objective.optimize_threshold(y_pred_proba,y,X)else:raiseValueError("Problem type must be binary and objective must be optimizable.")