首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >imblearn.oversampling SMOTENC ValueError

imblearn.oversampling SMOTENC ValueError
EN

Stack Overflow用户
提问于 2020-03-05 11:41:03
回答 2查看 1.1K关注 0票数 0

这是我第一次使用SMOTENC对分类数据进行上采样。然而,我已经得到了错误。你能建议一下我应该把什么当作是绝对的吗?_SMOTENC中的功能?

代码语言:javascript
复制
from imblearn.over_sampling import SMOTENC

x=df.drop("A",1)
y=df["A"]

smote_nc = SMOTENC(categorical_features=['A','B','C','D','E','F','G','H'], random_state=0)
X_resampled, y_resampled = smote_nc.fit_resample(x, y)

错误:

代码语言:javascript
复制
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
 in 

---> 14 X_resampled, y_resampled = smote_nc.fit_resample(x, y)
     15 #sm = SMOTE(random_state=100)
     16 #ros = RandomOverSampler()

~/.local/lib/python3.5/site-packages/imblearn/base.py in fit_resample(self, X, y)
     82             self.sampling_strategy, y, self._sampling_type)
     83 
---> 84         output = self._fit_resample(X, y)
     85 
     86         if binarize_y:

~/.local/lib/python3.5/site-packages/imblearn/over_sampling/_smote.py in _fit_resample(self, X, y)
    986     def _fit_resample(self, X, y):
    987         self.n_features_ = X.shape[1]
--> 988         self._validate_estimator()
    989 
    990         # compute the median of the standard deviation of the minority class

~/.local/lib/python3.5/site-packages/imblearn/over_sampling/_smote.py in _validate_estimator(self)
    979                 raise ValueError(
    980                     'Some of the categorical indices are out of range. Indices'
--> 981                     ' should be between 0 and {}'.format(self.n_features_))
    982             self.categorical_features_ = categorical_features
    983         self.continuous_features_ = np.setdiff1d(np.arange(self.n_features_),

ValueError: Some of the categorical indices are out of range. Indices should be between 0 and 7
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-05-07 19:20:28

根据文档:

代码语言:javascript
复制
categorical_features : ndarray, shape (n_cat_features,) or (n_features,)
Specified which features are categorical. Can either be:

- array of indices specifying the categorical features;
- mask array of shape (n_features, ) and ``bool`` dtype for which
  ``True`` indicates the categorical features.

所以,只需替换这一行

代码语言:javascript
复制
smote_nc = SMOTENC(categorical_features=['A','B','C','D','E','F','G','H'], random_state=0)

用这条线

代码语言:javascript
复制
smote_nc = SMOTENC(categorical_features=[df.dtypes==object], random_state=0)
票数 1
EN

Stack Overflow用户

发布于 2021-02-25 03:40:26

试试这个:

代码语言:javascript
复制
categorical_feature_mask = df.dtypes == object
smt=SMOTENC(categorical_features=categorical_feature_mask,random_state=99)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60537888

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档