gpt4 book ai didi

python - 一起使用过采样和 cross_validation 函数的方法是什么?

转载 作者:行者123 更新时间:2023-11-30 09:15:25 25 4
gpt4 key购买 nike

我正在尝试在分类问题中同时使用 cross_validate 函数和 SMOTE 函数,我想知道如何正确执行此操作。

这是我用来在机器学习分类算法中调用 cross_validation 的简单函数:

def bayes(dataIn, dataOut, cv, statistic):    
# trainning method
naive_bayes = GaussianNB()

# applying the method
outputBayes = cross_validate(estimator = naive_bayes,
X = dataIn, y = dataOut,
cv = cv, scoring = statistic)

return outputBayes

我访问了cross_validate documentation搜索是否可以在调用 cross_validate 函数之前确定训练数据集和测试数据集,并且不发送完整的数据输入和数据输出。因为我想使用 SMOTE 函数,为此,我需要在交叉验证之前分离数据集。如果我在跨数据集中使用 SMOTE,结果将会出现偏差。

如何解决?我应该执行交叉验证功能吗?我不想这样做,因为 cross_validate 函数返回非常好用,但我不知道如何执行完全相同的返回。

我看到了其他相关问题,但没有找到该具体问题:

SMOTE oversampling and cross-validation

Function for cross validation and oversampling (SMOTE)

Does oversampling happen before or after cross-validation using imblearn pipelines?

最佳答案

第三个链接实际上描述了您想要的内容。鉴于 this article 的结果,应在交叉验证过程中的每次折叠上进行过采样。此过程是在使用 IMBLearn 包和管道时完成的。该过程将使用该包,并仅指定您的过采样技术 (SMOTE) 和模型 (GaussianNB())。快速改编 third link 中的代码大致显示您想要的内容。

from imblearn.pipeline import Pipeline
model = Pipeline([
('sampling', SMOTE()), # this is the oversampling process
('classification', GaussianNB()) . # this is where to specify the model
])


param_dist = {...[REVIEW DOCUMENTATION FOR CORRECT SET OF PARAMS]
}

random_search = RandomizedSearchCV(model,
param_dist,
cv=StratifiedKFold(n_splits=5),
n_iter=10,
scoring=scorer_cv_cost_savings)
random_search.fit(X_train.values, y_train)

关于python - 一起使用过采样和 cross_validation 函数的方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57317739/

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