【发布时间】:2018-09-19 21:45:18
【问题描述】:
我有一个小的、不平衡的数据集,我想用不同的算法对其进行测试。出于评估目的,我需要多个性能指标(准确度、精确度、召回率、fscore、支持)。
我打算这样做,但我并不满意,因为可能有更简单的解决方案:
skf = StratifiedKFold(n_splits=3, random_state=42, shuffle=True)
accuracy = []
for train_index, test_index in skf.split(X,Y):
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = Y[train_index], Y[test_index]
gradientBoost.fit(X_train, y_train)
y_pred = gradientBoost.predict(X_test)
accuracy.append(round(accuracy_score(y_test, y_pred), 2))
precision, recall, fscore, support = np.round(score(y_test, y_pred), 2)
print('precision: ' + str(precision))
print('recall: ' + str(recall))
print('fscore: ' + str(fscore))
print('support: ' + str(support))
print(classification_report(y_test, y_pred))
meanAcc= np.mean(np.asarray(accuracy))
print('meanAcc: ', meanAcc)
理论上,我可以对所有指标进行平均,就像我为准确性所做的那样。有没有更简单和/或更有效的方法?
编辑:
我尝试将准确率和召回加权作为得分手。不幸的是,情节中只显示了准确性。在图例中提到了准确率+召回率。
#Initialize classifier
clf_gini = DecisionTreeClassifier(criterion = "gini", random_state = 42,
max_depth=10, min_samples_leaf=8)
scoring = {'Accuracy' : make_scorer(accuracy_score), 'Recall' : 'recall_weighted'}
gs = GridSearchCV(DecisionTreeClassifier(criterion= 'entropy', random_state=42, min_samples_leaf = 10), param_grid={'max_depth' : range(2, 30, 2)},
scoring=scoring, cv=3, refit='Accuracy')
gs.fit(X_Distances, Y)
results = gs.cv_results_
plt.figure(figsize=(13, 13))
plt.title("GridSearchCV evaluating using multiple scorers simultaneously",
fontsize=16)
plt.xlabel("max_depth")
plt.ylabel("Score")
plt.grid()
ax = plt.axes()
ax.set_xlim(0, 32)
ax.set_ylim(0, 1)
# Get the regular numpy array from the MaskedArray
X_axis = np.array(results['param_max_depth'].data, dtype=float)
for scorer, color in zip(sorted(scoring), ['g', 'k']):
for sample, style in (('train', '--'), ('test', '-')):
sample_score_mean = results['mean_%s_%s' % (sample, scorer)]
sample_score_std = results['std_%s_%s' % (sample, scorer)]
ax.fill_between(X_axis, sample_score_mean - sample_score_std,
sample_score_mean + sample_score_std,
alpha=0.1 if sample == 'test' else 0, color=color)
ax.plot(X_axis, sample_score_mean, style, color=color,
alpha=1 if sample == 'test' else 0.7,
label="%s (%s)" % (scorer, sample))
best_index = np.nonzero(results['rank_test_%s' % scorer] == 1)[0][0]
best_score = results['mean_test_%s' % scorer][best_index]
# Plot a dotted vertical line at the best score for that scorer marked by x
ax.plot([X_axis[best_index], ] * 2, [0, best_score],
linestyle='-.', color=color, marker='x', markeredgewidth=3, ms=8)
# Annotate the best score for that scorer
ax.annotate("%0.2f" % best_score,
(X_axis[best_index], best_score + 0.005))
plt.legend(loc="best")
plt.grid('off')
plt.show()
【问题讨论】:
-
为什么不附加到列表或字典本身?我不确定你还能做什么。
-
附加到列表是什么意思?对于我目前附加到列表的准确性,我可以对所有指标都这样做,是的。
-
是的,将它们附加到列表中,就像您正在做的那样以确保准确性。
标签: python machine-learning classification cross-validation