def mvm_test(self): """ select the potential test method wrapper around the possible test evaluations of different losses inputs: cOptDual object containing the optimal values of the dual variables outputs: cPredict object containing the prediction results """ cPredict = base.cls_predict() itest_method = 0 # =0 orig if itest_method == 0: # matrix completition test cPredict.Zrow = mvm_test_orig.mvm_test_orig(self, self.dual.alpha) return (cPredict)
def mvm_test(self): """ select the potential test method wrapper around the possible test evaluations of different losses inputs: cOptDual object containing the optimal values of the dual variables outputs: cPredict object containing the prediction results """ cPredict=base.cls_predict() itest_method=0 # =0 orig if itest_method==0: # matrix completition test cPredict.Zrow=mvm_test_orig.mvm_test_orig(self,self.dual.alpha) return(cPredict)
def test_dynamic_prog(cDataTest,cOptDual): (mtrain,mtest)=cDataTest.KXcross.shape ndim=cDataTest.YTrainNorm.shape[1] cPredict=mmr_base_classes.cls_predict() cPredict.zPred=np.zeros((mtest,ndim)) beta=cDataTest.KXcross*np.outer(cOptDual.alpha,np.ones(mtest)) beta=beta.T ## inverse covariance ## ymean=np.mean(cDataTest.YTrainNorm,axis=0) Y=cDataTest.YTrainNorm ## Yn=Y-np.outer(np.ones(mtrain),ymean) ## Ycov=np.dot(Yn.T,Yn)/mtrain ## Ycovinv=np_lin.pinv(Ycov,rcond=1e-2)/(2*(params.output.ipar1**2)) Ycovinv=np.eye(ndim)/(2*(cDataTest.kernel_params.ipar1**2)) CY=np.dot(Ycovinv,Y.T) cy0=np.sum(Y*CY.T,axis=1) y0=np.zeros(ndim) ## y0c=np.zeros(ndim) ## e=np.ones(mtrain) rtime=time.time() ntime=5 xdynstack0=np.zeros((ntime,ndim)) xdynstack1=np.zeros((ntime,ndim,ndim)) xdynstack2=np.zeros((ntime,ndim,mtrain)) for it in range(mtest): ## setup of the dynamic programming ## load time 0 f=np.dot(np.exp(-cy0),beta[it]) for i in range(ntime): for j in range(ndim): xdynstack0[i,j]=f xdynstack1[i,j,:]=y0 xdynstack2[i,j,:]=cy0 ## print('>>>',f) for t in range(0,ntime-1): if t==0: nitem=1 else: nitem=ndim for i1 in range(nitem): ## print(t,i1) f=xdynstack0[t,i1] y0=xdynstack1[t,i1,:] cy0=xdynstack2[t,i1,:] fmax=0 y0c=np.dot(Ycovinv,y0) for i2 in range(ndim): cy_new=cy0+Ycovinv[i2,i2]+y0c[i2]-CY[i2] fnew=np.dot(np.exp(-cy_new),beta[it]) if fnew>xdynstack0[t+1,i2]: ## print(t,i1,i2,fnew) y=np.copy(y0) y[i2]=1 xdynstack0[t+1,i2]=fnew xdynstack1[t+1,i2,:]=y xdynstack2[t+1,i2,:]=cy_new ## else: ## y=np.copy(y0) ## dynstack[t+1][i2]=(f,y,y0c) if fnew>fmax: fmax=fnew ## print(f,fmax) ## print(t,i1) ## find the best one fbest=0 ibest=-1 for i in range(ndim): if fbest<xdynstack0[ntime-1,i]: fbest=xdynstack0[ntime-1,i] ibest=i cPredict.zPred[it,:]=xdynstack1[ntime-1,ibest,:] if it%10==0: print(it,(time.time()-rtime)/(it+1)) return(cPredict)
def mmr_test(self,cOptDual,itraindata=1): if self.YKernel.kernel_params.kernel_type>0: self.itestmode=0 if itraindata==0: itest=self.itrain else: itest=self.itest if self.iperceptron in (0,2): KXcross=mmr_kernel(self,self.itrain,itest,ioutput=0, \ itraintest=1, itraindata=itraindata, \ itensor=self.kmode)[0] KYcross=mmr_kernel(self,self.itrain,self.itrain,ioutput=1, \ itraintest=1,itraindata=itraindata)[0] mtest=KXcross.shape[1] cPredict=base.cls_predict() if self.YKernel.ifeature==0: if self.YKernel.kernel_params.kernel_type==0: YTrain=self.YKernel.get_train_norm(self.itrain) Zw0=np.dot(YTrain.T,(np.outer(cOptDual.alpha, \ np.ones(mtest))*KXcross)) if self.csolver.ibias_estim==1: Zw0=Zw0+np.outer(cOptDual.bias,np.ones(mtest)) xnorm=np.sqrt(np.sum(Zw0**2,axis=0)) xnorm=xnorm+(xnorm==0) Zw=Zw0/np.outer(np.ones(Zw0.shape[0]),xnorm) Y0=self.YKernel.get_Y0_norm(self.itrain) cPredict.ZTest=np.dot(Y0,Zw) else: Zw0=np.dot(KYcross.T, \ (np.outer(cOptDual.alpha,np.ones(mtest))*KXcross)) cPredict.ZTest=Zw0 if self.csolver.ibias_estim==1: cPredict.bias=cOptDual.bias else: cPredict.bias=0 Zw=Zw0 cPredict.Zw0=Zw0.T cPredict.Zw=Zw.T if self.itestmode in (0,2,3,4): Y0=self.YKernel.get_Y0(self.itrain) cPredict.zPremax=cPredict.ZTest.max(0) cPredict.iPredCat=cPredict.ZTest.argmax(0) cPredict.zPred=Y0[cPredict.iPredCat] if self.itestmode==2 and self.YKernel.kernel_params.kernel_type==0: cPredict.knnPredCat=np.argsort \ (-cPredict.ZTest,axis=0)[:self.testknn,:] elif self.itestmode==3: n=YTrain.shape[1] nsort=5 for i in range(mtest): cPredict.zPred[i,:]=np.zeros(n) iones=np.argsort(-Zw[:,i])[:nsort] for j in range(nsort-1): if Zw[iones[j],i]<0.04 or \ Zw[iones[j],i]>10.0*Zw[iones[j+1],i]: iones=iones[:(j+1)] break cPredict.zPred[i,iones]=1 elif self.itestmode==1: YTrain=self.YKernel.get_train(self.itrain) ndim=YTrain.shape[1] i2boro=np.argsort(-Zw0,axis=0)[0:self.testknn,:].T cPredict.zPred=np.zeros((mtest,ndim)) for i in range(mtest): zsum=np.zeros(ndim) for j in range(self.testknn): zsum+=YTrain[i2boro[i,j]] cPredict.zPred[i,:]=1*(zsum>=self.testknn/2) elif self.YKernel.ifeature==1: Y0=self.YKernel.get_Y0(self.itrain) cPredict.ZTest=np.dot(KYcross.T,(np.outer(cOptDual.alpha, \ np.ones(mtest))*KXcross)) cPredict.zPremax=cPredict.ZTest.max(0) cPredict.iPredCat=cPredict.ZTest.argmax(0) cPredict.zPred=Y0[cPredict.iPredCat] if self.itestmode==2: cPredict.knnPredCat=np.argsort \ (-cPredict.ZTest,axis=0)[:self.testknn,:] elif self.iperceptron==1: if itraindata==0: X=self.XKernel[0].get_train_norm(self.itrain) for i in range(1,len(self.XKernel)): X=np.hstack((X,self.XKernel[i].get_train_norm(self.itrain))) else: X=self.XKernel[0].get_test_norm(itest) for i in range(1,len(self.XKernel)): X=np.hstack((X,self.XKernel[i].get_test_norm(itest))) cPredict=base.cls_predict() xmean=np.mean(X) mtest=X.shape[0] Zw=np.dot(cOptDual.W, \ np.hstack((X,np.ones((mtest,1))*xmean)).T) cPredict.ZTest=np.dot(YTrain,Zw) cPredict.zPremax=cPredict.ZTest.max(0) cPredict.iPredCat=cPredict.ZTest.argmax(0) cPredict.zPred=YTrain[cPredict.iPredCat] if self.itestmode==2: cPredict.knnPredCat=np.argsort(-cPredict.ZTest, \ axis=0)[:self.testknn,:] return(cPredict)
def mmr_test(self, cOptDual, itraindata=1): if self.YKernel.kernel_params.kernel_type > 0: self.itestmode = 0 if itraindata == 0: itest = self.itrain else: itest = self.itest if self.iperceptron in (0, 2): KXcross=mmr_kernel(self,self.itrain,itest,ioutput=0, \ itraintest=1, itraindata=itraindata, \ itensor=self.kmode)[0] KYcross=mmr_kernel(self,self.itrain,self.itrain,ioutput=1, \ itraintest=1,itraindata=itraindata)[0] mtest = KXcross.shape[1] cPredict = base.cls_predict() if self.YKernel.ifeature == 0: if self.YKernel.kernel_params.kernel_type == 0: YTrain = self.YKernel.get_train_norm(self.itrain) Zw0=np.dot(YTrain.T,(np.outer(cOptDual.alpha, \ np.ones(mtest))*KXcross)) if self.csolver.ibias_estim == 1: Zw0 = Zw0 + np.outer(cOptDual.bias, np.ones(mtest)) xnorm = np.sqrt(np.sum(Zw0**2, axis=0)) xnorm = xnorm + (xnorm == 0) Zw = Zw0 / np.outer(np.ones(Zw0.shape[0]), xnorm) Y0 = self.YKernel.get_Y0_norm(self.itrain) cPredict.ZTest = np.dot(Y0, Zw) else: Zw0=np.dot(KYcross.T, \ (np.outer(cOptDual.alpha,np.ones(mtest))*KXcross)) cPredict.ZTest = Zw0 if self.csolver.ibias_estim == 1: cPredict.bias = cOptDual.bias else: cPredict.bias = 0 Zw = Zw0 cPredict.Zw0 = Zw0.T cPredict.Zw = Zw.T if self.itestmode in (0, 2, 3, 4): Y0 = self.YKernel.get_Y0(self.itrain) cPredict.zPremax = cPredict.ZTest.max(0) cPredict.iPredCat = cPredict.ZTest.argmax(0) cPredict.zPred = Y0[cPredict.iPredCat] if self.itestmode == 2 and self.YKernel.kernel_params.kernel_type == 0: cPredict.knnPredCat=np.argsort \ (-cPredict.ZTest,axis=0)[:self.testknn,:] elif self.itestmode == 3: n = YTrain.shape[1] nsort = 5 for i in range(mtest): cPredict.zPred[i, :] = np.zeros(n) iones = np.argsort(-Zw[:, i])[:nsort] for j in range(nsort - 1): if Zw[iones[j],i]<0.04 or \ Zw[iones[j],i]>10.0*Zw[iones[j+1],i]: iones = iones[:(j + 1)] break cPredict.zPred[i, iones] = 1 elif self.itestmode == 1: YTrain = self.YKernel.get_train(self.itrain) ndim = YTrain.shape[1] i2boro = np.argsort(-Zw0, axis=0)[0:self.testknn, :].T cPredict.zPred = np.zeros((mtest, ndim)) for i in range(mtest): zsum = np.zeros(ndim) for j in range(self.testknn): zsum += YTrain[i2boro[i, j]] cPredict.zPred[i, :] = 1 * (zsum >= self.testknn / 2) elif self.YKernel.ifeature == 1: Y0 = self.YKernel.get_Y0(self.itrain) cPredict.ZTest=np.dot(KYcross.T,(np.outer(cOptDual.alpha, \ np.ones(mtest))*KXcross)) cPredict.zPremax = cPredict.ZTest.max(0) cPredict.iPredCat = cPredict.ZTest.argmax(0) cPredict.zPred = Y0[cPredict.iPredCat] if self.itestmode == 2: cPredict.knnPredCat=np.argsort \ (-cPredict.ZTest,axis=0)[:self.testknn,:] elif self.iperceptron == 1: if itraindata == 0: X = self.XKernel[0].get_train_norm(self.itrain) for i in range(1, len(self.XKernel)): X = np.hstack( (X, self.XKernel[i].get_train_norm(self.itrain))) else: X = self.XKernel[0].get_test_norm(itest) for i in range(1, len(self.XKernel)): X = np.hstack((X, self.XKernel[i].get_test_norm(itest))) cPredict = base.cls_predict() xmean = np.mean(X) mtest = X.shape[0] Zw=np.dot(cOptDual.W, \ np.hstack((X,np.ones((mtest,1))*xmean)).T) cPredict.ZTest = np.dot(YTrain, Zw) cPredict.zPremax = cPredict.ZTest.max(0) cPredict.iPredCat = cPredict.ZTest.argmax(0) cPredict.zPred = YTrain[cPredict.iPredCat] if self.itestmode == 2: cPredict.knnPredCat=np.argsort(-cPredict.ZTest, \ axis=0)[:self.testknn,:] return (cPredict)
def test_dynamic_prog(cDataTest, cOptDual): (mtrain, mtest) = cDataTest.KXcross.shape ndim = cDataTest.YTrainNorm.shape[1] cPredict = mmr_base_classes.cls_predict() cPredict.zPred = np.zeros((mtest, ndim)) beta = cDataTest.KXcross * np.outer(cOptDual.alpha, np.ones(mtest)) beta = beta.T ## inverse covariance ## ymean=np.mean(cDataTest.YTrainNorm,axis=0) Y = cDataTest.YTrainNorm ## Yn=Y-np.outer(np.ones(mtrain),ymean) ## Ycov=np.dot(Yn.T,Yn)/mtrain ## Ycovinv=np_lin.pinv(Ycov,rcond=1e-2)/(2*(params.output.ipar1**2)) Ycovinv = np.eye(ndim) / (2 * (cDataTest.kernel_params.ipar1**2)) CY = np.dot(Ycovinv, Y.T) cy0 = np.sum(Y * CY.T, axis=1) y0 = np.zeros(ndim) ## y0c=np.zeros(ndim) ## e=np.ones(mtrain) rtime = time.time() ntime = 5 xdynstack0 = np.zeros((ntime, ndim)) xdynstack1 = np.zeros((ntime, ndim, ndim)) xdynstack2 = np.zeros((ntime, ndim, mtrain)) for it in range(mtest): ## setup of the dynamic programming ## load time 0 f = np.dot(np.exp(-cy0), beta[it]) for i in range(ntime): for j in range(ndim): xdynstack0[i, j] = f xdynstack1[i, j, :] = y0 xdynstack2[i, j, :] = cy0 ## print('>>>',f) for t in range(0, ntime - 1): if t == 0: nitem = 1 else: nitem = ndim for i1 in range(nitem): ## print(t,i1) f = xdynstack0[t, i1] y0 = xdynstack1[t, i1, :] cy0 = xdynstack2[t, i1, :] fmax = 0 y0c = np.dot(Ycovinv, y0) for i2 in range(ndim): cy_new = cy0 + Ycovinv[i2, i2] + y0c[i2] - CY[i2] fnew = np.dot(np.exp(-cy_new), beta[it]) if fnew > xdynstack0[t + 1, i2]: ## print(t,i1,i2,fnew) y = np.copy(y0) y[i2] = 1 xdynstack0[t + 1, i2] = fnew xdynstack1[t + 1, i2, :] = y xdynstack2[t + 1, i2, :] = cy_new ## else: ## y=np.copy(y0) ## dynstack[t+1][i2]=(f,y,y0c) if fnew > fmax: fmax = fnew ## print(f,fmax) ## print(t,i1) ## find the best one fbest = 0 ibest = -1 for i in range(ndim): if fbest < xdynstack0[ntime - 1, i]: fbest = xdynstack0[ntime - 1, i] ibest = i cPredict.zPred[it, :] = xdynstack1[ntime - 1, ibest, :] if it % 10 == 0: print(it, (time.time() - rtime) / (it + 1)) return (cPredict)