예제 #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
 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
     """
     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
     if self.bias != 0:
         K += self.bias
     return K.T
예제 #3
0
파일: predictor.py 프로젝트: aatapa/RLScore
 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))
예제 #4
0
 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))
예제 #5
0
 def __init__(self, A, kernel):
     self.kernel = kernel
     #newbasis = list(set(nonz[0].tolist()))
     self.A = A
     #if len(newbasis) != A.shape[0]:
     #    self.A = A.todense()[newbasis]
     #    newpool = rpool.copy()
     #    newpool['basis_vectors'] = newbasis
     #    self.kernel = kernel.__class__.createKernel(**newpool)
     self.A = np.squeeze(array_tools.as_array(self.A))
예제 #6
0
 def __init__(self, A, kernel):
     self.kernel = kernel
     # newbasis = list(set(nonz[0].tolist()))
     self.A = A
     # if len(newbasis) != A.shape[0]:
     #    self.A = A.todense()[newbasis]
     #    newpool = rpool.copy()
     #    newpool['basis_vectors'] = newbasis
     #    self.kernel = kernel.__class__.createKernel(**newpool)
     self.A = np.squeeze(array_tools.as_array(self.A))
예제 #7
0
파일: model.py 프로젝트: peknau/RLScore
 def __init__(self, A, kernel):
     self.kernel = kernel
     nonz = A.nonzero()
     #newbasis = list(set(nonz[0].tolist()))
     self.A = A
     #if len(newbasis) != A.shape[0]:
     #    self.A = A.todense()[newbasis]
     #    newpool = rpool.copy()
     #    newpool[data_sources.BASIS_VECTORS] = newbasis
     #    self.kernel = kernel.__class__.createKernel(**newpool)
     self.A = array_tools.as_array(self.A)
예제 #8
0
 def __init__(self, W, b=0.):
     """Initializes a primal model
     @param W: coefficients of the linear model, one column per task
     @type W: numpy matrix
     @param b: bias of the model, one column per task
     @type b: numpy matrix
     """
     #self.W = array_tools.as_dense_matrix(W)
     #print type(W), W.shape
     self.W = np.squeeze(array_tools.as_array(W))
     #print type(self.W), self.W.shape
     self.b = np.squeeze(np.array(b))
예제 #9
0
 def __init__(self, W, b=0.0):
     """Initializes a primal model
     @param W: coefficients of the linear model, one column per task
     @type W: numpy matrix
     @param b: bias of the model, one column per task
     @type b: numpy matrix
     """
     # self.W = array_tools.as_dense_matrix(W)
     # print type(W), W.shape
     self.W = np.squeeze(array_tools.as_array(W))
     # print type(self.W), self.W.shape
     self.b = np.squeeze(np.array(b))
예제 #10
0
파일: model.py 프로젝트: peknau/RLScore
    def predict(self, X):
        """Computes predictions for test examples.

        Parameters
        ----------
        X: {array-like, sparse matrix}, shape = [n_samples, n_features]
            test data matrix
        
        Returns
        ----------
        P: array, shape = [n_samples, n_tasks]
            predictions
        """
        K = self.kernel.getKM(X)
        P = np.dot(K, self.A)
        P = array_tools.as_array(P)
        return P
예제 #11
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
     """
     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
     K = array_tools.as_array(K)
     if self.bias != 0:
         K += self.bias
     return K.T
예제 #12
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
     K = array_tools.as_array(K)
     if self.bias != 0:
         K += self.bias
     return K.T
예제 #13
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)
예제 #14
0
파일: model.py 프로젝트: peknau/RLScore
 def predictFromPool(self, rpool):
     """Makes real-valued predictions for new examples"""
     self.kernel.getKM(rpool[data_sources.PREDICTION_FEATURES])
     P = np.dot(K, self.A)
     P = array_tools.as_array(P)
     return P
예제 #15
0
파일: mmc.py 프로젝트: lenovor/RLScore
    def __init__(self, **kwargs):
        self.svdad = creators.createSVDAdapter(**kwargs)
        self.svals = self.svdad.svals
        self.svecs = self.svdad.rsvecs
        self.regparam = float(kwargs["regparam"])
        self.constraint = 0
        #if not kwargs.has_key('number_of_clusters'):
        #    raise Exception("Parameter 'number_of_clusters' must be given.")
        if kwargs.has_key("number_of_clusters"):
            self.labelcount = int(kwargs["number_of_clusters"])
        else:
            self.labelcount = 2
        if self.labelcount == 2:
            self.oneclass = True
        else:
            self.oneclass = False
        if kwargs.has_key("callback"):
            self.callbackfun = kwargs["callback"]
        else:
            self.callbackfun = None
        if kwargs.has_key("train_labels"):
            train_labels = kwargs["train_labels"]
        else:
            train_labels = None
        if train_labels != None:
            #Y_orig = array_tools.as_labelmatrix(train_labels)
            Y_orig = array_tools.as_array(train_labels)
            #if Y_orig.shape[1] == 1:
            if len(Y_orig.shape) == 1:
                self.Y = 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 == None: self.labelcount = 2
            self.Y = RandomLabelSource(size, ysize).readLabels()
        self.size = self.Y.shape[0]
        self.labelcount = self.Y.shape[1]
        self.classvec = -ones((self.size), dtype=int32)
        self.classcounts = zeros((self.labelcount), dtype=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 kwargs.has_key("fixed_indices"):
            self.fixedindices = kwargs["fixed_indices"]
        else:
            self.fixedindices = []
        self.results = {}
예제 #16
0
파일: mmc.py 프로젝트: StevenLOL/RLScore
 def __init__(self, **kwargs):
     self.svdad = creators.createSVDAdapter(**kwargs)
     self.svals = self.svdad.svals
     self.svecs = self.svdad.rsvecs
     self.regparam = float(kwargs["regparam"])
     self.constraint = 0
     #if not kwargs.has_key('number_of_clusters'):
     #    raise Exception("Parameter 'number_of_clusters' must be given.")
     if kwargs.has_key("number_of_clusters"):
         self.labelcount = int(kwargs["number_of_clusters"])
     else:
         self.labelcount = 2
     if self.labelcount == 2:
         self.oneclass = True
     else:
         self.oneclass = False
     if kwargs.has_key("callback"):
         self.callbackfun = kwargs["callback"]
     else:
         self.callbackfun = None
     if kwargs.has_key("train_labels"):
         train_labels = kwargs["train_labels"]
     else:
         train_labels = None
     if train_labels != None:
         #Y_orig = array_tools.as_labelmatrix(train_labels)
         Y_orig = array_tools.as_array(train_labels)
         #if Y_orig.shape[1] == 1:
         if len(Y_orig.shape) == 1:
             self.Y = 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 == None: self.labelcount = 2
         self.Y = RandomLabelSource(size, ysize).readLabels()
     self.size = self.Y.shape[0]
     self.labelcount = self.Y.shape[1]
     self.classvec = - ones((self.size), dtype = int32)
     self.classcounts = zeros((self.labelcount), dtype = 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 kwargs.has_key("fixed_indices"):
         self.fixedindices = kwargs["fixed_indices"]
     else:
         self.fixedindices = []
     self.results = {}
예제 #17
0
파일: mmc.py 프로젝트: disc5/RLScore
    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 != None:
            kwargs['basis_vectors'] = basis_vectors
        self.svdad = creators.createSVDAdapter(**kwargs)
        self.svals = 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 != 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 == 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 != None:
            self.fixedindices = fixed_indices
        else:
            self.fixedindices = []
        self.results = {}
        self.solve(self.regparam)
예제 #18
0
 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))
예제 #19
0
파일: predictor.py 프로젝트: aatapa/RLScore
 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))
예제 #20
0
 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)
     self.b = np.squeeze(np.array(b))