-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
79 lines (72 loc) · 2.53 KB
/
utils.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
#converts the response vector into adequate matrix form
def transformResponse(response,classifier,tt='Regression'):
import numpy as np
N=len(response)
response=np.reshape(response,(N,1))
respMat=np.array([])
classes=np.array([])
if (tt=='Regression'): respMat=np.asmatrix(response)
if (tt=='Classification'):
K=len(np.unique(response))
classes=np.unique(response)
if (K==2) & (classifier=='Softmax'): respMat=np.asmatrix(response)
if (K==2) & (classifier=='SVM'):
if np.any(response==0): response=2*(response-0.5)
respMat=np.asmatrix(response)
return {'Response':response,'respMat':respMat,'Classes':classes}
#converts algorithm output into vector form similar to initial response
def transformOutput(f_x,CL,tt='Regression'):
import numpy as np
N,K=f_x.shape
yhat=np.array([])
if tt=='Regression': yhat=f_x
if tt=='Classification':
if K==1:
fx=np.squeeze(np.asarray(f_x))
yhat=np.repeat(min(CL),N)
yhat[fx>0]=max(CL)
if K>2: yhat=np.apply_along_axis(lambda x:CL[np.argmax(x)],1,f_x)
yhat=yhat.reshape((N,1))
return {'yhat':yhat,'yhatMat':f_x}
def Softmax(X):
import numpy as np
X=np.matrix(X)
eps=1e-15
Eps=1-eps
M=np.max(X)
prod=np.apply_along_axis(lambda x: np.exp(x-M)/np.sum(np.exp(X-M),axis=1),0,X)
prod[prod>Eps]=Eps
prod[prod<eps]=eps
return(prod)
def buildKernel(ker,X,Z,degree=3,gamm=0.1,pred=False):
import numpy as np
H=np.matrix([])
S=np.matrix([])
if ker=='linear': degree=1
if ker in ['linear','polynomial']: kermat=np.power((np.dot(Z,X.T)+1),degree)-1
if ker=='gaussian': kermat=np.apply_along_axis(lambda x:np.exp(-gamm*np.sum(np.power(Z-x,2),1)).T,1,X)
if(pred is False):
N,_=kermat.shape
H=np.hstack((np.ones(N).reshape(N,1),kermat))
S=np.vstack((np.zeros(N),kermat))
S=np.hstack((np.zeros(N+1).reshape(N+1,1),S))
return {'H':H,'KerMat':kermat,'S':S}
#model predictions
def predictRKHS(Input,rkhs):
import numpy as np
X=np.asmatrix(Input)
args=rkhs['rkhsargs']
gamm=args['gamm']
degree=args['degree']
Xtrain=args['X']
kerName=args['kername']
opType=args['opType']
CL=args['classes']
out=buildKernel(kerName,Xtrain,X,degree,gamm,pred=True)
Ker=out['KerMat']
alphahat=rkhs['beta']
beta0=alphahat[0]
fx=beta0+np.dot(Ker,np.matrix(alphahat[1:]).T)
out=transformOutput(fx,CL,opType)
yhat=out['yhat']
return yhat