/
ordinal.py
executable file
·310 lines (237 loc) · 8.71 KB
/
ordinal.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
"""
some ordinal regression algorithms
This implements the margin-based ordinal regression methods described
in http://arxiv.org/abs/1408.2327
"""
import numpy as np
from scipy import optimize, linalg, stats
from sklearn import base, metrics, linear_model
from metrics import pairwise_disagreement
from joblib import Memory
METRIC = lambda x, y: - metrics.mean_absolute_error(x, y)
# METRIC = lambda x, y: - metrics.zero_one_loss(x, y, normalize=True)
# METRIC = lambda x, y: - pairwise_disagreement(x, y)
def sigmoid(t):
# sigmoid function, 1 / (1 + exp(-t))
# stable computation
idx = t > 0
out = np.zeros_like(t)
out[idx] = 1. / (1 + np.exp(-t[idx]))
exp_t = np.exp(t[~idx])
out[~idx] = exp_t / (1. + exp_t)
return out
def log_loss(Z):
# stable computation of the logistic loss
idx = Z > 0
out = np.zeros_like(Z)
out[idx] = np.log(1 + np.exp(-Z[idx]))
out[~idx] = (-Z[~idx] + np.log(1 + np.exp(Z[~idx])))
return out
def obj_margin(x0, X, y, alpha, n_class, weights):
"""
Objective function for the general margin-based formulation
"""
w = x0[:X.shape[1]]
c = x0[X.shape[1]:]
theta = np.cumsum(c)
#theta = np.sort(theta)
W = weights[y]
Xw = X.dot(w)
Alpha = theta[:, None] - Xw # (n_class - 1, n_samples)
idx = np.arange(n_class - 1)[:, None] < y
Alpha[idx] *= -1
return np.sum(W.T * log_loss(Alpha)) / float(X.shape[0]) + \
alpha * (linalg.norm(w) ** 2)
def grad_margin(x0, X, y, alpha, n_class, weights):
"""
Gradient for the general margin-based formulation
"""
w = x0[:X.shape[1]]
c = x0[X.shape[1]:]
theta = np.cumsum(c)
#theta = np.sort(theta)
W = weights[y]
Xw = X.dot(w)
Alpha = theta[:, None] - Xw # (n_class - 1, n_samples)
idx = np.arange(n_class - 1)[:, None] < y
Alpha[idx] *= -1
W[idx.T] *= -1
Sigma = W.T * sigmoid(-Alpha)
grad_w = X.T.dot(Sigma.sum(0)) / float(X.shape[0]) + alpha * 2 * w
grad_theta = - Sigma.sum(1) / float(X.shape[0])
tmp = np.concatenate(([0], grad_theta))
grad_c = np.sum(grad_theta) - np.cumsum(tmp[:-1])
return np.concatenate((grad_w, grad_c), axis=0)
def obj_multiclass(x0, X, y, alpha, n_class):
n_samples, n_features = X.shape
W = x0.reshape((n_features + 1, n_class-1))
Wk = - W.sum(1)[:, None]
W = np.concatenate((W, Wk), axis=1)
X = np.concatenate((X, np.ones((n_samples, 1))), axis=1)
Y = np.zeros((n_samples, n_class))
Y[:] = - 1./(n_class - 1.)
for i in range(n_samples):
Y[i, y[i]] = 1.
L = np.ones((n_class, n_class)) - np.eye(n_class)
obj = (L[y] * np.fmax(X.dot(W) - Y, 0)).sum() / float(n_samples)
Wt = W[:n_features]
penalty = alpha * np.trace(Wt.T.dot(Wt))
return obj + penalty
def obj_multiclass2(x0, X, y, alpha, n_class):
n_samples, n_features = X.shape
W = x0.reshape((n_features + 1, n_class-1))
Wk = - W.sum(1)[:, None]
W = np.concatenate((W, Wk), axis=1)
X = np.concatenate((X, np.ones((n_samples, 1))), axis=1)
Y = np.zeros((n_samples, n_class))
Y[:] = - 1./(n_class - 1.)
for i in range(n_samples):
Y[i, y[i]] = 1.
# L = np.abs(np.arange(n_class)[:, None] - np.arange(n_class))
L = np.ones((n_class, n_class)) - np.eye(n_class)
obj = (L[y] * np.fmax(X.dot(W) - Y, 0)).sum() / float(n_samples)
#1/0
Wt = W[:n_features]
penalty = alpha * np.trace(Wt.T.dot(Wt))
return obj + penalty
def threshold_fit(X, y, alpha, n_class, mode='AE', verbose=False,
maxiter=10000, bounds=False):
"""
Solve the general threshold-based ordinal regression model
using the logistic loss as surrogate of the 0-1 loss
Parameters
----------
mode : string, one of {'AE', '0-1'}
"""
X = np.asarray(X)
y = np.asarray(y) # XXX check its made of integers
n_samples, n_features = X.shape
if mode == 'AE':
weights = np.ones((n_class, n_class - 1))
elif mode == '0-1':
weights = np.diag(np.ones(n_class - 1)) + \
np.diag(np.ones(n_class - 2), k=-1)
weights = np.vstack((weights, np.zeros(n_class -1)))
weights[-1, -1] = 1
x0 = np.zeros(n_features + n_class - 1)
x0[X.shape[1]:] = np.arange(n_class - 1)
if bounds == True:
bounds = [(None, None)] * (n_features + 1) + [(0, None)] * (n_class - 2)
else:
bounds = None
options = {'maxiter' : maxiter}
sol = optimize.minimize(obj_margin, x0, jac=grad_margin,
args=(X, y, alpha, n_class, weights), method='L-BFGS-B', bounds=bounds,
options=options)
if not sol.success:
print(sol.message)
w, c = sol.x[:X.shape[1]], sol.x[X.shape[1]:]
theta = np.cumsum(c)
return w, np.sort(theta)
def threshold_predict(X, w, theta):
"""
Class numbers are between 0 and k-1
"""
idx = np.concatenate((np.argsort(theta), [theta.size]))
pred = []
n_samples = X.shape[0]
Xw = X.dot(w)
tmp = theta[:, None] - Xw
pred = np.sum(tmp <= 0, axis=0).astype(np.int)
return pred
def multiclass_fit(X, y, alpha, n_class, maxiter=5000000):
"""
Multiclass classification with absolute error cost
Lee, Yoonkyung, Yi Lin, and Grace Wahba. "Multicategory support
vector machines: Theory and application to the classification of
microarray data and satellite radiance data." Journal of the
American Statistical Association 99.465 (2004): 67-81.
"""
X = np.asarray(X)
y = np.asarray(y) # XXX check its made of integers
n_samples, n_features = X.shape
x0 = np.random.randn((n_features + 1) * (n_class - 1))
options = {'maxiter' : maxiter}
sol = optimize.minimize(obj_multiclass, x0, jac=False,
args=(X, y, alpha, n_class), method='L-BFGS-B',
options=options)
sol = optimize.minimize(obj_multiclass, sol.x, jac=False,
args=(X, y, alpha, n_class), method='L-BFGS-B',
options=options)
if not sol.success:
print(sol.message)
W = sol.x.reshape((n_features + 1, n_class-1))
Wk = - W.sum(1)[:, None]
W = np.concatenate((W, Wk), axis=1)
return W
def multiclass_predict(X, W):
n_samples, n_features = X.shape
X = np.concatenate((X, np.ones((n_samples, 1))), axis=1)
XW = X.dot(W)
return np.argmax(XW, axis=1)
class MarginOR(base.BaseEstimator):
def __init__(self, n_class=2, alpha=1., mode='AE',
verbose=0, maxiter=10000):
self.alpha = alpha
self.mode = mode
self.n_class = n_class
self.verbose = verbose
self.maxiter = maxiter
def fit(self, X, y):
self.w_, self.theta_ = threshold_fit(X, y, self.alpha, self.n_class,
mode=self.mode, verbose=self.verbose)
return self
def predict(self, X):
return threshold_predict(X, self.w_, self.theta_)
def score(self, X, y):
pred = self.predict(X)
return METRIC(pred, y)
class MulticlassOR(base.BaseEstimator):
def __init__(self, n_class=2, alpha=1.,
verbose=0, maxiter=10000):
self.alpha = alpha
self.n_class = n_class
self.verbose = verbose
self.maxiter = maxiter
def fit(self, X, y):
self.W_ = multiclass_fit(X, y, self.alpha, self.n_class)
return self
def predict(self, X):
return multiclass_predict(X, self.W_)
def score(self, X, y):
pred = self.predict(X)
return METRIC(pred, y)
if __name__ == '__main__':
np.random.seed(0)
from sklearn import datasets, metrics, svm, cross_validation
n_class = 5
n_samples = 100
n_dim = 10
X, y = datasets.make_regression(n_samples=n_samples, n_features=n_dim,
n_informative=n_dim // 10, noise=20)
bins = stats.mstats.mquantiles(y, np.linspace(0, 1, n_class + 1))
y = np.digitize(y, bins[:-1])
y -= np.min(y)
#X, y = datasets.make_classification(n_samples=n_samples,
#n_informative=5, n_classes=n_class, n_features=20)
print X.shape
print y
print
#w, theta = threshold_fit(X, y, 0., n_class, mode='AE')
#pred = threshold_predict(X, w, theta)
#print metrics.mean_absolute_error(pred, y)
#print metrics.zero_one_loss(pred, y, normalize=True)
#print
cv = cross_validation.KFold(y.size)
for train, test in cv:
#test = train
clf = linear_model.LogisticRegression(C=1e3).fit(X[train], y[train])
print clf.score(X[test], y[test])
#w, theta = threshold_fit(X[train], y[train], 0., n_class, mode='AE',
#bounds=False)
#pred = threshold_predict(X[test], w, theta)
#print metrics.accuracy_score(pred, y[test])
W = multiclass_fit(X[train], y[train], 0., n_class)
pred = multiclass_predict(X[test], W)
print metrics.accuracy_score(pred, y[test])
break