def classification_report(y_true, y_pred, labels=None, sample_weight=None, digits=4, threshold=None):
# this function is copied from https://github.com/scikit-learn/scikit-learn/blob/412996f/sklearn/metrics/classification.py#L1341 (c) respective authors
# I pulled it here to fix formatting bug.
from sklearn.metrics import precision_recall_fscore_support, accuracy_score
y_true = np.array(y_true)
y_pred = np.array(y_pred)
if labels is None:
from sklearn.utils.multiclass import unique_labels
if threshold is not None:
y_true = y_true > threshold
y_pred = y_pred > threshold
labels = unique_labels(y_true, y_pred)
else:
labels = np.asarray(labels)
last_line_heading = 'avg / total'
target_names = ['%s' % l for l in labels]
results = [["", "precision", "recall", "f1-score", "support", "accuracy"]]
p, r, f1, s = precision_recall_fscore_support(y_true, y_pred,
labels=labels,
average=None,
sample_weight=sample_weight)
for i, label in enumerate(labels):
values = [target_names[i]]
for v in (p[i], r[i], f1[i]):
values += ["{0:0.{1}f}".format(v, digits)]
values += ["{0}".format(s[i])]
accuracy = accuracy_score(y_true == label, y_pred == label, sample_weight=sample_weight)
values += ["{0:0.{1}f}".format(accuracy, digits)]
results.append(values)
values = [last_line_heading]
for v in (np.average(p, weights=s),
np.average(r, weights=s),
np.average(f1, weights=s)):
values += ["{0:0.{1}f}".format(v, digits)]
values += ['{0}'.format(np.sum(s))]
accuracy = accuracy_score(y_true, y_pred, sample_weight=sample_weight)
values += ["{0:0.{1}f}".format(accuracy, digits)]
results.append(values)
return results
评论列表
文章目录