/
actclf01.py
136 lines (89 loc) · 3.97 KB
/
actclf01.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# A script that uses Logistic Regression
# for the exercise at page 13, Figure 2.2.
# of the book "Active Learning" by Burr Settles,
# Link to the book: http://active-learning.net/
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
np.random.seed(1)
mu1 = np.array([-2,0])
mu2 = np.array([2,0])
sig = 1
n_points = 200
cl1 = sig * np.random.randn(n_points,2) + mu1
cl2 = sig * np.random.randn(n_points,2) + mu2
y1 = np.zeros((n_points,1))
y2 = np.ones((n_points,1))
ind1 = np.random.choice(range(n_points), size=1, replace=False)
ind2 = np.random.choice(range(n_points), size=1, replace=False)
X1_train, X1 = cl1[ind1,:], cl1[[i for i in range(n_points) if i not in ind1],:]
y1_train, y1 = y1[ind1,:], y1[[i for i in range(n_points) if i not in ind1],:]
X2_train, X2 = cl2[ind2,:], cl2[[i for i in range(n_points) if i not in ind2],:]
y2_train, y2 = y2[ind2,:], y2[[i for i in range(n_points) if i not in ind2],:]
X_test = np.vstack((X1,X2))
y_test = np.vstack((y1,y2))
X_train = np.vstack((X1_train,X2_train))
y_train = np.vstack((y1_train,y2_train))
X_test_r = np.copy(X_test)
y_test_r = np.copy(y_test)
X_train_r = np.copy(X_train)
y_train_r = np.copy(y_train)
logreg = LogisticRegression()
def clf(X_train, X_test, y_train, y_test):
logreg.fit(X_train, y_train.ravel())
pred = logreg.predict_proba(X_test)
sc_c = logreg.score(X_test, y_test)
w = logreg.coef_[0]
a = -w[0] / w[1]
yy = a * xx - (logreg.intercept_[0]) / w[1]
return pred, sc_c, yy
def xy_update(X_train, X_test, y_train, y_test, ind):
X_train = np.vstack((X_train, X_test[ind,:]))
y_train = np.vstack((y_train, y_test[ind,:]))
X_test = X_test[[i for i in range(len(X_test)) if i != ind],:]
y_test = y_test[[i for i in range(len(y_test)) if i != ind],:]
return X_train, X_test, y_train, y_test
n_iter = 50 # number of iterations
sc = []
sc_r = []
xx = np.linspace(-4,4)
for i in range(n_iter):
# --------------- Uncertainty sampling ----------------
pred, sc_c, yy = clf(X_train, X_test, y_train, y_test)
print('Score for unc sampling: %.6f' % (sc_c))
sc.append(sc_c)
ind_min_conf = np.argmin(np.absolute(pred-0.5), axis=0)[0]
# print('Least confident: ', X_test[ind_min_conf,:], y_test[ind_min_conf],'\n')
# ------------------ Random sampling ------------------
pred_r, sc_c, yy_r = clf(X_train_r, X_test_r, y_train_r, y_test_r)
print('Score for rnd sampling: %.6f\n' % (sc_c))
sc_r.append(sc_c)
ind_r = np.random.choice(range(len(X_test_r)))
# -----------------------------------------------------
class_colors = ["red" if i else "green" for i in y_train]
f, ax = plt.subplots(nrows=1, ncols=2)
# Data points
ax[0].scatter(cl1[:,0], cl1[:,1], s = 2, marker='.', color='green')
ax[0].scatter(cl2[:,0], cl2[:,1], s = 2, marker='.', color='red')
# Labeled data
ax[0].scatter(X_train[:,0], X_train[:,1], s = 50, marker="s", c=class_colors)
# Separating line obtained with uncertainty sampling
ax[0].plot(xx, yy, 'b-')
# Separating line obtained with random sampling
ax[0].plot(xx, yy_r, 'b:')
ax[0].set_xlim((-5, 5))
ax[0].set_ylim((-5, 5))
ax[0].grid()
# Histogram of prediction probabilities
# It's minimum is always around 0.5
# which makes uncertainty sampling win over random sampling
ax[1].hist(pred[:,0], bins=20)
plt.suptitle('Iteration '+str(i+1))
plt.show()
X_train, X_test, y_train, y_test = xy_update(X_train, X_test, y_train, y_test, ind_min_conf)
X_train_r, X_test_r, y_train_r, y_test_r = xy_update(X_train_r, X_test_r, y_train_r, y_test_r, ind_r)
# Plot the mean accuracy for the uncertainty sampling and random sampling approaches
plt.plot(range(n_iter), sc, 'b-')
plt.plot(range(n_iter), sc_r, 'b:')
plt.grid()
plt.show()