當前位置: 首頁>>代碼示例>>Python>>正文


Python xgboost.XGBClassifier方法代碼示例

本文整理匯總了Python中xgboost.XGBClassifier方法的典型用法代碼示例。如果您正苦於以下問題:Python xgboost.XGBClassifier方法的具體用法?Python xgboost.XGBClassifier怎麽用?Python xgboost.XGBClassifier使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在xgboost的用法示例。


在下文中一共展示了xgboost.XGBClassifier方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: Train

# 需要導入模塊: import xgboost [as 別名]
# 或者: from xgboost import XGBClassifier [as 別名]
def Train(data, modelcount, censhu, yanzhgdata):
    model = xgb.XGBClassifier(max_depth=censhu, learning_rate=0.1, n_estimators=modelcount,
                              silent=True, objective='binary:logistic', booster='gbtree')

    model.fit(data[:, :-1], data[:, -1])
    # 給出訓練數據的預測值
    train_out = model.predict(data[:, :-1])
    # 計算MSE
    train_mse = fmse(data[:, -1], train_out)[0]

    # 給出驗證數據的預測值
    add_yan = model.predict(yanzhgdata[:, :-1])
    # 計算f1度量
    add_mse = fmse(yanzhgdata[:, -1], add_yan)[0]
    print(train_mse, add_mse)
    return train_mse, add_mse

# 最終確定組合的函數 
開發者ID:Anfany,項目名稱:Machine-Learning-for-Beginner-by-Python3,代碼行數:20,代碼來源:XGBoost_Classify_adult.py

示例2: recspre

# 需要導入模塊: import xgboost [as 別名]
# 或者: from xgboost import XGBClassifier [as 別名]
def recspre(estrs, predata, datadict, zhe):

    mo, ze = estrs.split('-')
    model = xgb.XGBClassifier(max_depth=int(ze), learning_rate=0.1, n_estimators=int(mo),
                              silent=True, objective='binary:logistic', booster='gbtree')

    model.fit(datadict[zhe]['train'][:, :-1], datadict[zhe]['train'][:, -1])

    # 預測
    yucede = model.predict(predata[:, :-1])
    # 計算混淆矩陣

    print(ConfuseMatrix(predata[:, -1], yucede))

    return fmse(predata[:, -1], yucede)

# 主函數 
開發者ID:Anfany,項目名稱:Machine-Learning-for-Beginner-by-Python3,代碼行數:19,代碼來源:XGBoost_Classify_adult.py

示例3: __init__

# 需要導入模塊: import xgboost [as 別名]
# 或者: from xgboost import XGBClassifier [as 別名]
def __init__(self):
        self._models = dict()
        try:
            import sklearn.ensemble
            self._models['RandomForestClassifier'] = sklearn.ensemble.RandomForestClassifier
        except ImportError:
            pass

        try:
            import xgboost
            self._models['XGBClassifier'] = xgboost.XGBClassifier
        except ImportError:
            pass

        try:
            import lightgbm
            self._models['LGBMClassifier'] = lightgbm.LGBMClassifier
        except ImportError:
            pass

        try:
            import catboost
            self._models['CatBoostClassifier'] = catboost.CatBoostClassifier
        except ImportError:
            pass 
開發者ID:m3dev,項目名稱:redshells,代碼行數:27,代碼來源:prediction_model_factory.py

示例4: __call__

# 需要導入模塊: import xgboost [as 別名]
# 或者: from xgboost import XGBClassifier [as 別名]
def __call__(self, estimator):
        fitted_estimator = estimator.fit(self.X_train, self.y_train)

        if isinstance(estimator, (LinearClassifierMixin, SVC, NuSVC,
                                  LightBaseClassifier)):
            y_pred = estimator.decision_function(self.X_test)
        elif isinstance(estimator, DecisionTreeClassifier):
            y_pred = estimator.predict_proba(self.X_test.astype(np.float32))
        elif isinstance(
                estimator,
                (ForestClassifier, XGBClassifier, LGBMClassifier)):
            y_pred = estimator.predict_proba(self.X_test)
        else:
            y_pred = estimator.predict(self.X_test)

        return self.X_test, y_pred, fitted_estimator 
開發者ID:BayesWitnesses,項目名稱:m2cgen,代碼行數:18,代碼來源:utils.py

示例5: test_01_xgb_classifier

# 需要導入模塊: import xgboost [as 別名]
# 或者: from xgboost import XGBClassifier [as 別名]
def test_01_xgb_classifier(self):
        print("\ntest 01 (xgb classifier with preprocessing) [multi-class]\n")
        model = XGBClassifier()
        pipeline_obj = Pipeline([
            ('scaler',MaxAbsScaler()),
            ("model", model)
        ])
        pipeline_obj.fit(self.X,self.Y)
        file_name = "test01xgboost.pmml"
        xgboost_to_pmml(pipeline_obj, self.features, 'Species', file_name)
        model_name  = self.adapa_utility.upload_to_zserver(file_name)
        predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, self.test_file)
        model_pred = pipeline_obj.predict(self.X)
        model_prob = pipeline_obj.predict_proba(self.X)
        self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True)
        self.assertEqual(self.adapa_utility.compare_probability(probabilities, model_prob), True) 
開發者ID:nyoka-pmml,項目名稱:nyoka,代碼行數:18,代碼來源:testScoreWithAdapaXgboost.py

示例6: test_03_xgb_classifier

# 需要導入模塊: import xgboost [as 別名]
# 或者: from xgboost import XGBClassifier [as 別名]
def test_03_xgb_classifier(self):
        print("\ntest 03 (xgb classifier with preprocessing) [binary-class]\n")
        model = XGBClassifier()
        pipeline_obj = Pipeline([
            ('scaler',MinMaxScaler()),
            ("model", model)
        ])
        pipeline_obj.fit(self.X,self.Y_bin)
        file_name = "test03xgboost.pmml"
        xgboost_to_pmml(pipeline_obj, self.features, 'Species', file_name)
        model_name  = self.adapa_utility.upload_to_zserver(file_name)
        predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, self.test_file)
        model_pred = pipeline_obj.predict(self.X)
        model_prob = pipeline_obj.predict_proba(self.X)
        self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True)
        self.assertEqual(self.adapa_utility.compare_probability(probabilities, model_prob), True) 
開發者ID:nyoka-pmml,項目名稱:nyoka,代碼行數:18,代碼來源:testScoreWithAdapaXgboost.py

示例7: _build_classifier

# 需要導入模塊: import xgboost [as 別名]
# 或者: from xgboost import XGBClassifier [as 別名]
def _build_classifier(self, n_estimators, min_child_weight, max_depth, gamma, subsample, colsample_bytree, num_class):
        assert num_class >= 2
        
        if num_class == 2:
            clf = xgb.XGBClassifier(
            n_estimators=n_estimators,
            min_child_weight=min_child_weight,
            max_depth=max_depth,
            gamma=gamma,
            subsample=subsample,
            colsample_bytree=colsample_bytree
        ) 
        else:
            clf = xgb.XGBClassifier(
            n_estimators=n_estimators,
            min_child_weight=min_child_weight,
            max_depth=max_depth,
            gamma=gamma,
            subsample=subsample,
            colsample_bytree=colsample_bytree,
            objective='multi:softmax', 
            num_class=num_class
        ) 
        return clf 
開發者ID:nginyc,項目名稱:rafiki,代碼行數:26,代碼來源:XgbClf.py

示例8: _train_convert_evaluate_assert

# 需要導入模塊: import xgboost [as 別名]
# 或者: from xgboost import XGBClassifier [as 別名]
def _train_convert_evaluate_assert(self, **xgboost_params):
        """
        Train a scikit-learn model, convert it and then evaluate it with CoreML
        """
        xgb_model = xgboost.XGBClassifier(**xgboost_params)
        xgb_model.fit(self.X, self.target)

        # Convert the model
        spec = xgb_converter.convert(
            xgb_model, self.feature_names, self.output_name, mode="classifier"
        )

        if _is_macos() and _macos_version() >= (10, 13):
            # Get predictions
            df = pd.DataFrame(self.X, columns=self.feature_names)
            probabilities = xgb_model.predict_proba(self.X)
            df["classProbability"] = [
                dict(zip(xgb_model.classes_, cur_vals)) for cur_vals in probabilities
            ]
            metrics = evaluate_classifier_with_probabilities(
                spec, df, probabilities="classProbability", verbose=False
            )
            self.assertEquals(metrics["num_key_mismatch"], 0)
            self.assertLess(metrics["max_probability_error"], 1e-3) 
開發者ID:apple,項目名稱:coremltools,代碼行數:26,代碼來源:test_boosted_trees_classifier_numeric.py

示例9: test_compute_and_plot

# 需要導入模塊: import xgboost [as 別名]
# 或者: from xgboost import XGBClassifier [as 別名]
def test_compute_and_plot(self):
        rows = 20
        X = np.random.rand(rows, 3)
        X = pd.DataFrame(X, columns=[f"f{i}" for i in range(3)])
        y = np.random.randint(0, 2, rows)

        model = XGBClassifier(n_estimators=1, max_depth=2)
        model.fit(X, y)

        with tempfile.TemporaryDirectory() as tmpdir:
            PermutationImportance.compute_and_plot(
                model,
                X_validation=X,
                y_validation=y,
                model_file_path=tmpdir,
                learner_name="learner_test",
                metric_name=None,
                ml_task="binary_classification",
            )
            self.assertTrue(
                os.path.exists(os.path.join(tmpdir, "learner_test_importance.csv"))
            ) 
開發者ID:mljar,項目名稱:mljar-supervised,代碼行數:24,代碼來源:test_importance.py

示例10: classifier

# 需要導入模塊: import xgboost [as 別名]
# 或者: from xgboost import XGBClassifier [as 別名]
def classifier(self, c):
        """Validate the classifier property and set default parameters.

        Args:
            c (classifier): if None, implement the xgboost classifier

        Raises:
            ValueError: classifier does not implement `predict_proba`
        """
        if c is None:
            self._classifier = XGBClassifier()
        else:
            m = "predict_proba"
            if not hasattr(c, m):
                raise ValueError(f"Classifier must implement {m} method.")
            self._classifier = c 
開發者ID:kearnz,項目名稱:autoimpute,代碼行數:18,代碼來源:mis_classifier.py

示例11: execute

# 需要導入模塊: import xgboost [as 別名]
# 或者: from xgboost import XGBClassifier [as 別名]
def execute():
    env = Environment(
        train_dataset=get_toy_classification_data(),
        results_path="HyperparameterHunterAssets",
        metrics=["roc_auc_score"],
        cv_type=RepeatedStratifiedKFold,
        cv_params=dict(n_splits=5, n_repeats=2, random_state=32),
        runs=2,
        # Just instantiate `Environment` with your list of callbacks, and go about business as usual
        experiment_callbacks=[printer_callback(), confusion_matrix_oof()],
        # In addition to `printer_callback` made above, we're also adding the `confusion_matrix_oof` callback
        # This, and other callbacks, can be found in `hyperparameter_hunter.callbacks.recipes`
    )

    experiment = CVExperiment(
        model_initializer=XGBClassifier,
        model_init_params={},
        model_extra_params=dict(fit=dict(verbose=False)),
    ) 
開發者ID:HunterMcGushion,項目名稱:hyperparameter_hunter,代碼行數:21,代碼來源:lambda_callback_example.py

示例12: do_optimization

# 需要導入模塊: import xgboost [as 別名]
# 或者: from xgboost import XGBClassifier [as 別名]
def do_optimization():
    optimizer = BayesianOptPro(iterations=5, random_state=1337)
    optimizer.forge_experiment(
        model_initializer=XGBClassifier,
        model_init_params=dict(
            objective="reg:linear",
            max_depth=Integer(2, 20),
            learning_rate=Real(0.0001, 0.5),
            subsample=0.5,
            booster=Categorical(["gbtree", "dart"]),
        ),
    )
    optimizer.go()


# We'll start with a normal `Environment` for comparison, using only the `env_kwargs` define above 
開發者ID:HunterMcGushion,項目名稱:hyperparameter_hunter,代碼行數:18,代碼來源:recorder_example.py

示例13: opt_xgb_0

# 需要導入模塊: import xgboost [as 別名]
# 或者: from xgboost import XGBClassifier [as 別名]
def opt_xgb_0():
    optimizer = RandomForestOptPro(iterations=2, random_state=1337)
    optimizer.forge_experiment(
        model_initializer=XGBClassifier,
        model_init_params=dict(
            objective="reg:linear",
            max_depth=Integer(2, 20),
            learning_rate=Real(0.0001, 0.5),
            subsample=0.5,
            booster=Categorical(["gbtree", "dart"]),
        ),
    )
    optimizer.go()
    yield optimizer


##################################################
# Test Scenarios
################################################## 
開發者ID:HunterMcGushion,項目名稱:hyperparameter_hunter,代碼行數:21,代碼來源:test_xgboost.py

示例14: test_sentinels_optimization

# 需要導入模塊: import xgboost [as 別名]
# 或者: from xgboost import XGBClassifier [as 別名]
def test_sentinels_optimization(env_0):
    optimizer = GBRT(iterations=2)
    optimizer.forge_experiment(
        model_initializer=XGBClassifier,
        model_init_params=dict(objective="reg:linear", max_depth=Integer(2, 20), subsample=0.5),
        model_extra_params=dict(
            fit=dict(
                eval_set=get_all_sentinels(env_0),
                early_stopping_rounds=5,
                eval_metric=Categorical(["auc", "mae"]),
            )
        ),
    )
    optimizer.go()


##################################################
# General Sentinel Scenarios
################################################## 
開發者ID:HunterMcGushion,項目名稱:hyperparameter_hunter,代碼行數:21,代碼來源:test_sentinels.py

示例15: test_xgboost_classification

# 需要導入模塊: import xgboost [as 別名]
# 或者: from xgboost import XGBClassifier [as 別名]
def test_xgboost_classification(self):
    """Test that sklearn models can learn on simple classification datasets."""
    import xgboost
    np.random.seed(123)
    dataset = sklearn.datasets.load_digits(n_class=2)
    X, y = dataset.data, dataset.target

    frac_train = .7
    n_samples = len(X)
    n_train = int(frac_train * n_samples)
    X_train, y_train = X[:n_train], y[:n_train]
    X_test, y_test = X[n_train:], y[n_train:]
    train_dataset = dc.data.NumpyDataset(X_train, y_train)
    test_dataset = dc.data.NumpyDataset(X_test, y_test)

    classification_metric = dc.metrics.Metric(dc.metrics.roc_auc_score)
    esr = {'early_stopping_rounds': 50}
    xgb_model = xgboost.XGBClassifier(n_estimators=50, seed=123)
    model = dc.models.XGBoostModel(xgb_model, verbose=False, **esr)

    # Fit trained model
    model.fit(train_dataset)
    model.save()

    # Eval model on test
    scores = model.evaluate(test_dataset, [classification_metric])
    assert scores[classification_metric.name] > .9 
開發者ID:deepchem,項目名稱:deepchem,代碼行數:29,代碼來源:test_generalize.py


注:本文中的xgboost.XGBClassifier方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。