‘Smote’ Object Has No Attribute ‘fit_sample’ [SOLVED]

‘Smote’ Object Has No Attribute ‘fit_sample’ error happens when fit sample is wrong. Replace fit_sample() use fit_resample() function.

In this article, we’ll look at the whole process with a given example. First, we’ll make the problem happen again, and then we’ll fix it. Aside from that, we will also talk about another important fact about smote.

How To Solve Attributeerror: ‘Smote’ Object Has No Attribute ‘fit_sample’?

attributeerror 'smote' object has no attribute 'fit_sample'
attributeerror ‘smote’ object has no attribute ‘fit_sample’

AttributeError: ‘SMOTE’ object has no attribute ‘fit_sample’ happens when you are using the wrong function. If you use this code to import SMOTE, you should use fit_resample() instead of fit sample.

Error Replication & Reason

Let’s replicate the same issue with some examples:

from sklearn import datasets
import numpy as np
from imblearn.over_sampling import SMOTE
data_frame = datasets.load_breast_cancer()
X = data_frame.data
y = data_frame.target
print(X.shape,y.shape)
oversample = SMOTE()
X, y = oversample.fit_sample(X, y)
print(X.shape,y.shape)

When we run the code above, we will get the same error (no attribute ‘fit_sample’). Here is a picture of the same thing.

AttributeError 'SMOTE' object has no attribute 'fit_sample'
AttributeError ‘SMOTE’ object has no attribute ‘fit_sample’

How To Fix?

It will work if we change fit_sample() to fit_resample(). Here is all of the code and what it does.

From:

oversample = SMOTE()
X, y = oversample.fit_sample(X, y)

To:

oversample = SMOTE()
X, y = oversample.fit_resample(X, y)

Complete Source Code

Here’s the complete source code:

from sklearn import datasets
import numpy as np
from imblearn.over_sampling import SMOTE
data_frame = datasets.load_breast_cancer()
X = data_frame.data
y = data_frame.target
print(X.shape,y.shape)
oversample = SMOTE()
X, y = oversample.fit_resample(X, y)
print(X.shape,y.shape)

Output

Solved AttributeError 'SMOTE' object has no attribute 'fit_sample'
Solved AttributeError ‘SMOTE’ object has no attribute ‘fit_sample’

Conclusion

Python has powerful high-level data structures and an easy-to-use but effective way to program with objects. The way Python’s commands are written is a big plus. Its clarity, ease of understanding, and ability to be typed in a variety of ways make it an ideal language for scripting and app development in a wide range of fields and on all platforms.

The approach described above is a simple way to fix the major error “AttributeError: ‘SMOTE’ object has no attribute ‘fit_sample’,” which was mentioned above.

Recommendations

By the way if you encounter an error about importing libraries, I have here the list of articles made to solve your problem on how to fix error in libraries.

Inquiries

However, If you have any questions or suggestions about this tutorial ‘Smote’ Object Has No Attribute ‘fit_sample’, Please feel to comment below, Thank You and God Bless!

Leave a Comment