This is my first time using SMOTENC to upsampling my categorical data. However, I’ve been getting error. Can you please advice what should I pass for categorical_features in SMOTENC?
JavaScript
x
8
1
from imblearn.over_sampling import SMOTENC
2
3
x=df.drop("A",1)
4
y=df["A"]
5
6
smote_nc = SMOTENC(categorical_features=['A','B','C','D','E','F','G','H'], random_state=0)
7
X_resampled, y_resampled = smote_nc.fit_resample(x, y)
8
ERROR:
JavaScript
1
31
31
1
---------------------------------------------------------------------------
2
ValueError Traceback (most recent call last)
3
<ipython-input-26-f6c9d8a17967> in <module>
4
5
---> 14 X_resampled, y_resampled = smote_nc.fit_resample(x, y)
6
15 #sm = SMOTE(random_state=100)
7
16 #ros = RandomOverSampler()
8
9
~/.local/lib/python3.5/site-packages/imblearn/base.py in fit_resample(self, X, y)
10
82 self.sampling_strategy, y, self._sampling_type)
11
83
12
---> 84 output = self._fit_resample(X, y)
13
85
14
86 if binarize_y:
15
16
~/.local/lib/python3.5/site-packages/imblearn/over_sampling/_smote.py in _fit_resample(self, X, y)
17
986 def _fit_resample(self, X, y):
18
987 self.n_features_ = X.shape[1]
19
--> 988 self._validate_estimator()
20
989
21
990 # compute the median of the standard deviation of the minority class
22
23
~/.local/lib/python3.5/site-packages/imblearn/over_sampling/_smote.py in _validate_estimator(self)
24
979 raise ValueError(
25
980 'Some of the categorical indices are out of range. Indices'
26
--> 981 ' should be between 0 and {}'.format(self.n_features_))
27
982 self.categorical_features_ = categorical_features
28
983 self.continuous_features_ = np.setdiff1d(np.arange(self.n_features_),
29
30
ValueError: Some of the categorical indices are out of range. Indices should be between 0 and 7
31
Advertisement
Answer
As per documentation:
JavaScript
1
7
1
categorical_features : ndarray, shape (n_cat_features,) or (n_features,)
2
Specified which features are categorical. Can either be:
3
4
- array of indices specifying the categorical features;
5
- mask array of shape (n_features, ) and ``bool`` dtype for which
6
``True`` indicates the categorical features.
7
So, just replace the line
JavaScript
1
2
1
smote_nc = SMOTENC(categorical_features=['A','B','C','D','E','F','G','H'], random_state=0)
2
with the line
JavaScript
1
2
1
smote_nc = SMOTENC(categorical_features=[df.dtypes==object], random_state=0)
2