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


Python xgboost.XGBClassifier方法代码示例

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

在下文中一共展示了xgboost.XGBClassifier方法的30个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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: create_xgboost_classifier

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import XGBClassifier [as 别名]
def create_xgboost_classifier(X, y):
    xgb = XGBClassifier(
        learning_rate=0.1, max_depth=3, n_estimators=100, n_jobs=1, random_state=777
    )
    model = xgb.fit(X, y)
    return model 
开发者ID:interpretml,项目名称:interpret-text,代码行数:8,代码来源:common_utils.py


示例16: fit

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import XGBClassifier [as 别名]
def fit(self, dataset, **kwargs):
    """
    Fits XGBoost model to data.
    """
    X = dataset.X
    y = np.squeeze(dataset.y)
    w = np.squeeze(dataset.w)
    seed = self.model_instance.random_state
    import xgboost as xgb
    if isinstance(self.model_instance, xgb.XGBClassifier):
      xgb_metric = "auc"
      sklearn_metric = "roc_auc"
      stratify = y
    elif isinstance(self.model_instance, xgb.XGBRegressor):
      xgb_metric = "mae"
      sklearn_metric = "neg_mean_absolute_error"
      stratify = None
    best_param = self._search_param(sklearn_metric, X, y)
    # update model with best param
    self.model_instance = self.model_class(**best_param)

    # Find optimal n_estimators based on original learning_rate
    # and early_stopping_rounds
    X_train, X_test, y_train, y_test = train_test_split(
        X, y, test_size=0.2, random_state=seed, stratify=stratify)

    self.model_instance.fit(
        X_train,
        y_train,
        early_stopping_rounds=self.early_stopping_rounds,
        eval_metric=xgb_metric,
        eval_set=[(X_train, y_train), (X_test, y_test)],
        verbose=self.verbose)
    # Since test size is 20%, when retrain model to whole data, expect
    # n_estimator increased to 1/0.8 = 1.25 time.
    estimated_best_round = np.round(self.model_instance.best_ntree_limit * 1.25)
    self.model_instance.n_estimators = np.int64(estimated_best_round)
    self.model_instance.fit(X, y, eval_metric=xgb_metric, verbose=self.verbose) 
开发者ID:deepchem,项目名称:deepchem,代码行数:40,代码来源:__init__.py


示例17: 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


示例18: load_spark_model

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import XGBClassifier [as 别名]
def load_spark_model(model_path, metadata_path):

    import xgboost as xgb
    import json
    import numpy as np

    if not isinstance(model_path, str) or not isinstance(model_path, str):
        raise ValueError("model and metadata paths must be str, not {0} and {1}".format(type(model_path), type(metadata_path)))

    with open(metadata_path) as f:
        metadata = json.loads(f.read().strip())

    xgb_class = metadata.get("class")
    if xgb_class == "ml.dmlc.xgboost4j.scala.spark.XGBoostClassificationModel":
        clf = xgb.XGBClassifier()
        setattr(clf, "base_score", metadata["paramMap"]["baseScore"])
    elif xgb_class == "ml.dmlc.xgboost4j.scala.spark.XGBoostRegressionModel":
        clf = xgb.XGBRegressor()
    else:
        raise ValueError("Unsupported model.")

    setattr(clf, "objective", metadata["paramMap"]["objective"])
    setattr(clf, "missing",
            np.nan if metadata["paramMap"]["missing"] in ["NaN", "nan", "null", "None"] else metadata["paramMap"][
                "missing"])
    setattr(clf, "booster", metadata["paramMap"].get("booster", "gbtree"))
    setattr(clf, "n_estimators", metadata["paramMap"].get("numRound", 1))

    booster = xgb.Booster()
    booster.load_model(model_path)

    clf._Booster = booster
    return clf 
开发者ID:znly,项目名称:go-ml-transpiler,代码行数:35,代码来源:spark_tools.py


示例19: main

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import XGBClassifier [as 别名]
def main(export_dir):

    ## load dataset
    x, y = load_dataset(return_X_y=True)

    ## train xgb
    xgbc = xgb.XGBClassifier(n_estimators=100, max_depth=7)
    xgbc.fit(x, y)

    # transpile model
    os.mkdir(os.path.join(export_dir, "xgb"))
    transpiler = Transpiler(xgbc)
    transpiler.transpile(package_name="xgb", method_name="predict", export_method=True)
    transpiler.write(os.path.join(export_dir, "xgb"))
    print("xgb done.")


    ## train rfc
    rfc = RFC(n_estimators=100, max_depth=7)
    rfc.fit(x, y)

    # transpile model
    os.mkdir(os.path.join(export_dir, "rfc"))
    transpiler = Transpiler(rfc)
    transpiler.transpile(package_name="rfc", method_name="predict", export_method=True)
    transpiler.write(os.path.join(export_dir, "rfc"))
    print("rfc done.") 
开发者ID:znly,项目名称:go-ml-transpiler,代码行数:29,代码来源:transpile.py


示例20: XGBoost_First

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import XGBClassifier [as 别名]
def XGBoost_First(self, data, max_depth=8, n_estimators=220):
        model = xgb.XGBClassifier(max_depth=max_depth, learning_rate=0.1, n_estimators=n_estimators,
                                  silent=True, objective='binary:logistic', booster='gbtree')
        model.fit(data['train'][:, :-1], data['train'][:, -1])
        # 存储验证数据集结果和预测数据集结果
        # 训练数据集的预测结果
        xul = model.predict(data['train'][:, :-1])
        # 验证的预测结果
        yanre = model.predict(data['test'][:, :-1])
        # 预测的预测结果
        prer = model.predict(data['predict'][:, :-1])

        # 每计算一折后,要计算训练、验证、预测数据的误差
        xx = self.F1(xul, data['train'][:, -1])

        yy = self.F1(yanre, data['test'][:, -1])

        pp = self.F1(prer, data['predict'][:, -1])

        # 开始结合
        self.yanzhneg_pr.append(yanre)
        self.yanzhneg_real = data['test'][:, -1]
        self.predi.append(prer)
        self.preal = data['predict'][:, -1]

        # 存储误差
        self.error_dict['XGBoost'] = [xx, yy, pp]
        return print('1层中的XGBoost运行完毕')

    # CatBoost 
开发者ID:Anfany,项目名称:Machine-Learning-for-Beginner-by-Python3,代码行数:32,代码来源:Blending_Classify_adult.py


示例21: XGBoost_First

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import XGBClassifier [as 别名]
def XGBoost_First(self, data, max_depth=5, n_estimators=320):
        # 存储每一折中验证数据集的预测结果
        yanzhenglist = []
        # 存储每一折中验证数据集的真实结果
        yanzhenglist_real = []
        # 存储每一折中预测数据集的预测结果
        prelist = []
        # 存储训练、验证、预测数据的误差
        errorlsit = []
        # 开始每一折的训练
        for zhe in [zheshu for zheshu in data.keys() if zheshu != 'predict']:
            model = xgb.XGBClassifier(max_depth=max_depth, learning_rate=0.1, n_estimators=n_estimators,
                                      silent=True, objective='binary:logistic', booster='gbtree')
            model.fit(data[zhe]['train'][:, :-1], data[zhe]['train'][:, -1])
            # 注意存储验证数据集结果和预测数据集结果的不同
            # 训练数据集的预测结果
            xul = model.predict(data[zhe]['train'][:, :-1])
            # 验证的预测结果
            yanre = model.predict(data[zhe]['test'][:, :-1])
            # 预测的预测结果
            prer = model.predict(data['predict'][:, :-1])
            yanzhenglist += list(yanre)
            yanzhenglist_real += list(data[zhe]['test'][:, -1])
            prelist.append(prer)
            # 每计算一折后,要计算训练、验证、预测数据的误差
            xx = self.F1(xul, data[zhe]['train'][:, -1])
            yy = self.F1(yanre, data[zhe]['test'][:, -1])
            pp = self.F1(prer, data['predict'][:, -1])
            errorlsit.append([xx, yy, pp])
        # 针对预测数据集的预测结果计算均值
        meanPre = np.mean(np.array(prelist), axis=0)
        # 开始结合
        self.yanzhneg_pr.append(yanzhenglist)
        self.yanzhneg_real = yanzhenglist_real
        self.predi.append(meanPre)
        self.preal = data['predict'][:, -1]
        # 储存误差
        self.error_dict['XGBoost'] = np.mean(np.array(errorlsit), axis=0)
        return print('1层中的XGBoost运行完毕')

    # CatBoost 
开发者ID:Anfany,项目名称:Machine-Learning-for-Beginner-by-Python3,代码行数:43,代码来源:Stacking_Classify_adult.py


示例22: requires

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import XGBClassifier [as 别名]
def requires(self):
        data = MakeData()
        redshells.factory.register_prediction_model('XGBClassifier', xgboost.XGBClassifier)
        return redshells.train.OptimizeBinaryClassificationModel(
            rerun=True,
            train_data_task=data,
            target_column_name='y',
            model_name='XGBClassifier',
            model_kwargs=dict(n_estimators=50),
            test_size=0.2,
            optuna_param_name='XGBClassifier_default') 
开发者ID:m3dev,项目名称:redshells,代码行数:13,代码来源:binary_classification_example.py


示例23: test_binary_classification

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import XGBClassifier [as 别名]
def test_binary_classification():
    estimator = xgboost.XGBClassifier(n_estimators=2, random_state=1,
                                      max_depth=1)
    utils.get_binary_classification_model_trainer()(estimator)

    assembler = assemblers.XGBoostModelAssemblerSelector(estimator)
    actual = assembler.assemble()

    sigmoid = ast.BinNumExpr(
        ast.NumVal(1),
        ast.BinNumExpr(
            ast.NumVal(1),
            ast.ExpExpr(
                ast.BinNumExpr(
                    ast.NumVal(0),
                    ast.BinNumExpr(
                        ast.IfExpr(
                            ast.CompExpr(
                                ast.FeatureRef(20),
                                ast.NumVal(16.7950001),
                                ast.CompOpType.GTE),
                            ast.NumVal(-0.519171),
                            ast.NumVal(0.49032259)),
                        ast.IfExpr(
                            ast.CompExpr(
                                ast.FeatureRef(27),
                                ast.NumVal(0.142349988),
                                ast.CompOpType.GTE),
                            ast.NumVal(-0.443304211),
                            ast.NumVal(0.391988248)),
                        ast.BinNumOpType.ADD),
                    ast.BinNumOpType.SUB)),
            ast.BinNumOpType.ADD),
        ast.BinNumOpType.DIV,
        to_reuse=True)

    expected = ast.VectorVal([
        ast.BinNumExpr(ast.NumVal(1), sigmoid, ast.BinNumOpType.SUB),
        sigmoid])

    assert utils.cmp_exprs(actual, expected) 
开发者ID:BayesWitnesses,项目名称:m2cgen,代码行数:43,代码来源:test_xgboost.py


示例24: test_multi_class

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import XGBClassifier [as 别名]
def test_multi_class():
    estimator = xgboost.XGBClassifier(n_estimators=1, random_state=1,
                                      max_depth=1)
    estimator.fit(np.array([[1], [2], [3]]), np.array([1, 2, 3]))

    assembler = assemblers.XGBoostModelAssemblerSelector(estimator)
    actual = assembler.assemble()

    exponent = ast.ExpExpr(
        ast.BinNumExpr(
            ast.NumVal(0.5),
            ast.NumVal(0.0),
            ast.BinNumOpType.ADD),
        to_reuse=True)

    exponent_sum = ast.BinNumExpr(
        ast.BinNumExpr(exponent, exponent, ast.BinNumOpType.ADD),
        exponent,
        ast.BinNumOpType.ADD,
        to_reuse=True)

    softmax = ast.BinNumExpr(exponent, exponent_sum, ast.BinNumOpType.DIV)

    expected = ast.VectorVal([softmax] * 3)

    assert utils.cmp_exprs(actual, expected) 
开发者ID:BayesWitnesses,项目名称:m2cgen,代码行数:28,代码来源:test_xgboost.py


示例25: test_classifier

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import XGBClassifier [as 别名]
def test_classifier(self):
        boston = datasets.load_boston()
        X, y = boston.data, boston.target

        xgb1 = XGBClassifier(n_estimators=3)
        xgb1.fit(X[0:70],y[0:70]) 
开发者ID:Kaggle,项目名称:docker-python,代码行数:8,代码来源:test_xgboost.py


示例26: get_model

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import XGBClassifier [as 别名]
def get_model(model_type):
    if model_type == "logistic_regression":
        model = LogisticRegression(solver='lbfgs', fit_intercept=False)  # 快,准确率一般。val mean acc:0.91
    elif model_type == "random_forest":
        model = RandomForestClassifier(n_estimators=300)  # 速度还行,准确率一般。val mean acc:0.93125
    elif model_type == "decision_tree":
        model = DecisionTreeClassifier()  # 速度快,准确率低。val mean acc:0.62
    elif model_type == "knn":
        model = KNeighborsClassifier()  # 速度一般,准确率低。val mean acc:0.675
    elif model_type == "bayes":
        model = MultinomialNB()  # 速度快,准确率低。val mean acc:0.62
    elif model_type == "xgboost":
        from xgboost import XGBClassifier
        model = XGBClassifier()  # 速度慢,准确率高。val mean acc:0.95
    elif model_type == "svm":
        model = SVC(kernel='linear', probability=True)  # 速度慢,准确率高,val mean acc:0.945
    elif model_type == 'mlp':
        model = MLPClassifier()  # 速度一般,准确率一般。val mean acc:0.89125
    elif model_type == 'ensemble':
        from mlxtend.classifier import EnsembleVoteClassifier
        from xgboost import XGBClassifier
        clf1 = LogisticRegression(random_state=0)
        clf2 = XGBClassifier(random_state=0)
        clf3 = SVC(random_state=0, kernel='linear', probability=True)
        clf4 = MLPClassifier(random_state=0)
        model = EnsembleVoteClassifier(clfs=[clf1, clf2, clf3, clf4],
                                       weights=[1, 2, 2, 1], voting='soft', verbose=2)
    elif model_type == 'stack':
        from mlxtend.classifier import StackingClassifier
        from xgboost import XGBClassifier
        clf1 = XGBClassifier(random_state=0)
        clf2 = SVC(random_state=0, kernel='linear', probability=True)
        clf3 = MLPClassifier(random_state=0)
        lr = LogisticRegression()
        model = StackingClassifier(classifiers=[clf1, clf2, clf3],
                                   use_probas=True,
                                   average_probas=False,
                                   meta_classifier=lr)
    else:
        raise ValueError('model type set error.')
    return model 
开发者ID:shibing624,项目名称:text-classifier,代码行数:43,代码来源:classic_model.py


示例27: __init__

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import XGBClassifier [as 别名]
def __init__(self, model_save_path=''):
        import xgboost as xgb
        self.lr_clf = LogisticRegression()
        self.one_hot_encoder = OneHotEncoder()
        self.xgb_clf = xgb.XGBClassifier()
        self.xgb_eval_metric = 'mlogloss'
        self.model_save_path = model_save_path
        self.init = False 
开发者ID:shibing624,项目名称:text-classifier,代码行数:10,代码来源:xgboost_lr_model.py


示例28: test_xgboost_06

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import XGBClassifier [as 别名]
def test_xgboost_06(self):
        iris = datasets.load_iris()
        irisd = pd.DataFrame(iris.data, columns=iris.feature_names)
        irisd['Species'] = iris.target

        features = irisd.columns.drop('Species')
        target = 'Species'
        f_name = "xgbc_pmml.pmml"

        model = XGBClassifier()

        model.fit(irisd[features], irisd[target])

        with self.assertRaises(TypeError):
            xgboost_to_pmml(model, features, target,f_name , model_name="testModel") 
开发者ID:nyoka-pmml,项目名称:nyoka,代码行数:17,代码来源:test_xgboost_to_pmml_UnitTest.py


示例29: train

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import XGBClassifier [as 别名]
def train(self):
        ### fit model for train data
        self.model = XGBClassifier(learning_rate=0.3,
                              n_estimators=100,         # 树的个数--1000棵树建立xgboost
                              max_depth=6,               # 树的深度
                              min_child_weight = 1,      # 叶子节点最小权重
                              gamma=0.,                  # 惩罚项中叶子结点个数前的参数
                              subsample=0.8,             # 随机选择80%样本建立决策树
                              colsample_btree=0.8,       # 随机选择80%特征建立决策树
                              objective='multi:softmax', # 指定损失函数
                              scale_pos_weight=1,        # 解决样本个数不平衡的问题
                              random_state=27            # 随机数
                              )
        print("开始训练!")
        self.model.fit(self.data['x_train'],
                  self.data['y_train'],
                  eval_set = [(self.data['x_test'],self.data['y_test'])],
                  eval_metric = "mlogloss",
                  early_stopping_rounds = 10,
                  verbose = True)

        ### model evaluate

        predictions = self.model.predict_proba(self.data['x_test'])
        y_pred = np.argmax(predictions, 1)
        scores = [predictions[idx][y_pred[idx]] for idx in range(len(y_pred))]
        #for idx in range(len(y_pred)):
        #    if scores[idx] < self.thre:
        #        y_pred[idx] = -1
        accuracy = accuracy_score(self.data['y_test'],y_pred)
        print("accuarcy: %.2f%%" % (accuracy*100.0))

        #dt = pd.DataFrame({'text':self.data['raw_test_list'],
        #                   'feature':self.data['test_list'], 
        #                   'target':[self.labels_rev[item] for item in
        #                             self.data['y_test']] ,
        #                   'pred': [self.labels_rev[item] for item in 
        #                            y_pred],
        #                   'score': scores })
        #dt.to_csv(self.result_path,index=False,sep=',') 
开发者ID:zhufz,项目名称:nlp_research,代码行数:42,代码来源:xgb.py


示例30: setUpClass

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import XGBClassifier [as 别名]
def setUpClass(self):
        """
        Set up the unit test by loading the dataset and training a model.
        """
        from sklearn.datasets import load_boston

        scikit_data = load_boston()
        self.xgb_model = xgboost.XGBClassifier()
        target = scikit_data["target"] > scikit_data["target"].mean()
        self.xgb_model.fit(scikit_data["data"], target)

        # Save the data and the model
        self.scikit_data = scikit_data 
开发者ID:apple,项目名称:coremltools,代码行数:15,代码来源:test_boosted_trees_classifier.py



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