AttributeError: 'SMOTE' object has no attribute 'fit_sample'
Asked Answered
A

3

33

Why I am getting the error

AttributeError: 'SMOTE' object has no attribute 'fit_sample'

I don't think this code should cause any error?

from imblearn.over_sampling import SMOTE
smt = SMOTE(random_state=0)
X_train_SMOTE, y_train_SMOTE = smt.fit_sample(X_train, y_train)
Affair answered 25/2, 2021 at 7:51 Comment(1)
It's called fit_resample.Anaximander
O
50

If you import like this

from imblearn.over_sampling import SMOTE

you need to do fit_resample()

oversample = SMOTE()
X, y = oversample.fit_resample(X, y)
Obstinate answered 25/2, 2021 at 7:56 Comment(3)
Hi Thank you for responding, but may I ask is fit_sample() and fit_resample() is the same? Because I am actually following some tutorial when I face this error and they write fit_sample(). Many ThanksAffair
Probably this tutorial: towardsdatascience.com/… - Landed here because their code has this error.Parasynapsis
Precisely, lol, this is exactly why I got here too.Brooch
T
1

Another way

from imblearn.combine import SMOTEENN

Sm=SMOTEENN()
X, y=Sm.fit_resample(X, y)
Tippets answered 30/9, 2023 at 11:1 Comment(0)
A
0

It used to be fit_sample but was renamed fit_resample with an alias for backward compatibility in imblearn 0.4 (this was documented). Then the alias was removed in version 0.8 (for some reason it was not documented). In short, SMOTE().fit_sample(X_train, y_train) used to work but not anymore.

Now only SMOTE().fit_resample(X_train, y_train) works.

Also, all imblearn objects have a fit() method defined as well but it's completely useless because everything it does is already done by fit_resample() anyway (the documentation even urges you to use fit_resample() over fit()).

Airlike answered 13/5, 2023 at 7:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.