32

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)
cottontail
  • 10,268
  • 18
  • 50
  • 51

2 Answers2

47

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)
Subbu VidyaSekar
  • 2,503
  • 3
  • 21
  • 39
  • 1
    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 Thanks –  Feb 25 '21 at 08:30
  • 1
    Probably this tutorial: https://towardsdatascience.com/building-a-logistic-regression-in-python-step-by-step-becd4d56c9c8 - Landed here because their code has this error. – Béatrice Moissinac Jun 03 '22 at 14:31
  • Precisely, lol, this is exactly why I got here too. – Tadashi Mori Jul 18 '22 at 15:01
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()).

cottontail
  • 10,268
  • 18
  • 50
  • 51