当前位置: 首页>>代码示例>>Python>>正文


Python xgboost.cv方法代码示例

本文整理汇总了Python中xgboost.cv方法的典型用法代码示例。如果您正苦于以下问题:Python xgboost.cv方法的具体用法?Python xgboost.cv怎么用?Python xgboost.cv使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在xgboost的用法示例。


在下文中一共展示了xgboost.cv方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: run_grid_search

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import cv [as 别名]
def run_grid_search(self):
        """
        This method is called by derived class to start grid search process
        """
        features,labels,cv_folds = self.getFeaturesLabel()
        dtrain_cv  = xgb.DMatrix(features, label= labels,feature_names=features.columns)
           
        parameter_iterable = self.__get_param_iterable(self.__get_param_grid())  
        kwargs = self.get_learning_params()
        for param in parameter_iterable:
            logging.info("used parameters: {}".format(param))
            bst = xgb.cv(param, dtrain_cv, folds=cv_folds,**kwargs)
            self.__add_to_resultset(param, bst)

        self.__disp_result() 
        return 
开发者ID:LevinJ,项目名称:Supply-demand-forecasting,代码行数:18,代码来源:xgbbasemodel.py

示例2: test_xgboost_pruning_callback_cv

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import cv [as 别名]
def test_xgboost_pruning_callback_cv():
    # type: () -> None

    def objective(trial):
        # type: (optuna.trial.Trial) -> float

        dtrain = xgb.DMatrix(np.ones((2, 1)), label=[1.0, 1.0])
        params = {
            "silent": 1,
            "objective": "binary:logistic",
        }

        pruning_callback = optuna.integration.XGBoostPruningCallback(trial, "test-error")
        xgb.cv(params, dtrain, callbacks=[pruning_callback], nfold=2)
        return 1.0

    study = optuna.create_study(pruner=DeterministicPruner(True))
    study.optimize(objective, n_trials=1)
    assert study.trials[0].state == optuna.trial.TrialState.PRUNED

    study = optuna.create_study(pruner=DeterministicPruner(False))
    study.optimize(objective, n_trials=1)
    assert study.trials[0].state == optuna.trial.TrialState.COMPLETE
    assert study.trials[0].value == 1.0 
开发者ID:optuna,项目名称:optuna,代码行数:26,代码来源:test_xgboost.py

示例3: GBM

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import cv [as 别名]
def GBM(self, argsDict):
        max_depth = argsDict["max_depth"] + 10
        subsample = argsDict["subsample"] * 0.1 + 0.5
        #n_estimators = argsDict['n_estimators'] * 5 + 50
        learning_rate = argsDict["learning_rate"] * 0.02 + 0.12
        #gamma = argsDict["gamma"] * 0.1
        #min_child_weight = argsDict["min_child_weight"] + 1
        
        print("max_depth:" + str(max_depth), "learning_rate:" + str(learning_rate), "subsample:" + str(subsample))
        params={
            "max_depth":max_depth,
            #"gamma":gamma,
             'subsample' : subsample,
            'learning_rate' : learning_rate,
            #'subsample' : subsample,
            #'min_child_weight': min_child_weight,
            'objective': "multi:softmax",
            'num_class': 7 ,
            "eval_metric":'merror',
            'silent':False,

            # 'gpu_id':1,
            # 'max_bin':16,
            # 'tree_method': "gpu_exact",
            # 'updater':'grow_gpu',
            # 'n_gpus':-1,
            # 'predictor' : "gpu_predictor",

        }
        num_round = 1
        model=xgb.train(params,self.train, num_round, self.watchlist, feval=Xg_iter_precision)
        cov_res=xgb.cv(params,self.train, num_round, nfold=5, feval=Xg_iter_precision)
        #print(cov_res.head())
        cov_rec=cov_res.tail(1)['test-precision_4_5_6-mean'].values
        predicted=model.predict(self.test)
        scoring=precision_score( self.test_y,predicted,average='micro',labels=[4,5,6])
        print('precision is ',scoring)
        print('cv_precision_4_5_6',cov_rec[0])
        return -cov_rec[0] 
开发者ID:doncat99,项目名称:StockRecommendSystem,代码行数:41,代码来源:Stock_Prediction_Model_XgBoost.py

示例4: optimal_n_rounds

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import cv [as 别名]
def optimal_n_rounds(xgb_model, xgb_matrix, max_n_estimators):
	""" take the input model and xgb matrix (x and y values) 
		and determine the optimal number of trees via cross validation.
		 returns the number of trees """
	cvresult = xgb.cv(xgb_model, x_values, 
						num_boost_round = max_n_estimators, 
						nfold = 5,
						metrics	= 'auc', 
						early_stopping_rounds = 50)	
	return cvresult.shape[0] 
开发者ID:CNuge,项目名称:kaggle-code,代码行数:12,代码来源:comments_xgb_final.py

示例5: optimal_params

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import cv [as 别名]
def optimal_params(xgb_model, x_vals, y_vals, xgb_param_grid):
	""" take a model, predictor matrix and paramater grid and
		return the optimal paramater set """
	_gsearch = GridSearchCV(xgb_model,  xgb_param_grid, 
								scoring='roc_auc', 
								n_jobs=4, 
								iid=False, 
								cv=3)
	_gsearch.fit(x_vals, y_vals)

	return _gsearch.best_params_ 
开发者ID:CNuge,项目名称:kaggle-code,代码行数:13,代码来源:comments_xgb_final.py

示例6: get_n_estimators

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import cv [as 别名]
def get_n_estimators(self,model):
        """
        returns optimal number of estimators using CV on training set
        """
        xgb_param = model.get_xgb_params()
        xgb_param['eta'] = self._params['learning_rate']
        self._params['eta'] = self._params['learning_rate']
        
        if self.balance_class:
            xgb_train = xgb.DMatrix(self.X, label=self.y, weight=self.get_label_weights())
        else:
            xgb_train = xgb.DMatrix(self.X, label=self.y)
        
        kwargs_cv = {'num_boost_round':self.params['n_estimators'],
                     'nfold':self.params_cv['cv_folds'],
                     'early_stopping_rounds':self.params_cv['early_stopping_rounds'],
                     'stratified':self.params_cv['stratified']}
        
        try: # check if custom evalution function is specified
            if callable(self.params_cv['feval']):
                kwargs_cv['feval'] = self.params_cv['feval']
        except KeyError:
            kwargs_cv['metrics'] = self.params_cv['metrics']
        
        if self._greater_is_better:
            kwargs_cv['maximize'] = True
        else:
            kwargs_cv['maximize'] = False
        
        cvresult = xgb.cv(xgb_param,xgb_train,**kwargs_cv)
        self._params['n_estimators'] = int(cvresult.shape[0]/(1-1/self.params_cv['cv_folds']))
        return self 
开发者ID:arnaudvl,项目名称:ml-parameter-optimization,代码行数:34,代码来源:xgb_tune.py

示例7: cross_validate

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import cv [as 别名]
def cross_validate(self):
        """Train model using k-fold cross validation and
        return mean value of validation metric.
        """
        d_train = xgb.DMatrix(self.x_train, label=self.y_train)
        # xgb calls its k-fold cross-validation parameter 'nfold'
        cv_result = xgb.cv(
            self.params, d_train, num_boost_round=self.num_boost_round,
            early_stopping_rounds=self.early_stopping_rounds, nfold=self.kfold
        )
        return cv_result['test-{}-mean'.format(self.eval_metric)][-1] 
开发者ID:gmontamat,项目名称:gentun,代码行数:13,代码来源:xgboost_models.py

示例8: run_cv

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import cv [as 别名]
def run_cv(self):
        self._run_startup()
        print("Running CV to determine optional number of rounds")
        xg_train = xgb.DMatrix(self.x_train, label=self.y_train)
        cvresult = xgb.cv(self.params, xg_train, num_boost_round=self.rounds,nfold=self.num_folds,
                verbose_eval = True, early_stopping_rounds=self.early_stop)
        self.rounds=cvresult.shape[0]
        #self.score = cvresult.iloc[-1]['test-logloss-mean']
        self.score = cvresult.iloc[-1]['test-rmse-mean']
        self.scores = [self.score]
        print("Should use {} rounds.".format(self.rounds))
        return self.score, self.rounds 
开发者ID:jeffheaton,项目名称:jh-kaggle-util,代码行数:14,代码来源:train_xgboost.py

示例9: run_croos_validation

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import cv [as 别名]
def run_croos_validation(self):
        
        features,labels,cv_folds = self.getFeaturesLabel()
        dtrain_cv  = xgb.DMatrix(features, label= labels,feature_names=features.columns)
        self.set_xgb_parameters()

        # specify validations set to watch performance
        model = xgb.cv(self.xgb_params, dtrain_cv, folds=cv_folds, **self.xgb_learning_params)
        best_scroe = model[self.best_score_colname_in_cv].max()
        return best_scroe 
开发者ID:LevinJ,项目名称:Supply-demand-forecasting,代码行数:12,代码来源:xgbbasemodel.py

示例10: hyper

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import cv [as 别名]
def hyper(self, select_params, fixed_params):
        """
        Tune XGBoost hyper-parameters by selecting from permutations of values from the ``select_params`` dictionary. Remaining parameters with single values are specified by the ``fixed_params`` dictionary. Returns a dataframe with ranking of ``select_params`` items.
        """
        optimized_GBM = GridSearchCV(xgb.XGBClassifier(**fixed_params), select_params, scoring = 'accuracy', cv = 5, n_jobs = -1)
        optimized_GBM.fit(Base.train_X, Base.train_y)
        df = pd.DataFrame(optimized_GBM.cv_results_)[['rank_test_score', 'params']].sort_values(by='rank_test_score')
        df.rename(columns = {'rank_test_score': 'rank'}, inplace = True)
        return df 
开发者ID:Speedml,项目名称:speedml,代码行数:11,代码来源:xgb.py

示例11: cv

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import cv [as 别名]
def cv(self, grid_params):
        """
        Calculate the Cross-Validation (CV) score for XGBoost model based on ``grid_params`` parameters. Sets xgb.cv_results variable to the resulting dataframe.
        """
        xgdmat = xgb.DMatrix(Base.train_X, Base.train_y)
        self.cv_results = xgb.cv(
            params = grid_params, dtrain = xgdmat,
            num_boost_round = 1000, nfold = 5,
            metrics = ['error'], early_stopping_rounds = 20)
        self.error = self.cv_results.get_value(len(self.cv_results) - 1, 'test-error-mean') 
开发者ID:Speedml,项目名称:speedml,代码行数:12,代码来源:xgb.py

示例12: evaluate

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import cv [as 别名]
def evaluate(model, data: Tuple[xgb.DMatrix, xgb.DMatrix], args: argparse.Namespace):
    """
    Cross validate results, this will print result out as [iteration]  metric_name:mean_value

    :param xgb.core.Booster model:
    :param Tuple[xgb.DMatrix, xgb.DMatrix] data:    MNIST database train and test data and labels
    :param argparse.Namespace args:                 An object to take the attributes
                                                        The default is a new empty Namespace object

    :return:                                        None
    """
    dtrain = data[0]
    dtest = data[1]
    y_pred = model.predict(dtest)
    _logger.info('y_pred.shape: {}'.format(y_pred.shape))

    # ------------- extract most confident predictions ---------------------------------------------
    # output is a vector of ndata * nclass, which can be further reshaped to ndata * nclass matrix
    # probabilities contains predicted probability of each data point belonging to each class
    probabilities = y_pred.reshape(y_pred.shape[0], y_pred.shape[1])
    # classes is an array of the most confident classification predictions
    classes = np.argmax(probabilities, axis=1).tolist()

    y_pred_precision_score = precision_score(dtest.get_label(), classes, average='macro')
    _logger.info('y_pred_precision_score: %s' % y_pred_precision_score)

    _logger.info('running cross validation')

    cv_result = xgb.cv(
        args.booster_params,
        dtrain,
        num_boost_round=10,
        nfold=5,
        metrics={EVAL_METRIC},
        seed=0,
        callbacks=[
            xgb.callback.print_evaluation(show_stdv=False),
            xgb.callback.early_stop(3)
        ]
    )
    _logger.info('evaluate.cv_result: %s' % cv_result) 
开发者ID:PipelineAI,项目名称:models,代码行数:43,代码来源:pipeline_train.py

示例13: evaluate

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import cv [as 别名]
def evaluate(model: xgb.core.Booster, data: Tuple[xgb.DMatrix, xgb.DMatrix], args: argparse.Namespace):
    """
    Cross validate results, this will print result out as [iteration]  metric_name:mean_value

    :param xgb.core.Booster model:                  Trained XGBoost MNIST model
    :param Tuple[xgb.DMatrix, xgb.DMatrix] data:    MNIST database train and test data and labels
    :param argparse.Namespace args:                 An object to take the attributes
                                                        The default is a new empty Namespace object

    :return:                                        None
    """
    dtrain = data[0]
    dtest = data[1]
    y_pred = model.predict(dtest)
    _logger.info('y_pred.shape: {}'.format(y_pred.shape))

    # ------------- extract most confident predictions ---------------------------------------------
    # output is a vector of ndata * nclass, which can be further reshaped to ndata * nclass matrix
    # probabilities contains predicted probability of each data point belonging to each class
    probabilities = y_pred.reshape(y_pred.shape[0], y_pred.shape[1])
    # classes is an array of the most confident classification predictions
    classes = np.argmax(probabilities, axis=1).tolist()

    y_pred_precision_score = precision_score(dtest.get_label(), classes, average='macro')
    _logger.info('y_pred_precision_score: %s' % y_pred_precision_score)

    _logger.info('running cross validation')

    cv_result = xgb.cv(
        args.booster_params,
        dtrain,
        num_boost_round=10,
        nfold=5,
        metrics={EVAL_METRIC},
        seed=0,
        callbacks=[
            xgb.callback.print_evaluation(show_stdv=False),
            xgb.callback.early_stop(3)
        ]
    )
    _logger.info('evaluate.cv_result: %s' % cv_result) 
开发者ID:PipelineAI,项目名称:models,代码行数:43,代码来源:pipeline_train.py

示例14: rmse_cv

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import cv [as 别名]
def rmse_cv(model):
    rmse = np.sqrt(
        -cross_val_score(model, X_train, y, scoring="neg_mean_squared_error", cv=5)
    )
    return rmse 
开发者ID:modin-project,项目名称:modin,代码行数:7,代码来源:kaggle9.py

示例15: objective

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import cv [as 别名]
def objective(trial):
    train_x, train_y = sklearn.datasets.load_breast_cancer(return_X_y=True)
    dtrain = xgb.DMatrix(train_x, label=train_y)

    param = {
        "silent": 1,
        "objective": "binary:logistic",
        "eval_metric": "auc",
        "booster": trial.suggest_categorical("booster", ["gbtree", "gblinear", "dart"]),
        "lambda": trial.suggest_loguniform("lambda", 1e-8, 1.0),
        "alpha": trial.suggest_loguniform("alpha", 1e-8, 1.0),
    }

    if param["booster"] == "gbtree" or param["booster"] == "dart":
        param["max_depth"] = trial.suggest_int("max_depth", 1, 9)
        param["eta"] = trial.suggest_loguniform("eta", 1e-8, 1.0)
        param["gamma"] = trial.suggest_loguniform("gamma", 1e-8, 1.0)
        param["grow_policy"] = trial.suggest_categorical("grow_policy", ["depthwise", "lossguide"])
    if param["booster"] == "dart":
        param["sample_type"] = trial.suggest_categorical("sample_type", ["uniform", "weighted"])
        param["normalize_type"] = trial.suggest_categorical("normalize_type", ["tree", "forest"])
        param["rate_drop"] = trial.suggest_loguniform("rate_drop", 1e-8, 1.0)
        param["skip_drop"] = trial.suggest_loguniform("skip_drop", 1e-8, 1.0)

    pruning_callback = optuna.integration.XGBoostPruningCallback(trial, "test-auc")
    history = xgb.cv(param, dtrain, num_boost_round=100, callbacks=[pruning_callback])

    mean_auc = history["test-auc-mean"].values[-1]
    return mean_auc 
开发者ID:optuna,项目名称:optuna,代码行数:31,代码来源:xgboost_cv_integration.py


注:本文中的xgboost.cv方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。