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

The AttributeError: ‘SMOTE’ object has no attribute ‘fit_sample’ error occurs when we incorrectly use the fit_sample function. The error can be easily solved by replacing the fit_sample function with the fit_resample function. In this short article, we will discuss the reasons for getting AttributeError: ‘SMOTE’ object has no attribute ‘fit_sample’ error and will go through various possible solutions as well. Moreover, we will also cover how to interpret errors in Python and solve them.

AttributeError: ‘SMOTE’ object has no attribute ‘fit_sample’ – Possible Solutions

The error clearly says the the SMOTE object does not have the fit_sample which means we are trying to use the fit_sample on the SMOTE object when the object does not have any attribute. The error can be solved by replacing the fit_sample with the fit_resample method.

Let us first take a look and take an example to see why we are getting AttributeError: ‘SMOTE’ object has no attribute ‘fit_sample’ error:

# importing the modules
from sklearn import datasets
import numpy as np
from imblearn.over_sampling import SMOTE

# loading the breast dataset
data_frame = datasets.load_breast_cancer()
X = data_frame.data
y = data_frame.target

# initializing the SMOTE method
oversample = SMOTE()

# using fit_sample method
X, y = oversample.fit_sample(X, y)

Output:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/tmp/ipykernel_145595/4127142106.py in <module>
     13 
     14 # using fit_sample method
---> 15 X, y = oversample.fit_sample(X, y)

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

Here is the code where we got the error:

Attributeerror: ‘Smote’ Object Has No Attribute ‘fit_sample’

As you can see, we got the error that clearly says that the SMOTE object does not have a fit_sample attribute.

Let us now go through some of the possible solutions.

Solution-1: Using the resample method

As we have noticed that the SMOTE object does not have an attribute known as fit_sample. We can use the fit_resample method instead in order to get rid of the error:

# importing the modules
from sklearn import datasets
import numpy as np
from imblearn.over_sampling import SMOTE

# loading the breast dataset
data_frame = datasets.load_breast_cancer()
X = data_frame.data
y = data_frame.target

# initializing the SMOTE method
oversample = SMOTE()

# using fit_sample method
X, y = oversample.fit_resample(X, y)

When you run the code, you will not get the error because we have replaced the fit_sample with the fit_resample method.

What is SMOTE in Machine Learning?

SMOTE stands for Synthetic Minority Oversampley Techniques. It is a statistical technique that is used to increase the number of samples when you have an unbalanced dataset. This technique generates new data points from the existing minority cases from your dataset.

Let us take an example and see how it helps us to balance the dataset. Let us first create a classification dataset with unbalanced output categories.

# importing the module
from sklearn.datasets import make_classification

# creating inbalanced dataset
X, y = make_classification(n_samples=1000, n_features=2, n_redundant=0,
    n_clusters_per_class=1, weights=[0.99], flip_y=0, random_state=1)

Once we created the dataset, we can visualize it in order to see how the output categories look like:

# importing the modules
from collections import Counter
from matplotlib import pyplot
from numpy import where

# summarize class distribution
counter = Counter(y)

# scatter plot of examples by class label
for label, _ in counter.items():
 row_ix = where(y == label)[0]
 pyplot.scatter(X[row_ix, 0], X[row_ix, 1], label=str(label))
pyplot.legend()
pyplot.show()

Output:

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

As you can see, there are very few elements from the second category which means the dataset is highly imbalanced. In order to make the data balanced, we will apply the SMOTE method in order to increase the minority class to make the dataset balanced.

from imblearn.over_sampling import SMOTE

# transform the dataset
oversample = SMOTE()
X, y = oversample.fit_resample(X, y)
# summarize the new class distribution
counter = Counter(y)
# scatter plot of examples by class label
for label, _ in counter.items():
	row_ix = where(y == label)[0]
	pyplot.scatter(X[row_ix, 0], X[row_ix, 1], label=str(label))
pyplot.legend()
pyplot.show()

Output:

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

As you can see, by using the SMOTE method, we were able to increase the number of minority class samples in our dataset.

Summary

We know that the error occurs when we used the fit_sample function instead of using the fit_resample method in SMOTE. In this short article, we discussed the reasons for getting AttributeError: ‘SMOTE’ object has no attribute ‘fit_sample’ error and learned how to solve the problem.

Related Articles:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top