def test_create_scoring(): classifier = RandomForestClassifier() regressor = RandomForestRegressor() res = create_scoring(classifier, "accuracy") assert isinstance(res, OrderedDict) assert "accuracy" in res for k, v in res.items(): assert callable(v) res = create_scoring(regressor, "neg_mean_squared_error") assert isinstance(res, OrderedDict) assert "neg_mean_squared_error" in res for k, v in res.items(): assert callable(v) res = create_scoring( regressor, ["neg_mean_squared_error", "neg_median_absolute_error"]) assert isinstance(res, OrderedDict) assert "neg_mean_squared_error" in res assert "neg_median_absolute_error" in res for k, v in res.items(): assert callable(v) res = create_scoring(regressor, res) assert isinstance(res, OrderedDict) assert "neg_mean_squared_error" in res assert "neg_median_absolute_error" in res for k, v in res.items(): assert callable(v) assert type(v) == type(res[k]) res = create_scoring(regressor, {"scorer1": "accuracy"}) assert isinstance(res, OrderedDict) assert "scorer1" in res for k, v in res.items(): assert callable(v) assert type(v) == type(res[k]) res = create_scoring(regressor, {"scorer1": SCORERS["accuracy"]}) assert isinstance(res, OrderedDict) assert "scorer1" in res for k, v in res.items(): assert callable(v) assert type(v) == type(res[k]) res = create_scoring(regressor, SCORERS["accuracy"]) assert isinstance(res, OrderedDict) for k, v in res.items(): assert callable(v) assert type(v) == type(res[k]) res = create_scoring(regressor, None) assert "default_score" in res assert isinstance(res, OrderedDict) for k, v in res.items(): assert callable(v) assert type(v) == type(res[k])
def test_approx_cross_validation_BoxCoxTargetTransformer(): np.random.seed(123) X = np.random.randn(100, 10) y = np.exp(np.random.randn(100)) for ll in (0, 0.1, 0.5, 2): # Scorer entered as a string # bb = BoxCoxTargetTransformer(Ridge(), ll=ll) cv_res1, yhat1 = bb.approx_cross_validation( X, y, scoring=["neg_mean_squared_error"], cv=10, return_predict=True) assert isinstance(cv_res1, pd.DataFrame) assert cv_res1.shape[0] == 10 assert "test_neg_mean_squared_error" in cv_res1 assert "train_neg_mean_squared_error" in cv_res1 assert yhat1.ndim == 1 assert yhat1.shape[0] == y.shape[0] with pytest.raises(NotFittedError): bb.predict(X) with pytest.raises(NotFittedError): bb.model.predict(X) ######################################### ### Scorer entered as a dictionnary ### ######################################### scoring = create_scoring(Ridge(), ["neg_mean_squared_error"]) cv_res2, yhat2 = bb.approx_cross_validation(X, y, scoring=scoring, cv=10, return_predict=True) assert isinstance(cv_res2, pd.DataFrame) assert cv_res2.shape[0] == 10 assert "test_neg_mean_squared_error" in cv_res2 assert "train_neg_mean_squared_error" in cv_res2 assert yhat2.ndim == 1 assert yhat2.shape[0] == y.shape[0] with pytest.raises(NotFittedError): bb.predict(X) with pytest.raises(NotFittedError): bb.model.predict(X) assert np.abs(cv_res2["test_neg_mean_squared_error"] - cv_res1["test_neg_mean_squared_error"]).max() <= 10**(-5) assert np.abs(cv_res2["train_neg_mean_squared_error"] - cv_res1["train_neg_mean_squared_error"]).max() <= 10**( -5) assert np.max(np.abs(yhat2 - yhat1)) <= 10**(-5)