Ejemplo n.º 1
0
    def connectWidgets(self):
        svr = SVR()
        svr.kernel = 'rbf'
        svr.degree = 3
        svr.gamma = 'auto'
        svr.coef0 = 0.0
        svr.tol = 1e-3
        svr.C = 1.0
        svr.epsilon = 0.1
        svr.shrinking = True
        svr.cache_size = 200
        svr.verbose = False
        svr.max_iter = -1

        self.cDoubleSpinBox.setValue(svr.C)
        self.epsilonDoubleSpinBox.setValue(svr.epsilon)
        self.defaultComboItem(self.kernelComboBox, svr.kernel)
        self.degreeSpinBox.setValue(svr.degree)
        self.defaultComboItem(self.gammaComboBox, svr.gamma)
        self.coeff0DoubleSpinBox.setValue(svr.coef0)
        self.shrinkingCheckBox.setChecked(svr.shrinking)
        self.toleranceDoubleSpinBox.setValue(svr.tol)
        self.cacheSizeSpinBox.setValue(svr.cache_size)
        self.verboseCheckBox.setChecked(svr.verbose)
        self.maxIterationsSpinBox.setValue(svr.max_iter)
Ejemplo n.º 2
0
    def trainsvr (self, train, trainlabel, seed, Cmin, Cmax, numC, rmin, rmax, numr, degree=3,\
                  verbose = 1, method = 'roc_auc', rad_stat =2):
        C_range = np.logspace(Cmin, Cmax, num=numC, base=2, endpoint=True)
        gamma_range = np.logspace(rmin, rmax, num=numr, base=2, endpoint=True)

        scr = SVR(kernel=seed)
        #        mean_score=[]
        df_C_gamma = pd.DataFrame({'gamma_range': gamma_range})
        #        df_this = DataFrame({'gamma_range':gamma_range})
        count = 0
        for C in C_range:
            score_C = []
            #            score_C_this = []
            count = count + 1
            for gamma in gamma_range:

                scr.C = C
                scr.gamma = gamma
                scr.degree = degree
                scr.random_state = rad_stat
                this_scores = cross_val_score(scr, train, trainlabel, scoring=method, cv=10, n_jobs=-1 \
                                              )

                score_C.append(np.mean(this_scores))

            #score_C_this.append(np.mean(this_scores))
            if verbose == 1:
                print(np.mean(score_C))
                print("%r cycle finished, %r left" % (count, numC - count))
            df_C_gamma[C] = score_C
            #df_this[C] = score_C_this

        return df_C_gamma
    def train_regress (self, train, trainlabel, seed, Cmin, Cmax, numC, rmin, rmax, numr, degree=3, method = 'rrmse', rad_stat =2):
        C_range=np.logspace(Cmin, Cmax, num=numC, base=2,endpoint= True)
        gamma_range=np.logspace(rmin, rmax, num=numr, base=2,endpoint= True)
        
        svc = SVR(kernel=seed)
#        mean_score=[]
        df_C_gamma= DataFrame({'gamma_range':gamma_range})
#        df_this = DataFrame({'gamma_range':gamma_range})
        count = 0 
        for C in C_range:    
            score_C=[]    
#            score_C_this = []
            count=count+1
            for gamma in gamma_range: 
                svc.epsilon = 0.00001                 
     
                svc.C = C
                svc.gamma = gamma
                svc.degree = degree
                svc.random_state = rad_stat
                this_scores = cross_val_score(svc, train, trainlabel, scoring=method, cv=10, n_jobs=-1 \
                                              )
                
                score_C.append(np.mean(this_scores))                                      

               #score_C_this.append(np.mean(this_scores))
            print (np.mean(score_C) )
            print ("%r cycle finished, %r left" %(count, numC-count))
            df_C_gamma[C]= score_C
            #df_this[C] = score_C_this        
        
        return df_C_gamma 
Ejemplo n.º 4
0
def train_svr(x_train: np.ndarray, y_train: np.ndarray,
              **kwargs) -> (SVR, dict):
    mdl = SVR(**kwargs)
    mdl.degree = 2

    should_grid_search = False
    param_grid = {}

    if 'C' not in kwargs:
        should_grid_search = True
        param_grid['C'] = [0.1, 1, 10, 100, 250]
    if 'gamma' not in kwargs:
        should_grid_search = True
        param_grid['gamma'] = [1e-5, 1e-4, 1e-3, 0.01, 0.1, 0.25]
    if 'kernel' not in kwargs:
        should_grid_search = True
        param_grid['kernel'] = ['rbf', 'linear', 'poly']

    if should_grid_search:
        param_grid['degree'] = [2]

        logger.info(f'Performing grid search. Using param_grid: {param_grid} '
                    f'This may take a while...')
        gs = GridSearchCV(estimator=mdl, param_grid=param_grid)
        gs.fit(x_train, y_train)
        mdl = gs.best_estimator_

    mdl.fit(x_train, y_train)
    return mdl, get_model_params(model_name='svr', model=mdl)
    def train_regress(self,
                      train,
                      trainlabel,
                      seed,
                      Cmin,
                      Cmax,
                      numC,
                      rmin,
                      rmax,
                      numr,
                      degree=3,
                      method='rrmse',
                      rad_stat=2):
        C_range = np.logspace(Cmin, Cmax, num=numC, base=2, endpoint=True)
        gamma_range = np.logspace(rmin, rmax, num=numr, base=2, endpoint=True)

        svc = SVR(kernel=seed)
        #        mean_score=[]
        df_C_gamma = DataFrame({'gamma_range': gamma_range})
        #        df_this = DataFrame({'gamma_range':gamma_range})
        count = 0
        for C in C_range:
            score_C = []
            #            score_C_this = []
            count = count + 1
            for gamma in gamma_range:
                svc.epsilon = 0.00001

                svc.C = C
                svc.gamma = gamma
                svc.degree = degree
                svc.random_state = rad_stat
                this_scores = cross_val_score(svc, train, trainlabel, scoring=method, cv=10, n_jobs=-1 \
                                              )

                score_C.append(np.mean(this_scores))

            #score_C_this.append(np.mean(this_scores))
            print(np.mean(score_C))
            print("%r cycle finished, %r left" % (count, numC - count))
            df_C_gamma[C] = score_C
            #df_this[C] = score_C_this

        return df_C_gamma
Ejemplo n.º 6
0
    def connectWidgets(self):
        svr = SVR()
        svr.kernel = 'rbf'
        svr.degree = 3
        svr.gamma = 'auto'
        svr.coef0 = 0.0
        svr.tol = 1e-3
        svr.C = 1.0
        svr.epsilon = 0.1
        svr.shrinking = True
        svr.cache_size = 200
        svr.verbose = False
        svr.max_iter = -1

        self.cLineEdit.setText(str(svr.C))
        self.epsilonLineEdit.setText(str(svr.epsilon))
        self.kernel_list.setCurrentItem(self.kernel_list.findItems('Radial Basis Function', QtCore.Qt.MatchExactly)[0])
        self.degreeLineEdit.setText(str(svr.degree))
        self.coeff0LineEdit.setText(str(svr.coef0))
        self.shrinking_list.setCurrentItem(self.shrinking_list.findItems(str(svr.shrinking), QtCore.Qt.MatchExactly)[0])
        self.toleranceLineEdit.setText(str(svr.tol))
        self.maxIterationsLineEdit.setText(str(svr.max_iter))