in bayesmark/sklearn_funcs.py [0:0]
def evaluate(self, params):
"""Evaluate the sklearn CV objective at a particular parameter setting.
Parameters
----------
params : dict(str, object)
The varying (non-fixed) parameter dict to the sklearn model.
Returns
-------
cv_loss : float
Average loss over CV splits for sklearn model when tested using the settings in params.
"""
params = dict(params) # copy to avoid modification of original
params.update(self.fixed_params) # add in fixed params
# now build the skl object
clf = self.base_model(**params)
assert np.all(np.isfinite(self.data_X)), "all features must be finite"
assert np.all(np.isfinite(self.data_y)), "all targets must be finite"
# Do the x-val, ignore user warn since we expect BO to try weird stuff
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=UserWarning)
S = cross_val_score(clf, self.data_X, self.data_y, scoring=self.scorer, cv=CV_SPLITS)
# Take the mean score across all x-val splits
cv_score = np.mean(S)
# Now let's get the generalization error for same hypers
clf = self.base_model(**params)
clf.fit(self.data_X, self.data_y)
generalization_score = self.scorer(clf, self.data_Xt, self.data_yt)
# get_scorer makes everything a score not a loss, so we need to negate to get the loss back
cv_loss = -cv_score
assert np.isfinite(cv_loss), "loss not even finite"
generalization_loss = -generalization_score
assert np.isfinite(generalization_loss), "loss not even finite"
# Unbox to basic float to keep it simple
cv_loss = cv_loss.item()
assert isinstance(cv_loss, float)
generalization_loss = generalization_loss.item()
assert isinstance(generalization_loss, float)
# For now, score with same objective. We can later add generalization error
return cv_loss, generalization_loss