Skip to content

Commit cd56141

Browse files
authoredDec 18, 2017
Create optimizers.py
1 parent 13f3de3 commit cd56141

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
 

‎optimizers.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#performs newton-raphson optimization
2+
def NGD(X,resp,Ker,classifier,lambd,tol,epochs):
3+
import numpy as np
4+
N,K=resp.shape
5+
sv=np.repeat(True,N)
6+
betahat=np.array(np.repeat(0,N+1),dtype=float)
7+
intercept=0
8+
t=0
9+
if (classifier=="SVM") & (K==1) :
10+
sv_new=np.random.choice([True,False],N,replace=True)
11+
while (t<10) & np.any(sv_new != sv):
12+
t+=1
13+
if t>1: sv=sv_new
14+
betahat=np.array(np.repeat(0,N),dtype=float)
15+
intercept=0
16+
Ksv=Ker[sv,:]
17+
Ksv=Ksv[:,sv]
18+
Ysv=np.insert(resp[sv],0,0).T
19+
N1=sum(sv)
20+
IntMat=Ksv+lambd*np.diag(np.ones(N1))
21+
IntMat=np.hstack((np.ones(N1).reshape(N1,1),IntMat))
22+
IntMat=np.vstack((np.ones(N1+1).reshape(1,N1+1),IntMat))
23+
IntMat[0,0]=0
24+
bsv=np.dot(np.linalg.inv(IntMat),Ysv)
25+
intercept=bsv[0]
26+
betahat[sv]=bsv[1:]
27+
yhat=np.dot(Ker,betahat)+intercept
28+
yhat=np.squeeze(np.asarray(yhat))
29+
sv_new=np.squeeze(np.asarray(resp))*yhat<1
30+
betahat=np.insert(betahat,0,intercept)
31+
32+
return {'betahat':betahat,'n_iter':t,'sv':sv_new}

0 commit comments

Comments
 (0)
Please sign in to comment.