parent
8dba65dd9c
commit
4da561a6a8
@ -1,15 +0,0 @@
|
||||
# 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.
|
||||
|
||||
from sklearn.metrics import roc_auc_score
|
||||
|
||||
|
||||
class ClassifierEvaluator:
|
||||
def __init__(self) -> None:
|
||||
pass
|
||||
|
||||
def evaluate(self, clf, x_train, y_train):
|
||||
# FIXME: use cross-validation
|
||||
proba = clf.predict_proba(x_train)
|
||||
return roc_auc_score(y_train, proba[:, 1])
|
@ -1,3 +1,39 @@
|
||||
# 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.
|
||||
|
||||
from typing import Tuple
|
||||
|
||||
import numpy as np
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
|
||||
|
||||
def _build_circle_training_data() -> Tuple[np.ndarray, np.ndarray]:
|
||||
x_train = StandardScaler().fit_transform(
|
||||
np.array(
|
||||
[
|
||||
[
|
||||
x1,
|
||||
x2,
|
||||
]
|
||||
for x1 in range(-10, 11)
|
||||
for x2 in range(-10, 11)
|
||||
]
|
||||
)
|
||||
)
|
||||
y_train = np.array(
|
||||
[
|
||||
[
|
||||
False,
|
||||
True,
|
||||
]
|
||||
if x1 * x1 + x2 * x2 <= 100
|
||||
else [
|
||||
True,
|
||||
False,
|
||||
]
|
||||
for x1 in range(-10, 11)
|
||||
for x2 in range(-10, 11)
|
||||
]
|
||||
)
|
||||
return x_train, y_train
|
||||
|
@ -0,0 +1,41 @@
|
||||
# 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.
|
||||
from typing import cast
|
||||
|
||||
from numpy.linalg import norm
|
||||
from sklearn.svm import SVC
|
||||
|
||||
from miplearn import AdaptiveClassifier, ScikitLearnClassifier
|
||||
from miplearn.classifiers.adaptive import CandidateClassifierSpecs
|
||||
from tests.classifiers import _build_circle_training_data
|
||||
|
||||
|
||||
def test_adaptive() -> None:
|
||||
clf = AdaptiveClassifier(
|
||||
candidates={
|
||||
"linear": CandidateClassifierSpecs(
|
||||
classifier=lambda: ScikitLearnClassifier(
|
||||
SVC(
|
||||
probability=True,
|
||||
random_state=42,
|
||||
)
|
||||
)
|
||||
),
|
||||
"poly": CandidateClassifierSpecs(
|
||||
classifier=lambda: ScikitLearnClassifier(
|
||||
SVC(
|
||||
probability=True,
|
||||
kernel="poly",
|
||||
degree=2,
|
||||
random_state=42,
|
||||
)
|
||||
)
|
||||
),
|
||||
}
|
||||
)
|
||||
x_train, y_train = _build_circle_training_data()
|
||||
clf.fit(x_train, y_train)
|
||||
proba = clf.predict_proba(x_train)
|
||||
y_pred = (proba[:, 1] > 0.5).astype(float)
|
||||
assert norm(y_train[:, 1] - y_pred) < 0.1
|
@ -1,20 +0,0 @@
|
||||
# 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 sklearn.neighbors import KNeighborsClassifier
|
||||
|
||||
from miplearn.classifiers.evaluator import ClassifierEvaluator
|
||||
|
||||
|
||||
def test_evaluator():
|
||||
clf_a = KNeighborsClassifier(n_neighbors=1)
|
||||
clf_b = KNeighborsClassifier(n_neighbors=2)
|
||||
x_train = np.array([[0, 0], [1, 0]])
|
||||
y_train = np.array([0, 1])
|
||||
clf_a.fit(x_train, y_train)
|
||||
clf_b.fit(x_train, y_train)
|
||||
ev = ClassifierEvaluator()
|
||||
assert ev.evaluate(clf_a, x_train, y_train) == 1.0
|
||||
assert ev.evaluate(clf_b, x_train, y_train) == 0.5
|
Loading…
Reference in new issue