首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用imblearn在击打后执行随机欠采样

使用imblearn在击打后执行随机欠采样
EN

Stack Overflow用户
提问于 2022-04-08 10:41:38
回答 1查看 357关注 0票数 1

我正在尝试用RandomUnderSampler()SMOTE()来实现过采样和欠采样的结合.

我正在处理loan_status数据集。

我已经做了以下的分裂。

代码语言:javascript
复制
X = df.drop(['Loan_Status'],axis=1).values   # independant features
y = df['Loan_Status'].values# dependant variable

这就是我训练数据的分布情况。

这是我试图执行的用于类平衡的代码片段。

代码语言:javascript
复制
from imblearn.over_sampling import SMOTE
from imblearn.under_sampling import RandomUnderSampler
from imblearn.pipeline import make_pipeline
over = SMOTE(sampling_strategy=0.1)
under = RandomUnderSampler(sampling_strategy=0.5)
pipeline = make_pipeline(over,under)
    
x_sm,y_sm = pipeline.fit_resample(X_train,y_train)

它给了我一个具有以下追溯功能的ValueError:

代码语言:javascript
复制
ValueError                                Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_64588/3438707951.py in <module>
      4 pipeline = make_pipeline(over,under)
      5 
----> 6 x_copy,y_copy = pipeline.fit_resample(x_train_copy,y_train_copy)

~\Anaconda3\lib\site-packages\imblearn\pipeline.py in fit_resample(self, X, y, **fit_params)
    351             fit_params_last_step = fit_params_steps[self.steps[-1][0]]
    352             if hasattr(last_step, "fit_resample"):
--> 353                 return last_step.fit_resample(Xt, yt, **fit_params_last_step)
    354 
    355     @if_delegate_has_method(delegate="_final_estimator")

~\Anaconda3\lib\site-packages\imblearn\base.py in fit_resample(self, X, y)
     77         X, y, binarize_y = self._check_X_y(X, y)
     78 
---> 79         self.sampling_strategy_ = check_sampling_strategy(
     80             self.sampling_strategy, y, self._sampling_type
     81         )

~\Anaconda3\lib\site-packages\imblearn\utils\_validation.py in check_sampling_strategy(sampling_strategy, y, sampling_type, **kwargs)
    532         return OrderedDict(
    533             sorted(
--> 534                 _sampling_strategy_float(sampling_strategy, y, sampling_type).items()
    535             )
    536         )

~\Anaconda3\lib\site-packages\imblearn\utils\_validation.py in _sampling_strategy_float(sampling_strategy, y, sampling_type)
    391             ]
    392         ):
--> 393             raise ValueError(
    394                 "The specified ratio required to generate new "
    395                 "sample in the majority class while trying to "

ValueError: The specified ratio required to generate new sample in the majority class while trying to remove samples. Please increase the ratio.
EN

回答 1

Stack Overflow用户

发布于 2022-04-08 10:57:38

您必须增加SMOTE的抽样策略,因为((y_train==0).sum())/((y_train==1).sum())高于0.1。看来你的起始不平衡比率是(从眼睛上看) 0.4。尝试:

代码语言:javascript
复制
over = SMOTE(sampling_strategy=0.5)

最后,您可能需要一个相同的最终比率(在低采样之后),因此您应该将采样策略设置为1.0,用于RandomUnderSampler

代码语言:javascript
复制
under = RandomUnderSampler(sampling_strategy=1)

试着这样做,如果你有其他问题,给我一个反馈。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71795592

复制
相关文章

相似问题

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