gpt4 book ai didi

python - 获取 RandomizedSearchCV 最佳模型的概率

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

我使用 RandomizedSearchCV 通过 10 倍交叉验证和 100 次迭代来获得最佳参数。这效果很好。但现在我还想从性能最佳的模型中获取每个预测测试数据点的概率(例如 predict_proba)。

如何做到这一点?

我看到两个选项。首先,也许可以直接从 RandomizedSearchCV 中获取这些概率,或者第二,从 RandomizedSearchCV 中获取最佳参数,然后再次进行 10 倍交叉验证(使用相同的种子,以便我得到相同的分割)和最佳参数。

编辑:以下代码对于获取最佳性能模型的概率是否正确? X 是训练数据,y 是标签,模型是我的 RandomizedSearchCV,其中包含一个带有输入缺失值、标准化和 SVM 的 Pipeline

cv_outer = StratifiedKFold(n_splits=10, shuffle=True, random_state=0)
y_prob = np.empty([y.size, nrClasses]) * np.nan
best_model = model.fit(X, y).best_estimator_

for train, test in cv_outer.split(X, y):
probas_ = best_model.fit(X[train], y[train]).predict_proba(X[test])
y_prob[test] = probas_

最佳答案

如果我理解正确的话,您希望获得测试中每个样本的单独分数,以获取 CV 分数最高的案例。如果是这种情况,您必须使用可以控制分割索引的 CV 生成器之一,如下所示: http://scikit-learn.org/stable/tutorial/statistical_inference/model_selection.html#cross-validation-generators

如果您想使用性能最佳的模型计算新测试样本的分数,考虑到您的基础模型支持,RandomizedSearchCVpredict_proba() 函数就足够了它。

示例:

import numpy
skf = StratifiedKFold(n_splits=10, random_state=0, shuffle=True)
scores = cross_val_score(svc, X, y, cv=skf, n_jobs=-1)
max_score_split = numpy.argmax(scores)

现在您知道您的最佳模型出现在 max_score_split 处,您可以自己进行分割并用它来拟合您的模型。

train_indices, test_indices = k_fold.split(X)[max_score_split]
X_train = X[train_indices]
y_train = y[train_indices]
X_test = X[test_indices]
y_test = y[test_indices]
model.fit(X_train, y_train) # this is your model object that should have been created before

最后通过以下方式获得您的预测:

model.predict_proba(X_test)

我自己还没有测试过代码,但应该进行一些小的修改。

关于python - 获取 RandomizedSearchCV 最佳模型的概率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50214310/

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