def loadResources(self):
     """
     Loads the resources from the previously set resource pool.
     
     @raise Exception: when some of the resources required by the learner is not available in the ResourcePool object.
     """
     AbstractIterativeLearner.loadResources(self)
     X = self.resource_pool['train_features']
     if isinstance(X, sp.base.spmatrix):
         self.X = X.todense()
     else:
         self.X = X
     self.X = self.X.T
     self.Y = self.resource_pool['train_labels']
     #Number of training examples
     self.size = self.Y.shape[0]
     if self.resource_pool.has_key('bias'):
         self.bias = float(self.resource_pool['bias'])
     else:
         self.bias = 0.
     if self.resource_pool.has_key('measure'):
         self.measure = None
     #    self.measure = self.resource_pool['measure']
     else:
         self.measure = None
     self.results = {}
Exemplo n.º 2
0
 def loadResources(self):
     AbstractIterativeLearner.loadResources(self)
     if data_sources.TRAIN_LABELS in self.resource_pool:
         Y = self.resource_pool[data_sources.TRAIN_LABELS]
         self.Y = array_tools.as_labelmatrix(Y)
         #Number of training examples
         self.size = Y.shape[0]
         if Y.shape[1] > 1:
             raise Exception('CGRankRLS does not currently work in multi-label mode')
         self.learn_from_labels = True
         if (data_sources.VALIDATION_FEATURES in self.resource_pool) and (data_sources.VALIDATION_LABELS in self.resource_pool):
             validation_X = self.resource_pool[data_sources.VALIDATION_FEATURES]
             validation_Y = self.resource_pool[data_sources.VALIDATION_LABELS]
             if data_sources.VALIDATION_QIDS in self.resource_pool:
                 validation_qids = self.resource_pool[data_sources.VALIDATION_QIDS]
             else:
                 validation_qids = None
             self.callbackfun = EarlyStopCB(validation_X, validation_Y, validation_qids)
     elif data_sources.TRAIN_PREFERENCES in self.resource_pool:
         self.pairs = self.resource_pool[data_sources.TRAIN_PREFERENCES]
         self.learn_from_labels = False
     else:
         raise Exception('Neither labels nor preference information found')
     X = self.resource_pool[data_sources.TRAIN_FEATURES]
     self.X = csc_matrix(X.T)
     self.bias = 0.
     if data_sources.TRAIN_QIDS in self.resource_pool:
         qids = self.resource_pool[data_sources.TRAIN_QIDS]
         self.setQids(qids)
     self.results = {}
Exemplo n.º 3
0
 def loadResources(self):
     AbstractIterativeLearner.loadResources(self)
     AbstractSupervisedLearner.loadResources(self)
     #Y = self.resource_pool[data_sources.TRAIN_LABELS]
     #self.setLabels(Y)
     X = self.resource_pool[data_sources.TRAIN_FEATURES]
     self.X = csc_matrix(X.T)
     if self.resource_pool.has_key('bias'):
         self.bias = float(self.resource_pool['bias'])
         if self.bias != 0.:
             bias_slice = sqrt(self.bias)*np.mat(ones((1,self.X.shape[1]),dtype=np.float64))
             self.X = sparse.vstack([self.X,bias_slice]).tocsc()
     else:
         self.bias = 0.
     self.X_csr = self.X.tocsr()
     if (data_sources.VALIDATION_FEATURES in self.resource_pool) and (data_sources.VALIDATION_LABELS in self.resource_pool):
         validation_X = self.resource_pool[data_sources.VALIDATION_FEATURES]
         validation_Y = self.resource_pool[data_sources.VALIDATION_LABELS]
         self.callbackfun = EarlyStopCB(validation_X, validation_Y)
    def loadResources(self):
        """
        Loads the resources from the previously set resource pool.
        
        @raise Exception: when some of the resources required by the learner is not available in the ResourcePool object.
        """
        AbstractIterativeLearner.loadResources(self)
        AbstractSupervisedLearner.loadResources(self)

        self.Y = Y
        # Number of training examples
        self.size = Y.shape[0]
        if not Y.shape[1] == 1:
            raise Exception(
                "GreedyRLS currently supports only one output at a time. The output matrix is now of shape "
                + str(Y.shape)
                + "."
            )

        X = self.resource_pool["train_features"]
        if isinstance(X, scipy.sparse.base.spmatrix):
            self.X = X.todense()
        else:
            self.X = X
Exemplo n.º 5
0
 def loadResources(self):
     AbstractSvdLearner.loadResources(self)
     AbstractIterativeLearner.loadResources(self)
     
     self.constraint = 0
     if not self.resource_pool.has_key('number_of_clusters'):
         raise Exception("Parameter 'number_of_clusters' must be given.")
     self.labelcount = int(self.resource_pool['number_of_clusters'])
     
     if self.labelcount == 2:
         self.oneclass = True
     else:
         self.oneclass = False
     
     if self.resource_pool.has_key(data_sources.TRAIN_LABELS):
         Y_orig = self.resource_pool[data_sources.TRAIN_LABELS]
         if Y_orig.shape[1] == 1:
             self.Y = mat(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 = - mat(ones((self.size, 1), dtype = int32))
     self.classcounts = mat(zeros((self.labelcount, 1), 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 self.resource_pool.has_key('fixed_indices'):
         self.fixedindices = self.resource_pool['fixed_indices']