예제 #1
0
 def getKM(self, X):
     """Returns the kernel matrix between the basis vectors and X.
     
     Parameters
     ----------
     X : {array-like, sparse matrix}, shape = [n_samples, n_features]
     
     Returns
     -------
     K : array, shape = [n_samples, n_bvectors]
         kernel matrix
     """
     X = array_tools.as_2d_array(X, True)
     test_X = X
     degree, coef0, gamma = self.degree, self.coef0, self.gamma
     if sp.issparse(test_X):
         test_X = array_tools.spmat_resize(test_X, self.train_X.shape[1])
     else:
         test_X = array_tools.as_dense_matrix(test_X)
     train_X = self.train_X
     K = array_tools.as_array(train_X * test_X.T)
     K *= gamma
     K += coef0
     K = K**degree
     return K.T
예제 #2
0
파일: predictor.py 프로젝트: godeva/RanKit
 def __init__(self, W, b=0.):
     self.W = np.squeeze(array_tools.as_array(W))
     if self.W.ndim == 0:
         self.W = self.W.reshape(1)
     #special case: 1-dimensional multi-task predictor
     if W.shape[0] == 1 and W.shape[1] > 0:
         self.W = self.W.reshape(W.shape[0], W.shape[1])
     self.b = np.squeeze(np.array(b))
예제 #3
0
 def getKM(self, X):
     """Returns the kernel matrix between the basis vectors and X.
     
     Parameters
     ----------
     X : {array-like, sparse matrix}, shape = [n_samples, n_features]
     
     Returns
     -------
     K : array, shape = [n_samples, n_bvectors]
         kernel matrix
     """
     X = array_tools.as_2d_array(X, True)
     test_X = X
     if sp.issparse(test_X):
         test_X = array_tools.spmat_resize(test_X, self.train_X.shape[1])
     else:
         test_X = array_tools.as_dense_matrix(test_X)
     train_X = self.train_X
     K = (train_X * test_X.T).astype(float)
     K = array_tools.as_array(K)
     if self.bias != 0:
         K += self.bias
     return K.T
예제 #4
0
 def __init__(self, X, regparam=1.0, number_of_clusters=2, kernel='LinearKernel', basis_vectors=None, Y = None, fixed_indices=None, callback=None,  **kwargs):
     kwargs['X'] = X 
     kwargs['kernel'] = kernel
     if basis_vectors is not None:
         kwargs['basis_vectors'] = basis_vectors
     self.svdad = adapter.createSVDAdapter(**kwargs)
     self.svals = np.mat(self.svdad.svals)
     self.svecs = self.svdad.rsvecs
     self.regparam = regparam
     self.constraint = 0
     #if not kwargs.has_key('number_of_clusters'):
     #    raise Exception("Parameter 'number_of_clusters' must be given.")
     self.labelcount = number_of_clusters
     if self.labelcount == 2:
         self.oneclass = True
     else:
         self.oneclass = False
     self.callbackfun = callback
     if Y is not None:
         Y_orig = array_tools.as_array(Y)
         #if Y_orig.shape[1] == 1:
         if len(Y_orig.shape) == 1:
             self.Y = np.zeros((Y_orig.shape[0], 2))
             self.Y[:, 0] = Y_orig
             self.Y[:, 1] = - Y_orig
             self.oneclass = True
         else:
             self.Y = Y_orig.copy()
             self.oneclass = False
         for i in range(self.Y.shape[0]):
             largestind = 0
             largestval = self.Y[i, 0]
             for j in range(self.Y.shape[1]):
                 if self.Y[i, j] > largestval:
                     largestind = j
                     largestval = self.Y[i, j]
                 self.Y[i, j] = -1.
             self.Y[i, largestind] = 1.
     else:
         size = self.svecs.shape[0]
         ysize = self.labelcount
         if self.labelcount is None: self.labelcount = 2
         self.Y = RandomLabelSource(size, ysize).readLabels()
     self.size = self.Y.shape[0]
     self.labelcount = self.Y.shape[1]
     self.classvec = - np.ones((self.size), dtype = np.int32)
     self.classcounts = np.zeros((self.labelcount), dtype = np.int32)
     for i in range(self.size):
         clazzind = 0
         largestlabel = self.Y[i, 0]
         for j in range(self.labelcount):
             if self.Y[i, j] > largestlabel:
                 largestlabel = self.Y[i, j]
                 clazzind = j
         self.classvec[i] = clazzind
         self.classcounts[clazzind] = self.classcounts[clazzind] + 1
     
     self.svecs_list = []
     for i in range(self.size):
         self.svecs_list.append(self.svecs[i].T)
     self.fixedindices = []
     if fixed_indices is not None:
         self.fixedindices = fixed_indices
     else:
         self.fixedindices = []
     self.results = {}
     self.solve(self.regparam)
예제 #5
0
파일: predictor.py 프로젝트: godeva/RanKit
 def __init__(self, A, kernel):
     self.kernel = kernel
     self.dim = kernel.train_X.shape[1]
     self.A = A
     self.A = np.squeeze(array_tools.as_array(self.A))