gpt4 book ai didi

python - 在 Scikit-learn 中使用 Smote 和 Gridsearchcv

转载 作者:行者123 更新时间:2023-11-30 08:23:43 26 4
gpt4 key购买 nike

我正在处理不平衡的数据集,并且想要使用 scikit 的 gridsearchcv 进行网格搜索来调整模型的参数。为了对数据进行过采样,我想使用 SMOTE,并且我知道我可以将其作为管道的一个阶段并将其传递给 gridsearchcv。我担心的是,我认为 smote 将应用于训练和验证折叠,这不是你应该做的。验证集不应过采样。我是否正确,整个管道将应用于两个数据集拆分?如果是的话,我该如何扭转这个局面?提前非常感谢

最佳答案

是的,可以做到,但是用imblearn Pipeline .

你看,imblearn 有自己的 Pipeline 来正确处理采样器。我在 a similar question here 中描述了这一点.

当在 imblearn.Pipeline 对象上调用 predict() 时,它将跳过采样方法并将数据保留为要传递给下一个转换器的状态。您可以通过查看 source code here 来确认这一点。 :

        if hasattr(transform, "fit_sample"):
pass
else:
Xt = transform.transform(Xt)

因此,为了使其正常工作,您需要以下内容:

from imblearn.pipeline import Pipeline
model = Pipeline([
('sampling', SMOTE()),
('classification', LogisticRegression())
])

grid = GridSearchCV(model, params, ...)
grid.fit(X, y)

根据需要填写详细信息,管道将处理其余的事情。

关于python - 在 Scikit-learn 中使用 Smote 和 Gridsearchcv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50245684/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com