Skip to content

Commit 1d2d73c

Browse files
committed
fix
1 parent b97d6af commit 1d2d73c

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

imblearn/pipeline.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ def score_samples(self, X):
622622
"""
623623
Xt = X
624624
for _, _, transformer in self._iter(with_final=False):
625-
if hasattr(transform, "fit_resample"):
625+
if hasattr(transformer, "fit_resample"):
626626
pass
627627
else:
628628
Xt = transformer.transform(Xt)

imblearn/tests/test_pipeline.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@
3333
from sklearn.preprocessing import StandardScaler
3434
from sklearn.pipeline import FeatureUnion
3535

36+
from imblearn.datasets import make_imbalance
3637
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
4140

4241

4342
JUNK_FOOD_DOCS = (
@@ -1324,3 +1323,22 @@ def test_verbose(est, method, pattern, capsys):
13241323
est.set_params(verbose=True)
13251324
func(X, y)
13261325
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

Comments
 (0)