mirror of
https://github.com/ANL-CEEESA/MIPLearn.git
synced 2025-12-06 09:28:51 -06:00
Add ScikitLearnRegressor; move sklean classes to their own file
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
# 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
|
||||
|
||||
@@ -6,7 +6,7 @@ import numpy as np
|
||||
from numpy.linalg import norm
|
||||
from sklearn.svm import SVC
|
||||
|
||||
from miplearn.classifiers import ScikitLearnClassifier
|
||||
from miplearn.classifiers.sklearn import ScikitLearnClassifier
|
||||
from miplearn.classifiers.cv import CrossValidatedClassifier
|
||||
from tests.classifiers import _build_circle_training_data
|
||||
|
||||
|
||||
@@ -4,37 +4,30 @@
|
||||
|
||||
import numpy as np
|
||||
from numpy.testing import assert_array_equal
|
||||
from sklearn.linear_model import LinearRegression
|
||||
from sklearn.neighbors import KNeighborsClassifier
|
||||
|
||||
from miplearn import ScikitLearnClassifier
|
||||
from miplearn.classifiers.sklearn import ScikitLearnClassifier, ScikitLearnRegressor
|
||||
|
||||
|
||||
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,
|
||||
)
|
||||
)
|
||||
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],
|
||||
]
|
||||
),
|
||||
np.array([[1.0, 0.0], [1.0, 0.0]]),
|
||||
)
|
||||
|
||||
|
||||
def test_regressor():
|
||||
x_train = np.array([[0.0, 1.0], [1.0, 4.0], [2.0, 2.0]])
|
||||
y_train = np.array([[1.0], [5.0], [4.0]])
|
||||
x_test = np.array([[4.0, 4.0], [0.0, 0.0]])
|
||||
clf = ScikitLearnRegressor(LinearRegression())
|
||||
clf.fit(x_train, y_train)
|
||||
y_test_actual = clf.predict(x_test)
|
||||
y_test_expected = np.array([[8.0], [0.0]])
|
||||
assert_array_equal(np.round(y_test_actual, 2), y_test_expected)
|
||||
|
||||
Reference in New Issue
Block a user