def fit(self, Xl_onl, Xu_onl, patClassId_onl, Xl_off, Xu_off,
            patClassId_off):
        """
        Input data need to be normalized before using this function
        
        Xl_onl              Input data lower bounds (rows = objects, columns = features) for online learning
        Xu_onl              Input data upper bounds (rows = objects, columns = features) for online learning
        patClassId_onl      Input data class labels (crisp) for online learning
        
        Xl_off              Input data lower bounds (rows = objects, columns = features) for agglomerative learning
        Xu_off              Input data upper bounds (rows = objects, columns = features) for agglomerative learning
        patClassId_off      Input data class labels (crisp) for agglomerative learning
        """

        time_start = time.clock()
        # Perform agglomerative learning
        aggloClassifier = AccelBatchGFMM(self.gamma,
                                         self.teta_agglo,
                                         bthres=self.bthres,
                                         simil=self.simil,
                                         sing=self.sing,
                                         isDraw=self.isDraw,
                                         oper=self.oper,
                                         isNorm=False)
        aggloClassifier.fit(Xl_off, Xu_off, patClassId_off)
        self.offClassifier = Bunch(V=aggloClassifier.V,
                                   W=aggloClassifier.W,
                                   classId=aggloClassifier.classId)

        # Perform online learning
        onlClassifier = OnlineGFMM(self.gamma,
                                   self.teta_onl,
                                   self.teta_onl,
                                   isDraw=self.isDraw,
                                   oper=self.oper,
                                   isNorm=False,
                                   norm_range=[self.loLim, self.hiLim])
        onlClassifier.fit(Xl_onl, Xu_onl, patClassId_onl)
        self.onlClassifier = Bunch(V=onlClassifier.V,
                                   W=onlClassifier.W,
                                   classId=onlClassifier.classId)

        time_end = time.clock()
        self.elapsed_training_time = time_end - time_start

        return self
Example #2
0
 def fit(self, Xl_onl, Xu_onl, patClassId_onl, Xl_off, Xu_off, patClassId_off, typeOfAgglo = 1):
     """
     The input data need to be normalized before using this function
     
     Xl_onl              Input data lower bounds (rows = objects, columns = features) for online learning
     Xu_onl              Input data upper bounds (rows = objects, columns = features) for online learning
     patClassId_onl      Input data class labels (crisp) for online learning
     
     Xl_off              Input data lower bounds (rows = objects, columns = features) for agglomerative learning
     Xu_off              Input data upper bounds (rows = objects, columns = features) for agglomerative learning
     patClassId_off      Input data class labels (crisp) for agglomerative learning
     
     typeOfAgglo         The used type of agglomerative learning algorithms
     """
     
     time_start = time.clock()
     
     # Perform agglomerative learning
     if typeOfAgglo == 1:
         aggloClassifier = AccelBatchGFMM(self.gamma, self.teta_agglo, bthres = self.bthres, simil = self.simil, sing = self.sing, isDraw = self.isDraw, oper = self.oper, isNorm = False)
     elif typeOfAgglo == 2:
         aggloClassifier = BatchGFMMV2(self.gamma, self.teta_agglo, bthres = self.bthres, simil = self.simil, sing = self.sing, isDraw = self.isDraw, oper = self.oper, isNorm = False)
     else:
         aggloClassifier = BatchGFMMV1(self.gamma, self.teta_agglo, bthres = self.bthres, simil = self.simil, sing = self.sing, isDraw = self.isDraw, oper = self.oper, isNorm = False)
         
     aggloClassifier.fit(Xl_off, Xu_off, patClassId_off)
     
     self.V = aggloClassifier.V
     self.W = aggloClassifier.W
     self.classId = aggloClassifier.classId
     
     # Perform online learning
     onlClassifier = OnlineGFMM(self.gamma, self.teta_onl, self.teta_onl, isDraw = self.isDraw, oper = self.oper, isNorm = False, norm_range = [self.loLim, self.hiLim], V = self.V, W = self.W, classId = self.classId)
     # training for online GFMM
     onlClassifier.fit(Xl_onl, Xu_onl, patClassId_onl)
     
     self.V = onlClassifier.V
     self.W = onlClassifier.W
     self.classId = onlClassifier.classId
     
     time_end = time.clock()
     self.elapsed_training_time = time_end - time_start
     
     return self
Example #3
0
    def fit(self, X_l, X_u, patClassId, typeOfAgglo=1):
        """
        Xl              Input data lower bounds (rows = objects, columns = features)
        Xu              Input data upper bounds (rows = objects, columns = features)
        patClassId      Input data class labels (crisp)
        typeOfAgglo     Type of agglomerative learning
                         + 1: Accelerated agglomerative learning AGGLO-2
                         + 2: Full batch learning slower version
                         + 3: Full batch learning faster version
        """
        if self.isNorm == True:
            X_l, X_u = self.dataPreprocessing(X_l, X_u)

        time_start = time.clock()
        # Perform online learning
        onlClassifier = OnlineGFMM(self.gamma,
                                   self.teta_onl,
                                   self.teta_onl,
                                   isDraw=self.isDraw,
                                   oper=self.oper,
                                   isNorm=False,
                                   norm_range=[self.loLim, self.hiLim],
                                   V=self.V,
                                   W=self.W,
                                   classId=self.classId)
        # training for online GFMM
        onlClassifier.fit(X_l, X_u, patClassId)

        self.V = onlClassifier.V
        self.W = onlClassifier.W
        self.classId = onlClassifier.classId
        # print('No. hyperboxes after online learning:', len(self.classId))
        self.num_hyperbox_after_online = len(self.classId)

        # Perform agglomerative learning
        if typeOfAgglo == 1:
            aggloClassifier = AccelBatchGFMM(self.gamma,
                                             self.teta_agglo,
                                             bthres=self.bthres,
                                             simil=self.simil,
                                             sing=self.sing,
                                             isDraw=self.isDraw,
                                             oper=self.oper,
                                             isNorm=False)
        elif typeOfAgglo == 2:
            aggloClassifier = BatchGFMMV2(self.gamma,
                                          self.teta_agglo,
                                          bthres=self.bthres,
                                          simil=self.simil,
                                          sing=self.sing,
                                          isDraw=self.isDraw,
                                          oper=self.oper,
                                          isNorm=False)
        else:
            aggloClassifier = BatchGFMMV1(self.gamma,
                                          self.teta_agglo,
                                          bthres=self.bthres,
                                          simil=self.simil,
                                          sing=self.sing,
                                          isDraw=self.isDraw,
                                          oper=self.oper,
                                          isNorm=False)

        aggloClassifier.fit(self.V, self.W, self.classId)

        self.V = aggloClassifier.V
        self.W = aggloClassifier.W
        self.classId = aggloClassifier.classId
        #print('No. hyperboxes after the agglomerative learning:', len(self.classId))
        self.num_hyperbox_after_agglo = len(self.classId)

        time_end = time.clock()
        self.elapsed_training_time = time_end - time_start

        return self
 
 # Read training file
 Xtr, X_tmp, patClassIdTr, pat_tmp = loadDataset(training_file, 1, False)
 # Read testing file
 X_tmp, Xtest, pat_tmp, patClassIdTest = loadDataset(testing_file, 0, False)
 
 teta_save = np.array([])
 numhyperbox_save = np.array([], dtype=np.int64)
 training_time_save = np.array([])
 testing_error_save = np.array([])
 
 teta_start = [0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1]
 
 for teta in teta_start:
     teta_save = np.append(teta_save, teta)
     olnClassifier = OnlineGFMM(gamma = 1, teta = teta, tMin = 0.02, isDraw = False, oper = 'min', isNorm = False)
     olnClassifier.fit(Xtr, Xtr, patClassIdTr)
     training_time_save = np.append(training_time_save, olnClassifier.elapsed_training_time)
     numhyperbox_save = np.append(numhyperbox_save, len(olnClassifier.classId))
     
     result = olnClassifier.predict(Xtest, Xtest, patClassIdTest)
     if result != None:
         numTestSample = Xtest.shape[0]
         err = result.summis / numTestSample
         testing_error_save = np.append(testing_error_save, err)
         
 # save result to file
 data_save = np.hstack((teta_save.reshape(-1, 1), numhyperbox_save.reshape(-1, 1), training_time_save.reshape(-1, 1), testing_error_save.reshape(-1, 1)))
 filename = save_result_folder_path + '\\' + dataset_names[dt] + '.txt'
 
 open(filename, 'w').close() # make existing file empty
            pos_rnd = np.random.permutation(X_full.shape[0])
            X_full_tmp = X_full[pos_rnd]
            classId_full_tmp = classId_full[pos_rnd]

            Xtr_time_i = X_full_tmp[:pivot_pos]
            pathClassIdTr_time_i = classId_full_tmp[:pivot_pos]

            Xtest = X_full_tmp[pivot_pos:]
            patClassIdTest = classId_full_tmp[pivot_pos:]

            numTestSample = Xtest.shape[0]

            # Do online learning
            olnClassifier = OnlineGFMM(gamma=1,
                                       teta=teta,
                                       tMin=teta,
                                       isDraw=False,
                                       oper='min',
                                       isNorm=False)
            olnClassifier.fit(Xtr_time_i, Xtr_time_i, pathClassIdTr_time_i)

            training_time_online_save = np.append(
                training_time_online_save, olnClassifier.elapsed_training_time)
            numhyperbox_online_save = np.append(numhyperbox_online_save,
                                                len(olnClassifier.classId))

            result = olnClassifier.predict(Xtest, Xtest, patClassIdTest)
            if result != None:
                err = result.summis / numTestSample
                testing_error_online_save = np.append(
                    testing_error_online_save, err)
         testingData = fold4Data
         validationData = fold3Data
         trainingLabel = np.hstack((fold1Label, fold2Label))
         testingLabel = fold4Label
         validationLabel = fold3Label
         numTestSample = len(testingLabel)
         tetaOnlnGFMM = teta_onln_f4[dt]
         tetaAGGLO2 = teta_agglo2_f4[dt]
         tetaFMNN = teta_fmnn_f4[dt]
         tetaEFMNN = teta_efmnn_f4[dt]
         tetaKNEFMNN = teta_knefmnn_f4[dt]
         kOpt = k_knefmnn_f4[dt]
         
 
     # online GFMM
     olnClassifier = OnlineGFMM(gamma = 1, teta = tetaOnlnGFMM, tMin = tetaOnlnGFMM, isDraw = False, oper = 'min', isNorm = False)
     olnClassifier.fit(trainingData, trainingData, trainingLabel)
     
     numhyperbox_before_prun_online_gfmm_save = np.append(numhyperbox_before_prun_online_gfmm_save, len(olnClassifier.classId)) 
     result = olnClassifier.predict(testingData, testingData, testingLabel)
     if result != None:
         err = np.round(result.summis / numTestSample * 100, 3)
         testing_error_before_prun_online_gfmm_save = np.append(testing_error_before_prun_online_gfmm_save, err)            
     
     start_t = time.perf_counter()
     olnClassifier.pruning_val(validationData, validationData, validationLabel)
     end_t = time.perf_counter()
     
     training_time_online_gfmm_save = np.append(training_time_online_gfmm_save, olnClassifier.elapsed_training_time + (end_t - start_t))
     numhyperbox_online_gfmm_save = np.append(numhyperbox_online_gfmm_save, len(olnClassifier.classId))