forked from amueller/introduction_to_ml_with_python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_metrics.py
88 lines (72 loc) · 3.96 KB
/
plot_metrics.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import numpy as np
import matplotlib.pyplot as plt
from .tools import plot_2d_separator, plot_2d_scores, cm
def plot_confusion_matrix_illustration():
confusion = np.array([[401, 2], [8, 39]])
plt.title("confusion_matrix")
plt.text(0.45, .6, confusion[0, 0], size=70, horizontalalignment='right')
plt.text(0.45, .1, confusion[1, 0], size=70, horizontalalignment='right')
plt.text(.95, .6, confusion[0, 1], size=70, horizontalalignment='right')
plt.text(.95, 0.1, confusion[1, 1], size=70, horizontalalignment='right')
plt.xticks([.25, .75], ["predicted 'not 4'", "predicted '4'"], size=20)
plt.yticks([.25, .75], ["true '4'", "true 'not 4'"], size=20)
plt.plot([.5, .5], [0, 1], '--', c='k')
plt.plot([0, 1], [.5, .5], '--', c='k')
plt.xlim(0, 1)
plt.ylim(0, 1)
def plot_binary_confusion_matrix():
plt.title("binary_confusion_matrix_tp_fp")
plt.text(0.45, .6, "TN", size=100, horizontalalignment='right')
plt.text(0.45, .1, "FN", size=100, horizontalalignment='right')
plt.text(.95, .6, "FP", size=100, horizontalalignment='right')
plt.text(.95, 0.1, "TP", size=100, horizontalalignment='right')
plt.xticks([.25, .75], ["predicted negative", "predicted positive"], size=15)
plt.yticks([.25, .75], ["positive class", "negative class"], size=15)
plt.plot([.5, .5], [0, 1], '--', c='k')
plt.plot([0, 1], [.5, .5], '--', c='k')
plt.xlim(0, 1)
plt.ylim(0, 1)
def plot_decision_threshold():
from mglearn.datasets import make_blobs
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
X, y = make_blobs(n_samples=(400, 50), centers=2, cluster_std=[7.0, 2],
random_state=22)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
fig, axes = plt.subplots(2, 3, figsize=(15, 8))
plt.suptitle("decision_threshold")
axes[0, 0].set_title("training data")
axes[0, 0].scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm)
svc = SVC(gamma=.05).fit(X_train, y_train)
axes[0, 1].set_title("decision with threshold 0")
axes[0, 1].scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm)
plot_2d_scores(svc, X_train, function="decision_function", alpha=.7,
ax=axes[0, 1])
plot_2d_separator(svc, X_train, linewidth=3, ax=axes[0, 1])
axes[0, 2].set_title("decision with threshold -0.8")
axes[0, 2].scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm)
plot_2d_separator(svc, X_train, linewidth=3, ax=axes[0, 2], threshold=-.8)
plot_2d_scores(svc, X_train, function="decision_function", alpha=.7,
ax=axes[0, 2])
axes[1, 0].set_visible(False)
mask = np.abs(X_train[:, 1] - 7) < 5
bla = np.sum(mask)
line = np.linspace(X_train.min(), X_train.max(), 100)
axes[1, 1].set_title("Cross-section with threshold 0")
axes[1, 1].plot(line, svc.decision_function(np.c_[line, 10 * np.ones(100)]), c='k')
contour = (svc.decision_function(np.c_[line, 10 * np.ones(100)]) > 0).reshape(1, -1).repeat(10, axis=0)
axes[1, 1].contourf(line, np.linspace(-1.5, 1.5, 10), contour, alpha=0.2, cmap=cm)
axes[1, 1].scatter(X_train[mask, 0], np.zeros(bla), c=y_train[mask], cmap=cm, alpha=.1, s=100)
axes[1, 1].set_xlim(X_train.min(), X_train.max())
axes[1, 1].set_ylim(-1.5, 1.5)
axes[1, 1].set_xticks(())
axes[1, 1].set_ylabel("Decision value")
contour2 = (svc.decision_function(np.c_[line, 10 * np.ones(100)]) > -.8).reshape(1, -1).repeat(10, axis=0)
axes[1, 2].set_title("Cross-section with threshold -0.8")
axes[1, 2].contourf(line, np.linspace(-1.5, 1.5, 10), contour2, alpha=0.2, cmap=cm)
axes[1, 2].scatter(X_train[mask, 0], np.zeros(bla), c=y_train[mask], cmap=cm, alpha=.1, s=100)
axes[1, 2].plot(line, svc.decision_function(np.c_[line, 10 * np.ones(100)]), c='k')
axes[1, 2].set_xlim(X_train.min(), X_train.max())
axes[1, 2].set_ylim(-1.5, 1.5)
axes[1, 2].set_xticks(())
axes[1, 2].set_ylabel("Decision value")