gpt4 book ai didi

Python + Scikit-learn :How to plot the curves of training score and validation score against the additive smoothing parameter alpha

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

我正在使用 k 折交叉验证来计算加法平滑参数 alpha 的最佳值。另外,我想根据 alpha 值绘制训练准确度和验证准确度的曲线。我为此编写了代码:

alphas = list(np.arange(0.0001, 1.5000, 0.0001))

#empty lists that stores cv scores and training_scores
cv_scores = []
training_scores = []

#perform k fold cross validation
for alpha in alphas:
naive_bayes = MultinomialNB(alpha=alpha)
scores = cross_val_score(naive_bayes, x_train_counts, y_train, cv=20, scoring='accuracy')
scores_training = naive_bayes.fit(x_train_counts, y_train).score(x_train_counts, y_train)

cv_scores.append(scores.mean())
training_scores.append(scores_training)

#plot cross-validated score, training score vs alpha

plt.plot(alphas, cv_scores, 'r')
plt.plot(alphas, training_scores, 'b')
plt.xlabel('alpha')
plt.ylabel('score')

这是实现此目的的正确方法吗?

最佳答案

根据您是否想要调整其他模型超参数,使用所谓的 grid search 可能会更容易。 。使用它,您可以以更简单的方式调整额外的超参数,并且可以为您提供训练分数。请参阅我的下面的实现。

parameters = {'alpha':[0.0001, 1.5000, 0.0001]}
classifier = GridSearchCV(MultinomialNB(), parameters, cv=20)
clf.fit(x_train, y_train)

print('Mean train set score: {}'.format(clf.cv_results_['mean_train_score']))

关于Python + Scikit-learn :How to plot the curves of training score and validation score against the additive smoothing parameter alpha,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52724468/

28 4 0