主代码中的分层折叠与学习曲线路径相同

2024-10-01 02:18:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个代码,其中在body命令中使用分层折叠进行交叉验证。 因为在训练和验证集上有一些关于分数命中的绘图会很好,所以我阅读了关于运行learning\u curve例程来绘图的内容。 然后出现了一件奇怪的事情(或者我希望我完全错了):在learning\u curve例程的选项中,我可以对一个分层的K-fold进行交叉验证(例如,在参数中指定cv),但是这将是一个与我在主代码中定义的不同的新的。 我找不到解决这个问题的方法:我的想法是把我在正文中使用的同一层叠的情节。你知道吗

-----在主代码中---

skf = StratifiedKFold(difficulty_list,10)

for train, test in skf:
    X_train, X_test, y_train, y_test = X[train],X[test], y[train], y[test]

然后我想为这个给定的折叠调用验证学习曲线,但是我只能

 plotValidationCurve(regressor,X_train,y_train,item)

它使用单独的方法绘制绘图:

 def plotValidationCurve(estimator,X,y,item):
 ml_method_list = ['AdaBoost','LASSO','Elastic Net','SVR','Random Forest','Gradient Boosting','RGF']
from sklearn.model_selection import learning_curve
from sklearn.model_selection import ShuffleSplit
fig = plt.figure()
plt.xlabel("Training examples")
plt.ylabel("Score")
train_sizes, train_scores, test_scores = learning_curve(estimator, X, y, train_sizes=np.linspace(0.05, 1.0, 20),cv=10)

注意,我在这里指定了一个10-分层k-交叉验证,但与我在skf中指定的不同。你知道吗

有没有办法做到这一点?你知道吗


Tags: 方法代码test绘图分层trainplt例程
1条回答
网友
1楼 · 发布于 2024-10-01 02:18:43

也许我解决了这个问题:把skf作为例程的一个参数来传递,以绘制学习曲线并设置参数cv=skf,怎么样?你知道吗

像这样的

plotValidationCurve(regressor,X_train,y_train,item,skf)

然后呢

def plotValidationCurve(estimator,X,y,item):
...
train_sizes, train_scores, test_scores = learning_curve(estimator, X, y, train_sizes=np.linspace(0.05, 1.0, 20),cv=skf)

相关问题 更多 >