Skip to content

Commit 9706dc9

Browse files
committed
add model comparision
1 parent b3cd15c commit 9706dc9

5 files changed

+68
-3
lines changed
24.9 KB
Loading
Loading

metrics/precision_recall_curve.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,3 @@
5151
plt.title('2-class Precision-Recall curve')
5252
plt.legend()
5353
plt.show()
54-
55-
# %%

metrics/precision_recall_curve_edge_case.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Example of computing precision recall curve for random and ideal
2-
classificator.
2+
classifier.
33
"""
44
# %%
55

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""Example of computing precision recall curve
2+
"""
3+
# %%
4+
5+
import matplotlib.pyplot as plt
6+
import numpy as np
7+
import sklearn.metrics as skm
8+
9+
10+
11+
y_true = np.array([0, 0, 0, 0, 1, 1, 1, 1, 1, 1])
12+
13+
# model1 curve will dominate the model0
14+
# output from model0
15+
y_score0 = np.array([0.75, 0.5, 0.3, 0.35, 0.45, 0.7, 0.3, 0.33, 0.5, 0.8])
16+
# output from model1
17+
y_score1 = np.array([0.6, 0.3, 0.3, 0.55, 0.65, 0.4, 0.55, 0.33, 0.75, 0.3])
18+
19+
# model 1 is better
20+
# # output from model0
21+
# y_score0 = np.array([0.75, 0.5, 0.3, 0.35, 0.45, 0.7, 0.3, 0.33, 0.5, 0.8])
22+
# # output from model1
23+
# y_score1 = np.array([0.7, 0.3, 0.3, 0.55, 0.75, 0.4, 0.5, 0.33, 0.72, 0.3])
24+
25+
# looking only at curves it is not so obvious, which one is better
26+
# output from model0
27+
y_score0 = np.array([0.7, 0.45, 0.3, 0.35, 0.45, 0.7, 0.3, 0.33, 0.55, 0.8])
28+
# output from model1
29+
y_score1 = np.array([0.6, 0.3, 0.3, 0.55, 0.65, 0.4, 0.5, 0.33, 0.75, 0.3])
30+
31+
32+
# %
33+
34+
35+
# first model
36+
precision0, recall0, tresholds0 = skm.precision_recall_curve(y_true, y_score0)
37+
38+
# second model
39+
precision1, recall1, tresholds1 = skm.precision_recall_curve(y_true, y_score1)
40+
41+
avg_prec0 = skm.average_precision_score(y_true, y_score0)
42+
auc0 = skm.auc(recall0,precision0)
43+
print(f"Model 0 average_precision={avg_prec0} area under curve={auc0}")
44+
45+
avg_prec1 = skm.average_precision_score(y_true, y_score1)
46+
auc1 = skm.auc(recall1,precision1)
47+
48+
print(f"Model 1 average_precision={avg_prec1} area under curve={auc1}")
49+
50+
51+
# % plot curve
52+
plt.plot(recall0, precision0, 'ro')
53+
plt.plot(recall0, precision0, 'r', label='model 0')
54+
55+
plt.plot(recall1, precision1, 'bo')
56+
plt.plot(recall1, precision1, 'b', label='model 1')
57+
58+
plt.xlabel('Recall')
59+
plt.ylabel('Precision')
60+
plt.ylim([0.0, 1.05])
61+
plt.xlim([0.0, 1.0])
62+
plt.title('Precision-Recall curve for 2 ml models')
63+
plt.legend()
64+
plt.show()
65+
66+
67+
# %%

0 commit comments

Comments
 (0)