Sklearn: Handle the special case when all labels are the same

This commit is contained in:
2021-03-02 19:31:12 -06:00
parent b6ea0c5f1b
commit bcaf26b18c
2 changed files with 53 additions and 1 deletions

View File

@@ -0,0 +1,40 @@
# MIPLearn: Extensible Framework for Learning-Enhanced Mixed-Integer Optimization
# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved.
# Released under the modified BSD license. See COPYING.md for more details.
import numpy as np
from numpy.testing import assert_array_equal
from sklearn.neighbors import KNeighborsClassifier
from miplearn import ScikitLearnClassifier
def test_constant_prediction():
x_train = np.array(
[
[0.0, 1.0],
[1.0, 0.0],
]
)
y_train = np.array(
[
[True, False],
[True, False],
]
)
clf = ScikitLearnClassifier(
KNeighborsClassifier(
n_neighbors=1,
)
)
clf.fit(x_train, y_train)
proba = clf.predict_proba(x_train)
assert_array_equal(
proba,
np.array(
[
[1.0, 0.0],
[1.0, 0.0],
]
),
)