-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
312 lines (268 loc) · 10.5 KB
/
utils.py
File metadata and controls
312 lines (268 loc) · 10.5 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
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
310
311
312
"""
@author: suriya
"""
import numpy as np
import scipy.sparse as sp
import scipy.linalg as la
from numba import jit
from sklearn import svm
from sklearn.linear_model import LogisticRegression
from sklearn.grid_search import GridSearchCV
from sklearn import metrics
class KeyboardInterruptError(Exception): pass
maxval = 1e30
eps = np.sqrt(np.finfo(np.float).eps)
minb=0
class KeyboardInterruptError(Exception): pass
def min_prec_rec(ytest,ypred):
prec, rec, thr = metrics.precision_recall_curve(ytest,ypred)
diff = np.abs(prec-rec)
idx = np.argmin(diff)
#print idx,prec[idx],rec[idx],thr[idx]
#print prec,rec
score = np.min([prec[idx],rec[idx]])
return score
def min_prec_rec_v2(ytest,ypred):
prec, rec, thr = metrics.precision_recall_curve(ytest,ypred)
scorevec = np.minimum(prec, rec)
score = np.max(scorevec)
return score
def fit_predict_classification(X,y,Xtest,ytest,scorer_func=min_prec_rec,penalty='l2',param={'C':[1000,100,10,1,0.1,0.01,0.001,0.0001]},scale=1):
#Linear SVC with hinge loss
metric="roc_auc"
print "Shapes:", X.shape,y.shape,Xtest.shape,ytest.shape
#param={'C':[1000,100,10,1,0.1,0.01,0.001,0.0001]}
#param={'C':[0.1]}
#param={'C':[1e-1,1e-2,1e-3,1e-4,1e-5,1e-6,1e-7]}
if metric=="min_prec_recall":
scorer = metrics.make_scorer(scorer_func,greater_is_better=True,needs_threshold=True)
model=GridSearchCV(LogisticRegression(class_weight='balanced',penalty=penalty,intercept_scaling=scale),param,scoring=scorer)
elif metric:
model=GridSearchCV(LogisticRegression(class_weight='balanced',penalty=penalty,intercept_scaling=scale),param,scoring=metric)
model.fit(X,y)
print "Model:LinearSVC, metric:%s, best_param:" %(metric), model.best_params_
print model.grid_scores_
ypred=model.decision_function(Xtest)
return {'ypred': ypred, 'ytest': ytest, 'auprc': metrics.average_precision_score(ytest,ypred), 'roc': metrics.roc_auc_score(ytest,ypred),'model': model.best_estimator_}
#return {'ypred': ypred, 'ytest': ytest, 'auprc': metrics.precision_score(ytest,ypred)}
def fit_predict_classification_pr(X,y,Xtest,ytest):
#Linear SVC with hinge loss
metric="roc_auc_score"
param={'C':[100,10,1,0.1,0.01,0.001,0.0001]}
model=GridSearchCV(svm.LinearSVC(loss='hinge'),param,scoring=metric)
model.fit(X,y)
print "Model:LinearSVC, metric:%s, best_param:" %(metric), model.best_params_
print model.grid_scores_
ypred=model.decision_function(Xtest)
return {'ypred': ypred, 'ytest': ytest, 'auprc': metrics.roc_auc_score(ytest,ypred)}
@jit(nogil=True,cache=True)
def euclidean_proj_nnl1ball(v, s=1):
'''Uses Result from Tandon, Sra paper'''
assert s > 0, "Radius s must be strictly positive (%d <= 0)" % s
n, = v.shape # will raise ValueError if v is not 1-D
# compute the vector of non-negative values
u = v.copy();
u[u<0]=0;
# check if u is already a solution
if u.sum() <= s:
return u
# v is not already a solution: optimum lies on the boundary (norm == s)
# project *u* on the simplex
return euclidean_proj_simplex(u, s=s)
@jit(nogil=True,cache=True)
def euclidean_proj_simplex(v, s=1):
""" Compute the Euclidean projection on a positive simplex
Solves the optimisation problem (using the algorithm from [1]):
min_w 0.5 * || w - v ||_2^2 , s.t. \sum_i w_i = s, w_i >= 0
Parameters
----------
v: (n,) numpy array,
n-dimensional vector to project
s: int, optional, default: 1,
radius of the simplex
Returns
-------
w: (n,) numpy array,
Euclidean projection of v on the simplex
Notes
-----
The complexity of this algorithm is in O(n log(n)) as it involves sorting v.
Better alternatives exist for high-dimensional sparse vectors (cf. [1])
However, this implementation still easily scales to millions of dimensions.
References
----------
[1] Efficient Projections onto the .1-Ball for Learning in High Dimensions
John Duchi, Shai Shalev-Shwartz, Yoram Singer, and Tushar Chandra.
International Conference on Machine Learning (ICML 2008)
http://www.cs.berkeley.edu/~jduchi/projects/DuchiSiShCh08.pdf
"""
assert s > 0, "Radius s must be strictly positive (%d <= 0)" % s
n, = v.shape # will raise ValueError if v is not 1-D
# check if we are already on the simplex
if np.abs(v.sum()-s)<1e-15 and np.alltrue(v >= 0):
# best projection: itself!
return s*v/v.sum()
#floating point error fixes
if (np.abs(v.clip(min=0).sum()-s)<1e-15):
w = v.clip(min=0)
return s*w/w.sum()
# get the array of cumulative sums of a sorted (decreasing) copy of v
u = np.sort(v)[::-1]
cssv = np.cumsum(u)
# get the number of > 0 components of the optimal solution
if (len(np.nonzero(u * np.arange(1, n+1) > (cssv - s))[0])>0):
rho = np.nonzero(u * np.arange(1, n+1) > (cssv - s))[0][-1]
else:
w=np.zeros(n);i=np.argmax(v);w[i]=s
#print "Warning: IndexError in projection, s=%f" %s
return w
if (rho==0):
w=np.zeros(n);i=np.argmax(v);w[i]=s
return w;
# compute the Lagrange multiplier associated to the simplex constraint
theta = (cssv[rho] - s) / (rho + 1.0)
#compute the projection by thresholding v using theta
w = (v - theta).clip(min=0)
if (w.sum()==0):
w=np.zeros(n);i=np.argmax(v);w[i]=s
#print "Warning: w was set to zero, s=%f" %s
return w
#w=(s/np.sum(w))*w
assert np.alltrue(w>=0), np.where(w<0)
return w
@jit(nogil=True,cache=True)
def D_spGauss(w,A,x,b,g=1,A_sum=None,b_sum=None):
f = 0.0
assert sp.isspmatrx_csc(x), 'D defined only for sparse csc X'
xhat = A.dot(w)+b
f = 0.5*(np.sum((xhat[x.indices]-x.data)**2)) + 0.5*(np.sum(xhat**2)-np.sum(xhat[x.indices]**2))
if g:
gradF = (A.T).dot(xhat)-((A[x.indices]).T).dot(x.data)
return f,gradF
else: return f
@jit(nogil=True,cache=True)
def D_spPoisson(w,A,x,b,g=1,A_sum=None,b_sum=None):
f=0.0
assert sp.isspmatrix_csc(x), 'D defined only for sparse X'
xhat_ind=A[x.indices].dot(w)
if (A_sum is None): A_sum=np.sum(A,0)
if np.isscalar(b):
xhat_ind = xhat_ind+b
if b_sum is None: b_sum=b*A.shape[0]
b_ind=b
else:
xhat_ind = xhat_ind+b[x.indices]
if b_sum is None: b_sum=np.sum(b)
b_ind=b[x.indices]
if g:
if (np.any(xhat_ind==0)):
f = maxval
gradF = np.zeros(w.shape)
weps = np.copy(w)
for i in range(len(w)):
#print 'in here print', np.shape(weps)
weps[i] += eps
#print 'in here'
xhateps_ind = A[x.indices].dot(weps)+b_ind
#print 'successfully added bias'
if np.any(xhateps_ind==0):
feps = maxval
else:
feps= A_sum.dot(weps)+b_sum-np.sum(x.data)+np.sum(x.data*np.log(x.data/xhateps_ind))
gradF[i] = (feps-f)/eps
weps[i] -= eps
else:
t = (x.data/xhat_ind)
f = A_sum.dot(w)+b_sum-np.sum(x.data)+np.sum(x.data*np.log(t))
f = min(f,maxval)
gradF = A_sum-A[x.indices].T.dot(t)
return f,gradF
else:
if (np.any(xhat_ind==0)):
f = maxval
else:
t = (x.data/xhat_ind)
f = A_sum.dot(w)+b_sum-np.sum(x.data)+np.sum(x.data*np.log(t))
f = min(f,maxval)
return f
#@jit(nogil=True,cache=True)
def D_spPoissonpar(inargs):
# print "input to loss:", inargs, "kwargs:", kwargs
try:
return D_spPoisson(*inargs[0],**inargs[1])
except KeyboardInterrupt:
raise KeyboardInterruptError()
D={'sparse_gaussian':D_spGauss,'sparse_poisson':D_spPoisson, 'sparse_poisson_par': D_spPoissonpar}
#==================================
def proj_support_bound(v,s_vec=None,lb = 0.0, ub = 1.0):
#print "v:", v, len(v)
#print "svec:", s_vec, len(s_vec)
#print "assert:", len(v)==len(s_vec)
# v: Input vector
# s_vec: Binary vector imposing support constraints
assert lb < ub, "Bound constraints improper!"
if s_vec==None:
s_vec = np.ones(v.shape)
assert len(v)==len(s_vec), "input vector and support vector dimensions do not match!"
assert np.all([(x==0 or x==1) for x in s_vec]), "Function expects s_vec to be a binary vector"
#print "v before", v
v = np.multiply(v,s_vec) # element-wise multiplication
#print "v after", v
#print "svec",s_vec
v[v < lb] = lb
v[v > ub] = ub
#print "v after bound", v
return v
def soft_thres(v,s=1):
assert s > 0, "Radius s must be strictly positive (%d <= 0)" % s
n, = v.shape # will raise ValueError if v is not 1-D
# check if we are already on the simplex
u=v.copy()
u[u<0]=0.0
return np.clip(u-s,0.0,None)
def euclidean_proj_simplex_k(v, k=None):
'''Tasos's method'''
n, = v.shape # will raise ValueError if v is not 1-D
if (k==None):
k=n
vid=np.argsort(v)[::-1][:k];
w=np.zeros(v.shape)
tau=(1.0/k)*(np.sum(v[vid])-1)
w[vid]=(v[vid]-tau).clip(min=0)
return w
def proj_support_bound(v,s_vec=None,lb = 0.0, ub = 1.0):
#print "v:", v, len(v)
#print "svec:", s_vec, len(s_vec)
#print "assert:", len(v)==len(s_vec)
# v: Input vector
# s_vec: Binary vector imposing support constraints
assert lb < ub, "Bound constraints improper!"
if s_vec==None:
s_vec = np.ones(v.shape)
assert len(v)==len(s_vec), "input vector and support vector dimensions do not match!"
assert np.all([(x==0 or x==1) for x in s_vec]), "Function expects s_vec to be a binary vector"
#print "v before", v
v = np.multiply(v,s_vec) # element-wise multiplication
#print "v after", v
#print "svec",s_vec
v[v < lb] = lb
v[v > ub] = ub
#print "v after bound", v
return v
def soft_thres(v,s=1):
assert s > 0, "Radius s must be strictly positive (%d <= 0)" % s
n, = v.shape # will raise ValueError if v is not 1-D
# check if we are already on the simplex
u=v.copy()
u[u<0]=0.0
return np.clip(u-s,0.0,None)
def euclidean_proj_simplex_k(v, k=None):
'''Tasos's method'''
n, = v.shape # will raise ValueError if v is not 1-D
if (k==None):
k=n
vid=np.argsort(v)[::-1][:k];
w=np.zeros(v.shape)
tau=(1.0/k)*(np.sum(v[vid])-1)
w[vid]=(v[vid]-tau).clip(min=0)
return w