-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclac_metric.py
More file actions
59 lines (49 loc) · 2.38 KB
/
clac_metric.py
File metadata and controls
59 lines (49 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import numpy as np
def get_metrics(real_score, predict_score):
sorted_predict_score = np.array(sorted(list(set(np.array(predict_score).flatten()))))
sorted_predict_score_num = len(sorted_predict_score)
thresholds = sorted_predict_score[np.int32(sorted_predict_score_num * np.arange(1, 1000) / 1000)]
thresholds = np.mat(thresholds)
thresholds_num = thresholds.shape[1]
predict_score_matrix = np.tile(predict_score, (thresholds_num, 1))
negative_index = np.where(predict_score_matrix < thresholds.T)
positive_index = np.where(predict_score_matrix >= thresholds.T)
predict_score_matrix[negative_index] = 0
predict_score_matrix[positive_index] = 1
TP = predict_score_matrix.dot(real_score.T)
FP = predict_score_matrix.sum(axis=1) - TP
FN = real_score.sum() - TP
TN = len(real_score.T) - TP - FP - FN
fpr = FP / (FP + TN)
tpr = TP / (TP + FN)
ROC_dot_matrix = np.mat(sorted(np.column_stack((fpr, tpr)).tolist())).T
ROC_dot_matrix.T[0] = [0, 0]
ROC_dot_matrix = np.c_[ROC_dot_matrix, [1, 1]]
x_ROC = ROC_dot_matrix[0].T
y_ROC = ROC_dot_matrix[1].T
auc = 0.5 * (x_ROC[1:] - x_ROC[:-1]).T * (y_ROC[:-1] + y_ROC[1:])
recall_list = tpr
precision_list = TP / (TP + FP)
PR_dot_matrix = np.mat(sorted(np.column_stack((recall_list, precision_list)).tolist())).T
PR_dot_matrix.T[0] = [0, 1]
PR_dot_matrix = np.c_[PR_dot_matrix, [1, 0]]
x_PR = PR_dot_matrix[0].T
y_PR = PR_dot_matrix[1].T
aupr = 0.5 * (x_PR[1:] - x_PR[:-1]).T * (y_PR[:-1] + y_PR[1:])
f1_score_list = 2 * TP / (len(real_score.T) + TP - TN)
accuracy_list = (TP + TN) / len(real_score.T)
specificity_list = TN / (TN + FP)
mcc_list = (TP * TN - FP * FN) / np.sqrt((TP + FP) * (TP + FN) * (TN + FP) * (TN + FN))
max_index = np.argmax(f1_score_list)
f1_score = f1_score_list[max_index]
accuracy = accuracy_list[max_index]
specificity = specificity_list[max_index]
recall = recall_list[max_index]
precision = precision_list[max_index]
mcc = mcc_list[max_index]
return [aupr[0, 0], auc[0, 0], f1_score, accuracy, recall, specificity, precision, mcc]
def cv_model_evaluate(interaction_matrix, predict_matrix, train_matrix):
test_index = np.where(train_matrix == 0)
real_score = interaction_matrix[test_index]
predict_score = predict_matrix[test_index]
return get_metrics(real_score, predict_score)