python - 重新抽样查询

标签 python scikit-learn resampling

我在这里使用 sklearn.utils resample 有一点困惑。

使用不平衡的数据集,我尝试对多数类进行随机下采样。一切都很顺利,但是当我刚刚意识到使用 (replace= True) 的模型的性能确实显着提高时。

性能提高背后的原因是什么?样本是否被复制样本替换?这里是否存在过拟合的情况?

我对此有点陌生,因此,非常感谢这里的任何澄清。

这是代码中进行全部重大更改的部分。


from sklearn.utils import resample

Negative = sms[sms.Label == 'Neg']
Positive = sms[sms.Label == 'Pos']


# Upsample minority class
df_Negative_res = resample(Negative , replace=True,  # sample with replacement
                        n_samples=243 ,  # to match majority class
                        random_state=seed )  # reproducible results

df_Positive_res = resample(Positive , replace=True,  # sample with replacement
                        n_samples=243 ,  # to match majority class
                        random_state=seed)  # reproducible results

# Combine majority class with upsampled minority class
sms = pd.concat([df_Negative_res, df_Positive_res])

print(sms)

最佳答案

通过进行过采样/欠采样,您实际上可以改变不平衡数据集的分布,并尝试提高模型的性能。所以,它实际上可以被认为是一种增强方法。已经有关于替换重采样的讨论,所以我建议您看一下: https://stats.stackexchange.com/questions/171440/bootstrap-methodology-why-resample-with-replacement-instead-of-random-subsamp 为了查明您的模型是否过度拟合,您需要一个大型测试集(没有任何过采样等),并且该测试集必须与您的训练集不同。如果您的模型表现良好,则过度拟合的可能性就会降低。但是,当进行过采样/欠采样时,您仍然会影响数据的偏差。 因此,关键是要找出你的模型是否可以很好地概括未见过的样本。在训练集上它应该表现得非常好(在一般情况下)。 在进行交叉验证时,还必须考虑一些非常重要的事情 - 您是在每个折叠内还是在整个数据集上应用采样?这是关于该主题的讨论https://www.researchgate.net/post/should_oversampling_be_done_before_or_within_cross-validation 希望我的回答对您有帮助!

关于python - 重新抽样查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59333630/

相关文章:

python - SciPy 和 App Engine

python - 如何解析一个简单的csv文件?

python - Windows 上的 os.rename 引发 WindowsError 而不是 OSError

python - 我在随机森林分类器中收到“未拟合”错误?

python - pandas 使用用户定义的时间间隔重新采样

python - 如何在后台启动和运行外部脚本?

scikit-learn - cross_val_score 不支持hamming_loss?

python - 我们如何在列的每个值中对 pandas 数据框进行二次采样

c++ - 什么是大点云的高速子采样库?

python - pandas 每日平均值,pandas.resample