Ejemplo n.º 1
0
    def compute_base_kernel_vectors(self):

        ## column kernel
        xdata0 = np.copy(self.xdata)

        xdata=mmr_normalization( \
          self.norm_col.ilocal,-1, \
          xdata0,np.array([]),0)[0]
        xdata = xdata.T
        m = xdata.shape[0]
        xdata=mmr_normalization( \
          -1,self.norm_col.iscale, \
          xdata,np.array([]),0)[0]

        ikern_type = 1  ## =0 linear, =1 cond prob
        if ikern_type == 0:
            K = np.dot(xdata, xdata.T)
        elif ikern_type == 1:
            K0 = np.dot(xdata0.T, xdata0) + 1
            K0 = K0 / m
            ## K=np.dot(K0,K0)/np.outer(np.diag(K0),np.diag(K0))
            K = np.dot(K0, K0) * np.outer(np.diag(K0), np.diag(K0))
            dK = np.sqrt(np.diag(K))
            K = K / np.outer(dK, dK)
        elif ikern_type == 2:  # Tanimoto
            K = tanimoto(np.dot(xdata0.T, xdata0))

        Knl = kernel_eval_kernel((K, None), None, None, self.param_col)[0]

        icenter = 0
        if icenter >= 0:
            (Ka, aKa) = mmr_geometricmedian_ker(Knl)
            Ka1 = np.outer(Ka, np.ones(m))
            Knl_center = Knl + aKa - Ka1 - Ka1.T
            ineg = np.where(np.diag(Knl_center) < 0)[0]
            for i in ineg:
                Knl_center[i, i] = 0.0
            self.kernel_col = Knl_center
        else:
            self.kernel_col = Knl

        iscale = -1
        ## normalize kernel items
        if iscale >= 0:
            dK = np.sqrt(np.diag(self.kernel_col))
            idK = np.where(dK == 0)[0]
            for i in idK:
                self.kernel_col[i, i] = 1.0
                dK[i] = 1.0
            self.kernel_col = self.kernel_col / np.outer(dK, dK)

        if np.isnan(self.kernel_col).any()==True and \
           np.isfinite(self.kernel_col).all()!=True:
            print('!!!!!')

        return
Ejemplo n.º 2
0
  def compute_base_kernel_vectors(self):

    ## column kernel
    xdata0=np.copy(self.xdata)

    xdata=mmr_normalization( \
      self.norm_col.ilocal,-1, \
      xdata0,np.array([]),0)[0]
    xdata=xdata.T
    m=xdata.shape[0]
    xdata=mmr_normalization( \
      -1,self.norm_col.iscale, \
      xdata,np.array([]),0)[0]

    ikern_type=1          ## =0 linear, =1 cond prob
    if ikern_type==0:
      K=np.dot(xdata,xdata.T)
    elif ikern_type==1:
      K0=np.dot(xdata0.T,xdata0)+1
      K0=K0/m
      ## K=np.dot(K0,K0)/np.outer(np.diag(K0),np.diag(K0))
      K=np.dot(K0,K0)*np.outer(np.diag(K0),np.diag(K0))
      dK=np.sqrt(np.diag(K))
      K=K/np.outer(dK,dK)
    elif ikern_type==2:   # Tanimoto
      K=tanimoto(np.dot(xdata0.T,xdata0))

    Knl=kernel_eval_kernel((K,None),None,None,self.param_col)[0]

    icenter=0
    if icenter>=0:
      (Ka,aKa)=mmr_geometricmedian_ker(Knl)
      Ka1=np.outer(Ka,np.ones(m))
      Knl_center=Knl+aKa-Ka1-Ka1.T
      ineg=np.where(np.diag(Knl_center)<0)[0]
      for i in ineg:
        Knl_center[i,i]=0.0
      self.kernel_col=Knl_center
    else:
      self.kernel_col=Knl
      
    iscale=-1
    ## normalize kernel items
    if iscale>=0:
      dK=np.sqrt(np.diag(self.kernel_col))
      idK=np.where(dK==0)[0]
      for i in idK:
        self.kernel_col[i,i]=1.0
        dK[i]=1.0
      self.kernel_col=self.kernel_col/np.outer(dK,dK)

    if np.isnan(self.kernel_col).any()==True and \
       np.isfinite(self.kernel_col).all()!=True:
      print('!!!!!')
    
    return
Ejemplo n.º 3
0
    def compute_kernel(self, itrain, itest):

        if self.ifeature == 0:
            if self.icategory == 0:
                (self.XTrainNorm,self.XTestNorm)= \
                              mmr_normalization(self.norm.ilocal,self.norm.iscale, \
                                                self.data[itrain], \
                                                self.data[itest],0)[:2]

                if self.Y0 is not None:
                    self.Y0Norm=mmr_normalization(self.norm.ilocal,self.norm.iscale, \
                                                  self.Y0,None,0)[0]

                if self.prekernel_params.kernel_type in (0, 1, 2, 3):
                    xdata = [self.XTrainNorm, None]
                    (self.K,self.d1,self.d2)=kernel_eval_kernel(xdata,None,None, \
                                                    self.kernel_params)

                    if self.Y0 is None:
                        if len(itest) > 0:
                            xdata = [self.XTrainNorm, self.XTestNorm]
                            (self.Kcross,self.d1c,self.d2c)=kernel_eval_kernel(xdata,None, \
                                                                             None, \
                                                        self.kernel_params)
                    else:
                        xdata = [self.XTrainNorm, self.Y0Norm]
                        (self.Kcross,self.d1c,self.d2c)=kernel_eval_kernel(xdata,None, \
                                                                           None, \
                                                      self.kernel_params)
                elif self.prekernel_params.kernel_type in (5, ):
                    (m, n) = self.data.shape
                    datanorm = np.zeros((m, n))
                    datanorm[itrain] = self.XTrainNorm
                    datanorm[itest] = self.XTestNorm
                    K = gaussian_process_type_kernel(
                        datanorm, self.prekernel_params.ipar1)
                    (self.K,self.Kcross,self.d1,self.d2,self.d1c,self.d2c)= \
                        kernel_localscale(K,None,itrain,itest,self.kernel_params, \
                                       ilocal=self.norm.ilocal,iscale=self.norm.iscale)

        else:  ## implicit feature given by kernel
            if self.Kraw is not None:  ## Tanimoto
                kdata = self.Kraw
                kdataY0 = self.Krawcross
            else:
                kdata = self.data
                kdataY0 = None
            (self.K,self.Kcross,self.d1,self.d2,self.d1c,self.d2c)= \
                    kernel_localscale(kdata,kdataY0,itrain,itest, \
                                      self.kernel_params, \
                                   ilocal=self.norm.ilocal,iscale=self.norm.iscale)

        return
Ejemplo n.º 4
0
  def compute_kernel(self,itrain,itest):

    if self.ifeature==0:  
      if self.icategory==0:
        (self.XTrainNorm,self.XTestNorm)= \
                      mmr_normalization(self.norm.ilocal,self.norm.iscale, \
                                        self.data[itrain], \
                                        self.data[itest],0)[:2]

        if self.Y0 is not None:
          self.Y0Norm=mmr_normalization(self.norm.ilocal,self.norm.iscale, \
                                        self.Y0,None,0)[0]
          
        if self.prekernel_params.kernel_type in (0,1,2,3):
          xdata=[self.XTrainNorm,None]
          (self.K,self.d1,self.d2)=kernel_eval_kernel(xdata,None,None, \
                                          self.kernel_params)

          if self.Y0 is  None:
            if len(itest)>0:
              xdata=[self.XTrainNorm,self.XTestNorm]
              (self.Kcross,self.d1c,self.d2c)=kernel_eval_kernel(xdata,None, \
                                                               None, \
                                          self.kernel_params)
          else:
            xdata=[self.XTrainNorm,self.Y0Norm]
            (self.Kcross,self.d1c,self.d2c)=kernel_eval_kernel(xdata,None, \
                                                               None, \
                                          self.kernel_params)
        elif self.prekernel_params.kernel_type in (5,):
          (m,n)=self.data.shape
          datanorm=np.zeros((m,n))
          datanorm[itrain]=self.XTrainNorm
          datanorm[itest]=self.XTestNorm
          K=gaussian_process_type_kernel(datanorm,self.prekernel_params.ipar1)
          (self.K,self.Kcross,self.d1,self.d2,self.d1c,self.d2c)= \
              kernel_localscale(K,None,itrain,itest,self.kernel_params, \
                             ilocal=self.norm.ilocal,iscale=self.norm.iscale)
        
          
    else: ## implicit feature given by kernel
      if self.Kraw is not None:  ## Tanimoto
        kdata=self.Kraw
        kdataY0=self.Krawcross
      else:
        kdata=self.data
        kdataY0=None
      (self.K,self.Kcross,self.d1,self.d2,self.d1c,self.d2c)= \
              kernel_localscale(kdata,kdataY0,itrain,itest, \
                                self.kernel_params, \
                             ilocal=self.norm.ilocal,iscale=self.norm.iscale)
      
    return
Ejemplo n.º 5
0
    def compute_base_vectors(self):

        ## column kernel
        xdata = self.xdata.T

        xdata=mmr_normalization( \
          self.norm_col.ilocal,self.norm_col.iscale, \
          xdata,np.array([]),0)[0]
        K = np.dot(xdata, xdata.T)

        Knl = kernel_eval_kernel((K, ), None, None, self.param_col)[0]
        self.kernel_col = Knl
        ## normalize kernel items
        dK = np.sqrt(np.diag(self.kernel_col))
        idK = np.where(dK == 0)[0]
        for i in idK:
            self.kernel_col[i, i] = 1.0
            dK[i] = 1.0
        self.kernel_col = self.kernel_col / np.outer(dK, dK)

        (v, w) = sp_lin.svd(self.kernel_col)[:2]

        self.base_vectors = v * np.outer(np.ones(self.ncol), np.sqrt(w))

        return
Ejemplo n.º 6
0
  def compute_base_vectors(self):

    ## column kernel
    xdata=self.xdata.T

    xdata=mmr_normalization( \
      self.norm_col.ilocal,self.norm_col.iscale, \
      xdata,np.array([]),0)[0]
    K=np.dot(xdata,xdata.T)
    
    Knl=kernel_eval_kernel((K,),None,None,self.param_col)[0]
    self.kernel_col=Knl
    ## normalize kernel items
    dK=np.sqrt(np.diag(self.kernel_col))
    idK=np.where(dK==0)[0]
    for i in idK:
      self.kernel_col[i,i]=1.0
      dK[i]=1.0
    self.kernel_col=self.kernel_col/np.outer(dK,dK)
    
    (v,w)=sp_lin.svd(self.kernel_col)[:2]

    self.base_vectors=v*np.outer(np.ones(self.ncol),np.sqrt(w))
    
    return
Ejemplo n.º 7
0
    def get_train_norm(self, itrain):

        if self.XTrainNorm is None:
            (self.XTrainNorm,self.XTestNorm)= \
                    mmr_normalization(self.norm.ilocal,self.norm.iscale, \
                                      self.data[self.itrain], \
                                      self.data[self.itest],0)[:2]
        return (self.XTrainNorm)
Ejemplo n.º 8
0
  def get_train_norm(self,itrain):

    if self.XTrainNorm is None:
      (self.XTrainNorm,self.XTestNorm)= \
              mmr_normalization(self.norm.ilocal,self.norm.iscale, \
                                self.data[self.itrain], \
                                self.data[self.itest],0)[:2]
    return(self.XTrainNorm)
Ejemplo n.º 9
0
    def get_Y0_norm(self, itrain):

        if self.Y0Norm is None:
            if self.Y0 is None:
                if self.XTrainNorm is None:
                    self.XTrainNorm= \
                          mmr_normalization(self.norm.ilocal,self.norm.iscale, \
                                            self.data[itrain],None,0)[0]
                Y0Norm = self.XTrainNorm
            else:
                Y0Norm=mmr_normalization(self.norm.ilocal,self.norm.iscale, \
                                          self.Y0,None,0)[0]
        ## if self.Y0Norm.shape[0]!=itrain.shape[0]:
        ##   print('!!!!',self.Y0Norm.shape[0],itrain.shape[0], \
        ##         self.itrain.shape[0],self.itest.shape[0])

        return (Y0Norm)
Ejemplo n.º 10
0
  def get_Y0_norm(self,itrain):

    if self.Y0Norm is None:
      if self.Y0 is None:
        if self.XTrainNorm is None:
          self.XTrainNorm= \
                mmr_normalization(self.norm.ilocal,self.norm.iscale, \
                                  self.data[itrain],None,0)[0]
        Y0Norm=self.XTrainNorm
      else:
        Y0Norm=mmr_normalization(self.norm.ilocal,self.norm.iscale, \
                                  self.Y0,None,0)[0]
    ## if self.Y0Norm.shape[0]!=itrain.shape[0]:
    ##   print('!!!!',self.Y0Norm.shape[0],itrain.shape[0], \
    ##         self.itrain.shape[0],self.itest.shape[0])

    return(Y0Norm)
Ejemplo n.º 11
0
    def edge_prekernel(self, X, params_spec):

        (m, n) = X.shape  ## xdataraw[0]
        dalpha = {}
        dxy = {}
        ialpha = 1
        ny = 2
        nitem = 3

        self.ddiag = np.zeros(m)
        csolver = mmr_solver_cls.cls_mmr_solver()

        ## read the edges
        for iview in range(m):
            xy = X[iview]
            nxy = len(xy) // nitem
            xy = xy[:nxy * nitem]
            xy = xy.reshape((nxy, nitem))
            ixy = np.where(xy[:, 0] > 0)[0]
            xy = xy[ixy]
            x = xy[:, :2]  ## edge position
            ya = xy[:, 2]  ## edge angle

            y = np.vstack((np.cos(ya), np.sin(ya))).T
            ## normalize the locations
            ilocal = self.norm.ilocal
            iscale = self.norm.iscale
            (xnorm, xdummy, opar) = mmr_normalization(ilocal, iscale, x, None,
                                                      0)
            ## xnorm=x
            self.dxy[iview] = [xnorm, y]
            if ialpha == 1:
                Ky = np.dot(y, y.T)
                xdata = [xnorm, None]
                (Kx, dx1, dx2) = kernel_eval_kernel(xdata, None, None,
                                                    params_spec)
                C = 1
                D = 0
                xalpha = csolver.mmr_solver(Kx, Ky, C, D, 1, 1, 1, 1)
                self.dalpha[iview] = xalpha
            else:
                xalpha = np.ones(len(y)) / len(y)
                self.dalpha[iview] = xalpha

            self.ddiag[iview] = np.dot(xalpha, np.dot(Kx * Ky, xalpha))

        return
Ejemplo n.º 12
0
    def compute_kernel(self, itrain, itest):

        (self.XTrainNorm,self.XTestNorm)= \
                        mmr_normalization(self.norm.ilocal,self.norm.iscale, \
                                          self.data[self.itrain], \
                                          self.data[self.itest],0)[:2]

        csubspace_kernel=cls_kernel_subspace(self.get_train(itrain), \
                                             None, \
                                             self.prekernel_params, \
                                             self.norm, \
                                             self.kernel_params)
        csubspace_kernel.compute_base_kernel_vectors()
        csubspace_kernel.compute_kernel_kernel_row(itrates=0)
        self.K = csubspace_kernel.kernel_row
        self.d1 = np.diag(self.K)
        self.d2 = self.d1
Ejemplo n.º 13
0
  def compute_kernel(self,itrain,itest):

    (self.XTrainNorm,self.XTestNorm)= \
                    mmr_normalization(self.norm.ilocal,self.norm.iscale, \
                                      self.data[self.itrain], \
                                      self.data[self.itest],0)[:2]

    csubspace_kernel=cls_kernel_subspace(self.get_train(itrain), \
                                         None, \
                                         self.prekernel_params, \
                                         self.norm, \
                                         self.kernel_params)
    csubspace_kernel.compute_base_kernel_vectors()
    csubspace_kernel.compute_kernel_kernel_row(itrates=0)
    self.K=csubspace_kernel.kernel_row
    self.d1=np.diag(self.K)
    self.d2=self.d1
Ejemplo n.º 14
0
  def edge_prekernel(self,X,params_spec):

    (m,n)=X.shape   ## xdataraw[0]
    dalpha={}
    dxy={}
    ialpha=1
    ny=2
    nitem=3

    self.ddiag=np.zeros(m)
    csolver=mmr_solver_cls.cls_mmr_solver()

    ## read the edges
    for iview in range(m):
      xy=X[iview]
      nxy=len(xy)//nitem
      xy=xy[:nxy*nitem]
      xy=xy.reshape((nxy,nitem))
      ixy=np.where(xy[:,0]>0)[0]
      xy=xy[ixy]
      x=xy[:,:2]  ## edge position
      ya=xy[:,2]   ## edge angle

      y=np.vstack((np.cos(ya),np.sin(ya))).T
      ## normalize the locations
      ilocal=self.norm.ilocal
      iscale=self.norm.iscale
      (xnorm,xdummy,opar)=mmr_normalization(ilocal,iscale,x,None,0)
      ## xnorm=x
      self.dxy[iview]=[xnorm,y]
      if ialpha==1:
        Ky=np.dot(y,y.T)
        xdata=[xnorm,None]
        (Kx,dx1,dx2)=kernel_eval_kernel(xdata,None,None,params_spec)
        C=1
        D=0
        xalpha=csolver.mmr_solver(Kx,Ky,C,D,1,1,1,1)
        self.dalpha[iview]=xalpha
      else:
        xalpha=np.ones(len(y))/len(y)
        self.dalpha[iview]=xalpha
      
      self.ddiag[iview]=np.dot(xalpha,np.dot(Kx*Ky,xalpha))

    return