コード例 #1
0
def ramdom_kernels_combination(kernel_indexs,samples,classes,rbf_par,poly_par,scorer):
   kernels= []
   for indexes in kernel_indexs:    
      choice = np.random.randint(3, size=1)[0]
      if choice ==0:
         kernels.append( Kinterface(data=x_train[:,indexes], kernel=linear_kernel))
      elif choice ==1:
         #print(rbf_par)
         length_of_param1 = len(rbf_par["gamma"])
         # print(rbf_par["gamma"])
         # print(np.random.randint(length_of_param1, size=1)[0])
         K = Kinterface(data=x_train[:,indexes], kernel=rbf_kernel,kernel_args={"gamma": rbf_par["gamma"][np.random.randint(length_of_param1, size=1)[0]]})
         kernels.append(K)
      else:
         length_of_param1 = len(poly_par["degree"])
         K = Kinterface(data=x_train[:,indexes], kernel=poly_kernel,kernel_args={"degree": poly_par["degree"][np.random.randint(length_of_param1, size=1)[0]]})
         kernels.append(K)
   #mu = [random.randrange(0,1) for i in range(40)]
   model = Alignf(typ="convex")
   model.fit(kernels, classes.values)
   model.mu  # kernel weights (convex combination)
   mu = model.mu
   #print("numbers:" +str(mu))

   combined_k = lambda x,y: \
      sum([mu[i]*kernels[i](x[:,kernel_indexs[i]],y[:,kernel_indexs[i]]) for i in range(len(kernels))])
   return combined_k
コード例 #2
0
def createKernelCombination(kernel_indexs,samples,classes,rbf_par,poly_par,scorer):
   kernels= []
   for indexes in kernel_indexs:
      svm = tunning_svm(samples[:,indexes],classes,rbf_par,poly_par,scorer)
      kernel = svm.get_params()["kernel"]
      if kernel=="linear":
         kernels.append( Kinterface(data=x_train[:,indexes], kernel=linear_kernel))
      elif kernel =="rbf":
         gamma=svm.get_params()["gamma"]
         K = Kinterface(data=x_train[:,indexes], kernel=rbf_kernel,kernel_args={"gamma": gamma})
         kernels.append(K)
      else:
         degree=svm.get_params()["degree"]
         coef0 =degree=svm.get_params()["coef0"]
         K = Kinterface(data=x_train[:,indexes], kernel=poly_kernel,kernel_args={"degree": degree})
         kernels.append(K)

   model = Alignf(typ="convex")
   model.fit(kernels, classes.values)
   model.mu  # kernel weights (convex combination)
   mu = model.mu
   print(mu)

   combined_k = lambda x,y: \
      sum([mu[i]*kernels[i](x[:,kernel_indexs[i]],y[:,kernel_indexs[i]]) for i in range(len(kernels))])
   return combined_k