本文整理汇总了Python中sklearn.metrics.precision_score方法的典型用法代码示例。如果您正苦于以下问题:Python metrics.precision_score方法的具体用法?Python metrics.precision_score怎么用?Python metrics.precision_score使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.metrics
的用法示例。
在下文中一共展示了metrics.precision_score方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: classification_scores
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import precision_score [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
示例2: multi_class_classification
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import precision_score [as 别名]
def multi_class_classification(data_X,data_Y):
'''
calculate multi-class classification and return related evaluation metrics
'''
svc = svm.SVC(C=1, kernel='linear')
# X_train, X_test, y_train, y_test = train_test_split( data_X, data_Y, test_size=0.4, random_state=0)
clf = svc.fit(data_X, data_Y) #svm
# array = svc.coef_
# print array
predicted = cross_val_predict(clf, data_X, data_Y, cv=2)
print "accuracy",metrics.accuracy_score(data_Y, predicted)
print "f1 score macro",metrics.f1_score(data_Y, predicted, average='macro')
print "f1 score micro",metrics.f1_score(data_Y, predicted, average='micro')
print "precision score",metrics.precision_score(data_Y, predicted, average='macro')
print "recall score",metrics.recall_score(data_Y, predicted, average='macro')
print "hamming_loss",metrics.hamming_loss(data_Y, predicted)
print "classification_report", metrics.classification_report(data_Y, predicted)
print "jaccard_similarity_score", metrics.jaccard_similarity_score(data_Y, predicted)
# print "log_loss", metrics.log_loss(data_Y, predicted)
print "zero_one_loss", metrics.zero_one_loss(data_Y, predicted)
# print "AUC&ROC",metrics.roc_auc_score(data_Y, predicted)
# print "matthews_corrcoef", metrics.matthews_corrcoef(data_Y, predicted)
示例3: evaluation_analysis
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import precision_score [as 别名]
def evaluation_analysis(true_label,predicted):
'''
return all metrics results
'''
print "accuracy",metrics.accuracy_score(true_label, predicted)
print "f1 score macro",metrics.f1_score(true_label, predicted, average='macro')
print "f1 score micro",metrics.f1_score(true_label, predicted, average='micro')
print "precision score",metrics.precision_score(true_label, predicted, average='macro')
print "recall score",metrics.recall_score(true_label, predicted, average='macro')
print "hamming_loss",metrics.hamming_loss(true_label, predicted)
print "classification_report", metrics.classification_report(true_label, predicted)
print "jaccard_similarity_score", metrics.jaccard_similarity_score(true_label, predicted)
print "log_loss", metrics.log_loss(true_label, predicted)
print "zero_one_loss", metrics.zero_one_loss(true_label, predicted)
print "AUC&ROC",metrics.roc_auc_score(true_label, predicted)
print "matthews_corrcoef", metrics.matthews_corrcoef(true_label, predicted)
示例4: get_all_metrics
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import precision_score [as 别名]
def get_all_metrics(model, eval_data, eval_labels, pred_labels):
fpr, tpr, thresholds_keras = roc_curve(eval_labels, pred_labels)
auc_ = auc(fpr, tpr)
print("auc_keras:" + str(auc_))
score = model.evaluate(eval_data, eval_labels, verbose=0)
print("Test accuracy: " + str(score[1]))
precision = precision_score(eval_labels, pred_labels)
print('Precision score: {0:0.2f}'.format(precision))
recall = recall_score(eval_labels, pred_labels)
print('Recall score: {0:0.2f}'.format(recall))
f1 = f1_score(eval_labels, pred_labels)
print('F1 score: {0:0.2f}'.format(f1))
average_precision = average_precision_score(eval_labels, pred_labels)
print('Average precision-recall score: {0:0.2f}'.format(average_precision))
return auc_, score[1], precision, recall, f1, average_precision, fpr, tpr
示例5: get_all_metrics_
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import precision_score [as 别名]
def get_all_metrics_(eval_labels, pred_labels):
fpr, tpr, thresholds_keras = roc_curve(eval_labels, pred_labels)
auc_ = auc(fpr, tpr)
print("auc_keras:" + str(auc_))
precision = precision_score(eval_labels, pred_labels)
print('Precision score: {0:0.2f}'.format(precision))
recall = recall_score(eval_labels, pred_labels)
print('Recall score: {0:0.2f}'.format(recall))
f1 = f1_score(eval_labels, pred_labels)
print('F1 score: {0:0.2f}'.format(f1))
average_precision = average_precision_score(eval_labels, pred_labels)
print('Average precision-recall score: {0:0.2f}'.format(average_precision))
return auc_, precision, recall, f1, average_precision, fpr, tpr
示例6: run_evaluate
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import precision_score [as 别名]
def run_evaluate(self, test):
"""Evaluates performance on test set
Args:
test: dataset that yields tuple of (sentences, relation tags)
Returns:
metrics: (dict) metrics["acc"] = 98.4, ...
"""
y_true, y_pred = [], []
for data in minibatches(test, self.config.batch_size):
word_batch, pos1_batch, pos2_batch, pos_batch, y_batch = data
relations_pred = self.predict_batch(word_batch, pos1_batch, pos2_batch, pos_batch)
assert len(relations_pred) == len(y_batch)
y_true += y_batch
y_pred += relations_pred.tolist()
acc = accuracy_score(y_true, y_pred)
p = precision_score(y_true, y_pred, average='macro')
r = recall_score(y_true, y_pred, average='macro')
f1 = f1_score(y_true, y_pred, average='macro')
return {"acc":acc, "p":p, "r":r, "f1":f1}
示例7: evaluate
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import precision_score [as 别名]
def evaluate(trueValues, predicted, decimals, note):
print note
label = 1
avg = 'weighted'
a = accuracy_score(trueValues, predicted)
p = precision_score(trueValues, predicted, pos_label=label, average=avg)
r = recall_score(trueValues, predicted, pos_label=label, average=avg)
avg_f1 = f1_score(trueValues, predicted, pos_label=label, average=avg)
fclasses = f1_score(trueValues, predicted, average=None)
f1c1 = fclasses[0]; f1c2 = fclasses[1]
fw = (f1c1 + f1c2)/2.0
print 'accuracy:\t', str(round(a,decimals))
print 'precision:\t', str(round(p,decimals))
print 'recall:\t', str(round(r,decimals))
print 'avg f1:\t', str(round(avg_f1,decimals))
print 'c1 f1:\t', str(round(f1c1,decimals))
print 'c2 f1:\t', str(round(f1c2,decimals))
print 'avg(c1,c2):\t', str(round(fw,decimals))
print '------------'
###################################################################################
# split a parallel or comparable corpus into two parts
示例8: accuracy
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import precision_score [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
示例9: calculate_scores
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import precision_score [as 别名]
def calculate_scores(y_predicted, y_true):
"""
Function to calculate different performance scores
"""
accuracy = accuracy_score(y_pred=y_predicted, y_true=y_true)
precision = precision_score(y_pred=y_predicted, y_true=y_true)
average_precision_score1 = average_precision_score(y_score=y_predicted, y_true=y_true)
f1_score1 = f1_score(y_pred=y_predicted, y_true=y_true)
print("Accuracy score:", accuracy)
print("Precision score:", precision)
print("Average Precision score:", average_precision_score1)
print("F1 score:", f1_score1)
print("Outlier detection and/or treatment completed.")
return {"accuracy": accuracy,
"precision": precision,
"average_precision_score": average_precision_score1,
"f1_score": f1_score1,
}
示例10: test_zero_precision_recall
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import precision_score [as 别名]
def test_zero_precision_recall():
# Check that pathological cases do not bring NaNs
old_error_settings = np.seterr(all='raise')
try:
y_true = np.array([0, 1, 2, 0, 1, 2])
y_pred = np.array([2, 0, 1, 1, 2, 0])
assert_almost_equal(precision_score(y_true, y_pred,
average='macro'), 0.0, 2)
assert_almost_equal(recall_score(y_true, y_pred, average='macro'),
0.0, 2)
assert_almost_equal(f1_score(y_true, y_pred, average='macro'),
0.0, 2)
finally:
np.seterr(**old_error_settings)
示例11: test_precision_warnings
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import precision_score [as 别名]
def test_precision_warnings():
clean_warning_registry()
with warnings.catch_warnings(record=True) as record:
warnings.simplefilter('always')
precision_score(np.array([[1, 1], [1, 1]]),
np.array([[0, 0], [0, 0]]),
average='micro')
assert_equal(str(record.pop().message),
'Precision is ill-defined and '
'being set to 0.0 due to no predicted samples.')
precision_score([0, 0], [0, 0])
assert_equal(str(record.pop().message),
'Precision is ill-defined and '
'being set to 0.0 due to no predicted samples.')
assert_no_warnings(precision_score,
np.array([[0, 0], [0, 0]]),
np.array([[1, 1], [1, 1]]),
average='micro')
示例12: test_ovr_multilabel_dataset
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import precision_score [as 别名]
def test_ovr_multilabel_dataset():
base_clf = MultinomialNB(alpha=1)
for au, prec, recall in zip((True, False), (0.51, 0.66), (0.51, 0.80)):
X, Y = datasets.make_multilabel_classification(n_samples=100,
n_features=20,
n_classes=5,
n_labels=2,
length=50,
allow_unlabeled=au,
random_state=0)
X_train, Y_train = X[:80], Y[:80]
X_test, Y_test = X[80:], Y[80:]
clf = OneVsRestClassifier(base_clf).fit(X_train, Y_train)
Y_pred = clf.predict(X_test)
assert clf.multilabel_
assert_almost_equal(precision_score(Y_test, Y_pred, average="micro"),
prec,
decimal=2)
assert_almost_equal(recall_score(Y_test, Y_pred, average="micro"),
recall,
decimal=2)
示例13: get_score
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import precision_score [as 别名]
def get_score(self, model, texta, textb, labels, score_type='f1'):
metrics_map = {
'f1': f1_score,
'p': precision_score,
'r': recall_score,
'acc': accuracy_score
}
metric_func = metrics_map[score_type] if score_type in metrics_map else metrics_map['f1']
assert texta.size(1) == textb.size(1) == len(labels)
vec_predict = model(texta, textb)
soft_predict = torch.softmax(vec_predict, dim=1)
predict_prob, predict_index = torch.max(soft_predict.cpu().data, dim=1)
# print('prob', predict_prob)
# print('index', predict_index)
# print('labels', labels)
labels = labels.view(-1).cpu().data.numpy()
return metric_func(predict_index, labels, average='micro')
示例14: get_score
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import precision_score [as 别名]
def get_score(self, model, texta, textb, labels, score_type='f1'):
metrics_map = {
'f1': f1_score,
'p': precision_score,
'r': recall_score,
'acc': accuracy_score
}
metric_func = metrics_map[score_type] if score_type in metrics_map else metrics_map['f1']
assert texta.size(1) == textb.size(1) == len(labels)
predict_prob = model(texta, textb)
# print('predict', predict_prob)
# print('labels', labels)
predict_labels = torch.gt(predict_prob, 0.5)
predict_labels = predict_labels.view(-1).cpu().data.numpy()
labels = labels.view(-1).cpu().data.numpy()
return metric_func(predict_labels, labels, average='micro')
示例15: get_score
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import precision_score [as 别名]
def get_score(self, model, x, y, pos, rel, field_x, field_y, field_pos, score_type='f1'):
metrics_map = {
'f1': f1_score,
'p': precision_score,
'r': recall_score,
'acc': accuracy_score
}
metric_func = metrics_map[score_type] if score_type in metrics_map else metrics_map['f1']
vec_x = torch.tensor([field_x.stoi[i] for i in x])
len_vec_x = torch.tensor([len(vec_x)]).to(DEVICE)
vec_pos = torch.tensor([field_pos.stoi[i] for i in pos])
vec_rel = torch.tensor([int(x) for x in rel])
predict_y = model(vec_x.view(-1, 1).to(DEVICE), vec_pos.view(-1, 1).to(DEVICE), vec_rel.view(-1, 1).to(DEVICE),
len_vec_x)[0]
true_y = [field_y.stoi[i] for i in y]
assert len(true_y) == len(predict_y)
return metric_func(predict_y, true_y, average='micro')