随机过采样与随机欠采样 代码

Posted 卖山楂啦prss

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了随机过采样与随机欠采样 代码相关的知识,希望对你有一定的参考价值。

一般代码:

上采样

不调包

data['lable'].value_counts()
number_all = len(data[data.lable == 0])
pos_indices = np.array(data[data.lable == 1].index)
all_indices = data[data.lable == 0].index
random_pos_indices = np.random.choice(pos_indices, number_all, replace = True)
random_pos_indices = np.array(random_pos_indices)
up_sample_indices = np.concatenate([all_indices,random_pos_indices])
up_sample_data = data.iloc[up_sample_indices,:]
up_sample_data['lable'].value_counts()
X_up_sample = up_sample_data.loc[:, up_sample_data.columns != 'lable'][serial_feature+binary_discrete_feature+multi_discrete_feature]
y_up_sample = up_sample_data.loc[:, up_sample_data.columns == 'lable']
X_train, X_test, y_train, y_test = train_test_split(X_up_sample,y_up_sample,test_size = 0.3, random_state = 0)

下采样

不调包

data['lable'].value_counts()
number_pos = len(data[data.lable == 1])
pos_indices = np.array(data[data.lable == 1].index)
all_indices = data[data.lable == 0].index
random_all_indices = np.random.choice(all_indices, number_pos, replace = True)
random_all_indices = np.array(random_all_indices)
under_sample_indices = np.concatenate([pos_indices,random_all_indices])
under_sample_data = data.iloc[under_sample_indices,:]#下采样数据集
under_sample_data['lable'].value_counts()
X_under_sample = under_sample_data.loc[:, under_sample_data.columns != 'lable'][serial_feature+binary_discrete_feature+multi_discrete_feature]#下采样数据集的数据
y_under_sample = under_sample_data.loc[:, under_sample_data.columns == 'lable']#下采样数据集的label
X_train, X_test, y_train, y_test = train_test_split(X_under_sample,y_under_sample,test_size = 0.3, random_state = 0)

参考:手把手教你使用Python实战反欺诈模型

为什么数据处理的几种采样方法都只对训练集进行操作?

答:因为原始数据集的 0-1 比为 1:99,所以随即拆分成的训练集和测试集的 0-1 比也差不多是> 1:99,又因为我们用训练集来训练模型,如果不对训练集的数据做任何操作,得出来模型就会在预测分类0的准度上比1高,而我们希望的是两者都要兼顾,所以我们才要使用欠采样或者过采样对训练集进行处理,使训练集的> 0-1 比在我们之前聊到的 1:1 ~ 1:10这个比较合适的区间,用这样的训练集训练出来的模型的泛化能力会更强。以打靶作为比喻,靶心面积很小,对应了占比小的违约客户群体。在 0-1 比为1:99 的测试集的严酷考验下,模型打中靶心(成功预测违约客户)与打中靶心周围(成功预测履约客户)的概率都得到了保证。。

https://zhuanlan.zhihu.com/p/164590124


所以,可以先进行数据划分,再对训练集进行采样处理,而测试集是不做任何处理的,保留严峻的比例考验来测试模型。

上采样

from imblearn.over_sampling import RandomOverSampler
# 采样策略 sampling_strategy = 'auto' 的 auto 默认抽成 1:1,
ros = RandomOverSampler(random_state=0, sampling_strategy='auto') 
X_ros, y_ros = ros.fit_sample(X_train, y_train)
print('随机过采样后,训练集 y_ros 中的分类情况:{}'.format(Counter(y_ros)))

下采样

from imblearn.under_sampling import RandomUnderSampler
under = RandomUnderSampler(random_state=0, sampling_strategy='auto') 
X_rus, y_rus = under.fit_resample(X_train, y_train)
print('随机欠采样后,训练集 y_rus 中的分类情况:{}'.format(Counter(y_rus)))

补充:

引用:

过采样会随机复制少数样例以增大它们的规模。欠采样则随机地少采样主要的类。一些数据科学家(天真地)认为过采样更好,因为其会得到更多的数据,而欠采样会将数据丢掉。但请记住复制数据不是没有后果的——因为其会得到复制出来的数据,它就会使变量的方差表面上比实际上更小。而过采样的好处是它也会复制误差的数量:如果一个分类器在原始的少数类数据集上做出了一个错误的负面错误,那么将该数据集复制五次之后,该分类器就会在新的数据集上出现六个错误。相对地,欠采样会让独立变量(independent variable)的方差看起来比其实际的方差更高。

https://zhuanlan.zhihu.com/p/164590124

以上是关于随机过采样与随机欠采样 代码的主要内容,如果未能解决你的问题,请参考以下文章

ScikitLearn 随机森林中的欠采样与 class_weight

不平衡数据:欠采样还是过采样?

[1] 样本不均衡问题及其解决办法

逻辑回归分类中解决类别不平衡问题

如何解决样本不均衡问题

训练测试拆分后不平衡数据的欠采样