为什么机器学习模型图的结果会彼此叠加?

时间:2019-06-05 13:59:00

标签: matplotlib machine-learning keras scikit-learn

我正在用遗传数据比较多类分类问题中的一些模型(逻辑回归,SVM,随机森林,梯度提升,多层感知器和Keras)。我的代码运行良好,但是如果我运行两次,则第二次运行的图形输出如下所示:

graph

似乎保留了我第一次运行的结果并添加了第二次运行的结果,这是由于我在下面的代码中添加了内容吗?

这是我建立模型后的代码:

inner_cv = KFold(n_splits=10, shuffle=True, random_state=seed)
outer_cv = KFold(n_splits=10, shuffle=True, random_state=seed)

models = []
models.append(('LR', dcv.GridSearchCV(logreg, LR_par, cv=inner_cv, iid=False, n_jobs=-1)))
models.append(('SVM', dcv.GridSearchCV(svm, tuned_parameters, cv=inner_cv, iid=False, n_jobs=-1)))
models.append(('RFC', dcv.GridSearchCV(rfc, param_grid, cv=inner_cv,iid=False, n_jobs=-1)))
models.append(('GBM', dcv.GridSearchCV(gbm, param, cv=inner_cv,iid=False, n_jobs=-1)))
models.append(('MLP', dcv.GridSearchCV(mlp, parameter_space, cv=inner_cv,iid=False, n_jobs=-1)))
models.append(('Keras', GridSearchCV(estimator=keras, param_grid=kerasparams, cv=inner_cv,iid=False, n_jobs=-1)))


results = []
names = []
scoring = 'accuracy'


for name, model in models:
    nested_cv_results = model_selection.cross_val_score(model, X_res, y_res, cv=outer_cv, scoring=scoring)
    results.append(nested_cv_results)
    names.append(name)
    msg = "Nested CV Accuracy %s: %f (+/- %f )" % (name, nested_cv_results.mean()*100, nested_cv_results.std()*100)
    print(msg)
    model.fit(X_train, Y_train)
    print('Test set accuracy: {:.2f}'.format(model.score(X_test, Y_test)*100),  '%')
    print("Best Parameters: \n{}\n".format(model.best_params_))
    print("Best CV Score: \n{}\n".format(model.best_score_)) #average of all cv folds for a single combination of the parameters you specify 

fig = plt.figure()
fig.suptitle('Nested Cross-Validation Comparison')
ax = fig.add_subplot(111)
plt.boxplot(results)
ax.set_xticklabels(names)
plt.show()

我使用anaconda 2019.03和jupyter lab 0.35.4和python 3.7 任何帮助将不胜感激。

0 个答案:

没有答案