-
I've got the following code: import platform
import scipy
import sklearn
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import StratifiedKFold
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.ensemble import IsolationForest
from sklearn.metrics import average_precision_score, make_scorer, roc_auc_score
import numpy as np
from copy import deepcopy
print(platform.python_version())
print(sklearn.__version__)
def unsupervised_score(y_true, y_pred, scorer):
y_pred_copy = convert_unsupervised_labels(y_pred)
return scorer(y_true, y_pred_copy)
def convert_unsupervised_labels(y):
yc = deepcopy(y)
yc[np.argwhere(yc == 1)] = 0
yc[np.argwhere(yc == -1)] = 1
return yc
# ---------------------
estimator = sklearn.pipeline.Pipeline([("estimator", None)])
grid = [
dict(
estimator=[IsolationForest(random_state=0)],
estimator__n_estimators=range(50, 201),
estimator__contamination=[0.001],
),
]
scoring_unsup = {
"roc_auc": make_scorer(
unsupervised_score,
greater_is_better=True,
scorer=roc_auc_score,
needs_threshold=True,
)
}
cv = StratifiedKFold(n_splits=2)
rs = GridSearchCV(estimator=estimator,
param_grid=grid,
cv=cv,
scoring=scoring_unsup,
refit="roc_auc",
error_score="raise", return_train_score=True,)
# Generate random data for binary classification
X, y = make_classification(
n_samples=10, # Number of samples
n_features=3, # Number of features
n_informative=2, # Number of informative features
n_redundant=1, # Number of redundant features
n_clusters_per_class=1, # Number of clusters per class
random_state=42,
)
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
rs.fit(X_train, y_train)
print(rs.best_estimator_) that for the latest version of sklearn
The code works fine in The error is not quite informative as it suggests adding Why this breaking change and how to fix it? I'm looking for an easy (can be hacky) fix so it doesn't change much in this pipeline. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Thanks a lot for the reproducing script! I did a small edit to replace undefined I don't get the error in the development version, #27565 may be the PR that fixed it (not 100% sure), can you try installing the development version to confirm if that fixes it for you? |
Beta Was this translation helpful? Give feedback.
-
@lesteve I have a similar problem when using |
Beta Was this translation helpful? Give feedback.
Thanks a lot for the reproducing script! I did a small edit to replace undefined
y_copy
byyc
.I don't get the error in the development version, #27565 may be the PR that fixed it (not 100% sure), can you try installing the development version to confirm if that fixes it for you?