Skip to content

Commit adf9cb1

Browse files
committed
modAL.models.BayesianOptimizer.get_max() method added for obtaining the currently best values
1 parent 4d4810b commit adf9cb1

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

modAL/models.py

+17
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,23 @@ def _set_max(self, y):
459459
if y_max > self.max_val:
460460
self.max_val = y_max
461461

462+
def get_max(self):
463+
"""
464+
Gives the highest value so far.
465+
466+
Returns
467+
-------
468+
X: np.ndarray of shape (n_features)
469+
The location of the currently best value.
470+
471+
y: np.ndarray of shape (1)
472+
The currently best value.
473+
474+
"""
475+
max_idx = np.argmax(self.y_training)
476+
477+
return self.X_training[max_idx], self.y_training[max_idx]
478+
462479
def teach(self, X, y, bootstrap=False, only_new=False, **fit_kwargs):
463480
"""
464481
Adds X and y to the known training data and retrains the predictor with the

tests/core_tests.py

+13
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,19 @@ def test_set_new_max(self):
588588
learner._set_max(y_new)
589589
np.testing.assert_almost_equal(np.max(y_new), learner.max_val)
590590

591+
def test_get_max(self):
592+
for n_samples in range(1, 100):
593+
for max_idx in range(0, n_samples):
594+
X = np.random.rand(n_samples, 3)
595+
y = np.random.rand(n_samples)
596+
y[max_idx] = 10
597+
598+
regressor = mock.MockEstimator()
599+
optimizer = modAL.models.BayesianOptimizer(regressor, X_training=X, y_training=y)
600+
X_max, y_max = optimizer.get_max()
601+
np.testing.assert_equal(X_max, X[max_idx])
602+
np.testing.assert_equal(y_max, y[max_idx])
603+
591604
def test_teach(self):
592605
for bootstrap, only_new in product([True, False], [True, False]):
593606
# case 1. optimizer is uninitialized

0 commit comments

Comments
 (0)