Skip to content

Commit

Permalink
Fix issues with sklearn 1.4
Browse files Browse the repository at this point in the history
DON'T MERGE YET.

Some skorch tests fail with sklearn v1.4. This commit fixes them:

1. Inheritance structure of scorers seems to have changed.
2. classes_ attribute should always be numpy array
3. CalibratedClassifierCV only works with predict_proba being float64

To fix the latter, I'm now casting the output of predict_proba to
float64. However, I'm not sure if this is a good idea, as it might break
existing code. I'm just adding it in for now to check if the tests pass.
  • Loading branch information
BenjaminBossan committed Feb 8, 2024
1 parent 0d3a8ec commit d022b23
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion skorch/callbacks/scoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def convert_sklearn_metric_function(scoring):

# those are scoring objects returned by make_scorer starting
# from sklearn 0.22
scorer_names = ('_PredictScorer', '_ProbaScorer', '_ThresholdScorer')
scorer_names = ('_PredictScorer', '_ProbaScorer', '_ThresholdScorer', '_Scorer')
if (
hasattr(module, 'startswith') and
module.startswith('sklearn.metrics.') and
Expand Down
4 changes: 2 additions & 2 deletions skorch/classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def classes_(self):
if not len(self.classes):
raise AttributeError("{} has no attribute 'classes_'".format(
self.__class__.__name__))
return self.classes
return np.asarray(self.classes)

try:
return self.classes_inferred_
Expand Down Expand Up @@ -301,7 +301,7 @@ def _default_callbacks(self):

@property
def classes_(self):
return [0, 1]
return np.array([0, 1])

# pylint: disable=signature-differs
def check_data(self, X, y):
Expand Down
2 changes: 1 addition & 1 deletion skorch/net.py
Original file line number Diff line number Diff line change
Expand Up @@ -1597,7 +1597,7 @@ def predict_proba(self, X):
yp = nonlin(yp)
y_probas.append(to_numpy(yp))
y_proba = np.concatenate(y_probas, 0)
return y_proba
return y_proba.astype(np.float64)

def predict(self, X):
"""Where applicable, return class labels for samples in X.
Expand Down

0 comments on commit d022b23

Please sign in to comment.