本文整理汇总了Python中sklearn.metrics.confusion_matrix方法的典型用法代码示例。如果您正苦于以下问题:Python metrics.confusion_matrix方法的具体用法?Python metrics.confusion_matrix怎么用?Python metrics.confusion_matrix使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.metrics
的用法示例。
在下文中一共展示了metrics.confusion_matrix方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: plot_confusion_matrix
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import confusion_matrix [as 别名]
def plot_confusion_matrix(y_true, y_pred, size=None, normalize=False):
"""plot_confusion_matrix."""
cm = confusion_matrix(y_true, y_pred)
fmt = "%d"
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
fmt = "%.2f"
xticklabels = list(sorted(set(y_pred)))
yticklabels = list(sorted(set(y_true)))
if size is not None:
plt.figure(figsize=(size, size))
heatmap(cm, xlabel='Predicted label', ylabel='True label',
xticklabels=xticklabels, yticklabels=yticklabels,
cmap=plt.cm.Blues, fmt=fmt)
if normalize:
plt.title("Confusion matrix (norm.)")
else:
plt.title("Confusion matrix")
plt.gca().invert_yaxis()
示例2: class_accuracy
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import confusion_matrix [as 别名]
def class_accuracy(prediction, label):
cf = confusion_matrix(prediction, label)
cls_cnt = cf.sum(axis=1)
cls_hit = np.diag(cf)
cls_acc = cls_hit / cls_cnt.astype(float)
mean_cls_acc = cls_acc.mean()
return cls_acc, mean_cls_acc
示例3: train_and_evaluate
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import confusion_matrix [as 别名]
def train_and_evaluate(clf, X_train, X_test, y_train, y_test):
clf.fit(X_train, y_train)
print ("Accuracy on training set:")
print (clf.score(X_train, y_train))
print ("Accuracy on testing set:")
print (clf.score(X_test, y_test))
y_pred = clf.predict(X_test)
print ("Classification Report:")
print (metrics.classification_report(y_test, y_pred))
print ("Confusion Matrix:")
print (metrics.confusion_matrix(y_test, y_pred))
# ===============================================================================
# from FaceDetectPredict.py
# ===============================================================================
开发者ID:its-izhar,项目名称:Emotion-Recognition-Using-SVMs,代码行数:18,代码来源:Train Classifier and Test Video Feed.py
示例4: draw_confusion_matrix
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import confusion_matrix [as 别名]
def draw_confusion_matrix(dataset, model, set_trial=None, filename="test_results.sdf"):
path = find_average_trial(dataset, model, metric="test_pr") if set_trial is None \
else "../result/{}/{}/{}/".format(model, dataset, set_trial)
# Load true, pred value
true_y, pred_y = [], []
mols = Chem.SDMolSupplier(path + filename)
for mol in mols:
true_y.append(float(mol.GetProp("true")))
pred_y.append(float(mol.GetProp("pred")))
true_y = np.array(true_y, dtype=float)
pred_y = np.array(pred_y, dtype=float).round()
# Get precision and recall
confusion = confusion_matrix(true_y, pred_y)
tn, fp, fn, tp = confusion.ravel()
print("tn: {}, fp: {}, fn: {}, tp: {}".format(tn, fp, fn, tp))
示例5: classification_scores
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import confusion_matrix [as 别名]
def classification_scores(gts, preds, labels):
accuracy = metrics.accuracy_score(gts, preds)
class_accuracies = []
for lab in labels: # TODO Fix
class_accuracies.append(metrics.accuracy_score(gts[gts == lab], preds[gts == lab]))
class_accuracies = np.array(class_accuracies)
f1_micro = metrics.f1_score(gts, preds, average='micro')
precision_micro = metrics.precision_score(gts, preds, average='micro')
recall_micro = metrics.recall_score(gts, preds, average='micro')
f1_macro = metrics.f1_score(gts, preds, average='macro')
precision_macro = metrics.precision_score(gts, preds, average='macro')
recall_macro = metrics.recall_score(gts, preds, average='macro')
# class wise score
f1s = metrics.f1_score(gts, preds, average=None)
precisions = metrics.precision_score(gts, preds, average=None)
recalls = metrics.recall_score(gts, preds, average=None)
confusion = metrics.confusion_matrix(gts,preds, labels=labels)
#TODO confusion matrix, recall, precision
return accuracy, f1_micro, precision_micro, recall_micro, f1_macro, precision_macro, recall_macro, confusion, class_accuracies, f1s, precisions, recalls
示例6: main
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import confusion_matrix [as 别名]
def main(argv):
parser = argparse.ArgumentParser()
parser.add_argument("--classifier-saved-model-path", type=str)
parser.add_argument("--text-file-path", type=str, required=True)
parser.add_argument("--label-index", type=str, required=False)
parser.add_argument("--label-file-path", type=str, required=False)
args_namespace = parser.parse_args(argv)
command_line_args = vars(args_namespace)
global logger
logger = log_initializer.setup_custom_logger(global_config.logger_name, "INFO")
if not command_line_args['label_file_path'] and not command_line_args['label_index']:
raise Exception("Provide either label-index or label_file_path")
[style_transfer_score, confusion_matrix] = \
get_style_transfer_score(command_line_args['classifier_saved_model_path'],
command_line_args['text_file_path'],
command_line_args['label_index'],
command_line_args['label_file_path'])
logger.info("style_transfer_score: {}".format(style_transfer_score))
logger.info("confusion_matrix: {}".format(confusion_matrix))
示例7: plot_confusion
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import confusion_matrix [as 别名]
def plot_confusion(title, true_labels, predicted_labels, normalized=True):
labels = list(set(true_labels) | set(predicted_labels))
if normalized:
cm = confusion_matrix(true_labels, predicted_labels, labels=labels)
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
else:
cm = confusion_matrix(true_labels, predicted_labels, labels=labels)
fig, ax = plt.subplots(figsize=(10, 10))
ax.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)
ax.set_title(title)
# plt.colorbar()
tick_marks = np.arange(len(labels))
ax.set_xticks(tick_marks)
ax.set_xticklabels(labels, rotation=90)
ax.set_yticks(tick_marks)
ax.set_yticklabels(labels)
ax.set_ylabel('True Label')
ax.set_xlabel('Predicted Label')
ax.grid(False)
return fig, ax
示例8: QuadWeightedKappa
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import confusion_matrix [as 别名]
def QuadWeightedKappa(y, y_pred):
y_pred = np.argmax(y_pred, 1)
cm = confusion_matrix(y, y_pred)
classes_y, counts_y = np.unique(y, return_counts=True)
classes_y_pred, counts_y_pred = np.unique(y_pred, return_counts=True)
E = np.zeros((classes_y.shape[0], classes_y.shape[0]))
for i, c1 in enumerate(classes_y):
for j, c2 in enumerate(classes_y_pred):
E[c1, c2] = counts_y[i] * counts_y_pred[j]
E = E / np.sum(E) * np.sum(cm)
w = np.zeros((classes_y.shape[0], classes_y.shape[0]))
for i in range(classes_y.shape[0]):
for j in range(classes_y.shape[0]):
w[i, j] = float((i - j)**2) / (classes_y.shape[0] - 1)**2
re = 1 - np.sum(w * cm) / np.sum(w * E)
return re
示例9: _report_ice_cloud
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import confusion_matrix [as 别名]
def _report_ice_cloud(self, output_dir, experiment, test, retrieved):
# Confusion matrix:
fig, ax = plt.subplots(figsize=(12, 10))
cm = confusion_matrix(test.ice_cloud, retrieved.ice_cloud)
img = self._plot_matrix(cm, classes=["Yes", "No"], normalize=True)
fig.colorbar(img, label="probability")
ax.set_title("Ice Cloud Classifier - Performance")
ax.set_ylabel('real ice cloud')
ax.set_xlabel('predicted ice cloud')
fig.tight_layout()
fig.savefig(join(output_dir, "ice-cloud-confusion-matrix.png"))
fig, ax = plt.subplots(figsize=(12, 10))
ax.barh(
np.arange(len(self.ice_cloud.inputs)),
self.ice_cloud.estimator.feature_importances_
)
ax.set_yticks(np.arange(len(self.ice_cloud.inputs)))
ax.set_yticklabels(self.ice_cloud.inputs)
ax.set_xlabel("Feature Importance")
ax.set_ylabel("Feature")
ax.set_title("Ice Cloud Classifier - Importance")
fig.savefig(join(output_dir, "ice-cloud-feature-importance.png"))
示例10: find_optimal
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import confusion_matrix [as 别名]
def find_optimal(error_df):
optimal_threshold = 1000
max_f1 = 0
max_pr = 0
max_re = 0
for threshold in range(1000, 400000, 5000):
print("Threshold: " + str(threshold))
y_pred = [1 if e > threshold else 0 for e in error_df.Reconstruction_error.values]
conf_matrix = confusion_matrix(error_df.True_class, y_pred)
precision, recall, f1 = compute_metrics(conf_matrix)
if f1 > max_f1:
max_f1 = f1
optimal_threshold = threshold
max_pr = precision
max_re = recall
return optimal_threshold, max_pr, max_re, max_f1
示例11: print_evaluation
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import confusion_matrix [as 别名]
def print_evaluation(model,data,ls,log=None):
features,actual = data
predictions = predict(model, features, 500).data.numpy().reshape(-1).tolist()
labels = [ls.idx[i] for i, _ in enumerate(ls.idx)]
actual = [labels[i] for i in actual]
predictions = [labels[i] for i in predictions]
print(accuracy_score(actual, predictions))
print(classification_report(actual, predictions))
print(confusion_matrix(actual, predictions))
data = zip(actual,predictions)
if log is not None:
f = open(log, "w+")
for a,p in data:
f.write(json.dumps({"actual": a, "predicted": p}) + "\n")
f.close()
示例12: run_story_evaluation
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import confusion_matrix [as 别名]
def run_story_evaluation(story_file, policy_model_path, nlu_model_path,
out_file, max_stories):
"""Run the evaluation of the stories, plots the results."""
from sklearn.metrics import confusion_matrix
from sklearn.utils.multiclass import unique_labels
test_y, preds = collect_story_predictions(story_file, policy_model_path,
nlu_model_path, max_stories)
log_evaluation_table(test_y, preds)
cnf_matrix = confusion_matrix(test_y, preds)
plot_confusion_matrix(cnf_matrix, classes=unique_labels(test_y, preds),
title='Action Confusion matrix')
fig = plt.gcf()
fig.set_size_inches(int(20), int(20))
fig.savefig(out_file, bbox_inches='tight')
示例13: evaluate
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import confusion_matrix [as 别名]
def evaluate(config, model, data_iter, test=False):
model.eval()
loss_total = 0
predict_all = np.array([], dtype=int)
labels_all = np.array([], dtype=int)
with torch.no_grad():
for texts, labels in data_iter:
outputs = model(texts)
loss = F.cross_entropy(outputs, labels)
loss_total += loss
labels = labels.data.cpu().numpy()
predic = torch.max(outputs.data, 1)[1].cpu().numpy()
labels_all = np.append(labels_all, labels)
predict_all = np.append(predict_all, predic)
acc = metrics.accuracy_score(labels_all, predict_all)
if test:
report = metrics.classification_report(labels_all, predict_all, target_names=config.class_list, digits=4)
confusion = metrics.confusion_matrix(labels_all, predict_all)
return acc, loss_total / len(data_iter), report, confusion
return acc, loss_total / len(data_iter)
示例14: accuracy
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import confusion_matrix [as 别名]
def accuracy(y_true, y_pred):
# 计算混淆矩阵
y = np.zeros(len(y_true))
y_ = np.zeros(len(y_true))
for i in range(len(y_true)):
y[i] = np.argmax(y_true[i,:])
y_[i] = np.argmax(y_pred[i,:])
cnf_mat = confusion_matrix(y, y_)
# Acc = 1.0*(cnf_mat[1][1]+cnf_mat[0][0])/len(y_true)
# Sens = 1.0*cnf_mat[1][1]/(cnf_mat[1][1]+cnf_mat[1][0])
# Spec = 1.0*cnf_mat[0][0]/(cnf_mat[0][0]+cnf_mat[0][1])
# # 绘制ROC曲线
# fpr, tpr, thresholds = roc_curve(y_true[:,0], y_pred[:,0])
# Auc = auc(fpr, tpr)
# 计算多分类评价值
Sens = recall_score(y, y_, average='macro')
Prec = precision_score(y, y_, average='macro')
F1 = f1_score(y, y_, average='weighted')
Support = precision_recall_fscore_support(y, y_, beta=0.5, average=None)
return Sens, Prec, F1, cnf_mat
示例15: save_cnf_roc
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import confusion_matrix [as 别名]
def save_cnf_roc(y_true, y_pred, classes, isPlot, save_tag = ''):
# 计算混淆矩阵
y = np.zeros(len(y_true))
y_ = np.zeros(len(y_true))
for i in range(len(y_true)):
y[i] = np.argmax(y_true[i,:])
y_[i] = np.argmax(y_pred[i,:])
cnf_mat = confusion_matrix(y, y_)
print cnf_mat
# # 记录混淆矩阵
f = open('experiments/img/confuse_matrixes.txt', 'ab+')
if save_tag[-1] == '0':
f.write(save_tag+'\n')
f.write('No.' + save_tag[-1] + '\n')
f.write(str(cnf_mat) + '\n')
f.close()
# # 记录ROC曲线
plot_roc_curve(y_true, y_pred, range(classes), 'all/'+save_tag)
###########################
# 计算TP、TN、FP、FN