From 659ef4cf9eb30fc8e28884ead27b2a53c20782c5 Mon Sep 17 00:00:00 2001 From: Alinson S Xavier Date: Wed, 15 Apr 2020 08:42:06 -0500 Subject: [PATCH] Always pass (n,)-shaped arrays to regressors instead of (n,1) --- src/python/miplearn/components/objective.py | 10 +++++++--- src/python/miplearn/components/tests/test_objective.py | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/python/miplearn/components/objective.py b/src/python/miplearn/components/objective.py index 2cce8fe..0b53ef6 100644 --- a/src/python/miplearn/components/objective.py +++ b/src/python/miplearn/components/objective.py @@ -41,18 +41,22 @@ class ObjectiveValueComponent(Component): features = InstanceFeaturesExtractor().extract(training_instances) ub = ObjectiveValueExtractor(kind="upper bound").extract(training_instances) lb = ObjectiveValueExtractor(kind="lower bound").extract(training_instances) + assert ub.shape == (len(training_instances), 1) + assert lb.shape == (len(training_instances), 1) self.ub_regressor = deepcopy(self.regressor_prototype) self.lb_regressor = deepcopy(self.regressor_prototype) logger.debug("Fitting ub_regressor...") - self.ub_regressor.fit(features, ub) + self.ub_regressor.fit(features, ub.ravel()) logger.debug("Fitting ub_regressor...") - self.lb_regressor.fit(features, lb) + self.lb_regressor.fit(features, lb.ravel()) def predict(self, instances): features = InstanceFeaturesExtractor().extract(instances) lb = self.lb_regressor.predict(features) ub = self.ub_regressor.predict(features) - return np.hstack([lb, ub]) + assert lb.shape == (len(instances),) + assert ub.shape == (len(instances),) + return np.array([lb, ub]).T def evaluate(self, instances): y_pred = self.predict(instances) diff --git a/src/python/miplearn/components/tests/test_objective.py b/src/python/miplearn/components/tests/test_objective.py index eaf4087..4809f84 100644 --- a/src/python/miplearn/components/tests/test_objective.py +++ b/src/python/miplearn/components/tests/test_objective.py @@ -23,7 +23,7 @@ def test_usage(): def test_obj_evaluate(): instances, models = get_training_instances_and_models() reg = Mock(spec=Regressor) - reg.predict = Mock(return_value=np.array([[1000.0], [1000.0]])) + reg.predict = Mock(return_value=np.array([1000.0, 1000.0])) comp = ObjectiveValueComponent(regressor=reg) comp.fit(instances) ev = comp.evaluate(instances)