|
33 | 33 | from sklearn.preprocessing import StandardScaler |
34 | 34 | from sklearn.pipeline import FeatureUnion |
35 | 35 |
|
| 36 | +from imblearn.datasets import make_imbalance |
36 | 37 | from imblearn.pipeline import Pipeline, make_pipeline |
37 | | -from imblearn.under_sampling import ( |
38 | | - RandomUnderSampler, |
39 | | - EditedNearestNeighbours as ENN, |
40 | | -) |
| 38 | +from imblearn.under_sampling import RandomUnderSampler |
| 39 | +from imblearn.under_sampling import EditedNearestNeighbours as ENN |
41 | 40 |
|
42 | 41 |
|
43 | 42 | JUNK_FOOD_DOCS = ( |
@@ -1324,3 +1323,22 @@ def test_verbose(est, method, pattern, capsys): |
1324 | 1323 | est.set_params(verbose=True) |
1325 | 1324 | func(X, y) |
1326 | 1325 | assert re.match(pattern, capsys.readouterr().out) |
| 1326 | + |
| 1327 | + |
| 1328 | +def test_pipeline_score_samples_pca_lof(): |
| 1329 | + X, y = load_iris(return_X_y=True) |
| 1330 | + sampling_strategy = {0: 50, 1: 30, 2: 20} |
| 1331 | + X, y = make_imbalance(X, y, sampling_strategy=sampling_strategy) |
| 1332 | + # Test that the score_samples method is implemented on a pipeline. |
| 1333 | + # Test that the score_samples method on pipeline yields same results as |
| 1334 | + # applying transform and score_samples steps separately. |
| 1335 | + rus = RandomUnderSampler() |
| 1336 | + pca = PCA(svd_solver='full', n_components='mle', whiten=True) |
| 1337 | + lof = LocalOutlierFactor(novelty=True) |
| 1338 | + pipe = Pipeline([('rus', rus), ('pca', pca), ('lof', lof)]) |
| 1339 | + pipe.fit(X, y) |
| 1340 | + # Check the shapes |
| 1341 | + assert pipe.score_samples(X).shape == (X.shape[0],) |
| 1342 | + # Check the values |
| 1343 | + lof.fit(pca.fit_transform(X)) |
| 1344 | + assert_allclose(pipe.score_samples(X), lof.score_samples(pca.transform(X))) |
0 commit comments