def test_approx_cross_validation_OutSamplerTransformer_regressor(multi_output): np.random.seed(123) X = np.random.randn(100, 10) if multi_output: y = np.random.randn(100, 2) else: y = np.random.randn(100) model = OutSamplerTransformer(RandomForestRegressor(n_estimators=10, random_state=123), cv=10) cv_res, yhat = model.approx_cross_validation(X, y, cv=10, method="transform", no_scoring=True) assert cv_res is None assert yhat.ndim == 2 if multi_output: assert yhat.shape == y.shape else: assert yhat.shape == (y.shape[0], 1) with pytest.raises(NotFittedError): model.transform(X) cv = KFold(n_splits=10, shuffle=True, random_state=123) model = OutSamplerTransformer(DummyRegressor(), cv=cv) yhat1 = model.fit_transform(X, y) cv_res, yhat2 = model.approx_cross_validation(X, y, cv=cv, method="transform", no_scoring=True, return_predict=True) # Approx cross val and fit transform should return the same thing here assert np.abs((yhat1 - yhat2).flatten()).max() <= 10**(-5) if multi_output: yhat3 = np.zeros(y.shape) else: yhat3 = np.zeros((y.shape[0], 1)) for train, test in cv.split(X, y): model = DummyRegressor() model.fit(X[train, :], y[train]) if multi_output: yhat3[test, :] = model.predict(X[test, :]) else: yhat3[test, 0] = model.predict(X[test, :]) assert np.abs((yhat1 - yhat3).flatten()).max() <= 10**(-5) assert np.abs((yhat1 - yhat2).flatten()).max() <= 10**(-5)
def test_approx_cross_validation_OutSamplerTransformer_classifier(): np.random.seed(123) X = np.random.randn(100, 10) y = 1 * (np.random.randn(100) > 0) model = OutSamplerTransformer(RandomForestClassifier(random_state=123), cv=10) cv_res, yhat = model.approx_cross_validation(X, y, cv=10, method="transform", no_scoring=True) assert cv_res is None assert yhat.ndim == 2 assert yhat.shape == (y.shape[0], 1) with pytest.raises(NotFittedError): model.transform(X) with pytest.raises(NotFittedError): model.model.predict(X) cv = KFold(n_splits=10, shuffle=True, random_state=123) model = OutSamplerTransformer(LogisticRegression(C=1, random_state=123), cv=cv) yhat1 = model.fit_transform(X, y) model = OutSamplerTransformer(LogisticRegression(C=1, random_state=123), cv=cv) cv_res, yhat2 = model.approx_cross_validation(X, y, cv=cv, method="transform", no_scoring=True, return_predict=True) # Approx cross val and fit transform should return the same thing here assert np.abs((yhat1 - yhat2).flatten()).max() <= 10**(-5) yhat3 = np.zeros((y.shape[0], 1)) for train, test in cv.split(X, y): model = LogisticRegression() model.fit(X[train, :], y[train]) yhat3[test, 0] = model.predict_proba(X[test, :])[:, 1] assert np.abs((yhat1 - yhat3).flatten()).max() <= 10**(-5) assert np.abs((yhat1 - yhat2).flatten()).max() <= 10**(-5)