XGBClassifier 在不同机器的相似环境下给出不同的结果

问题描述 投票:0回答:1

我使用网格搜索和以下参数训练了 XGBoost 分类器模型:

params = {
    'max_depth':[5,6],
    'min_child_weight': [1, 2, 3],
    'subsample': [0.6, 0.8, 1.0],
    'colsample_bytree': [0.6, 0.8, 1.0]
}

xgb = XGBClassifier(device="cuda",learning_rate=0.02, n_estimators=1000, objective='binary:logistic', verbosity=0, tree_method="gpu_hist")

skf = StratifiedKFold(n_splits=folds, shuffle = True, random_state = 1001)

grid_search = GridSearchCV(estimator=xgb, param_grid=params, scoring='roc_auc', n_jobs=-1, cv=skf.split(x_train,y_train), verbose=100, return_train_score=True)

grid_search.fit(x_train, y_train)

然后我保存了最佳模型如下:

from joblib import dump
joblib.dump(grid_search.best_estimator_, 'xgboost_grid_search.joblib')

当我再次加载模型时,predict_proba 给出不同的结果,这就是我加载模型以获得预测的方式:

import joblib
model = joblib.load("xgboost_grid_search.joblib")
model.predict_proba(x_test)

这里 x_train 和 x_test 包含数值特征。 y_train 和 y_test 是分类值(0 或 1)

通过阅读相当多的博客、文章、堆栈溢出答案,我确保这两种环境都满足以下条件:

 1. Correct python version - 3.11.5
 2. Same/consistent joblib and xgboost pip versions - 1.2.0 and 2.0.0 respectively
 3. Correct ordering of features in x_test as x_train and model.feature_names_in_

但是,我指出这两个环境的操作系统是不同的:Mac M1 和 Ubuntu(不确定这是否是一个问题)。

感谢任何帮助,如果我做错了什么,请告诉我。

提前致谢!

machine-learning scikit-learn xgboost joblib
1个回答
1
投票

要重现结果,您还需要设置随机种子,即

random_state
 中的 
xgboost.XGBClassifier

输入参数
params = {
    'max_depth':[5,6],
    'min_child_weight': [1, 2, 3],
    'subsample': [0.6, 0.8, 1.0],
    'colsample_bytree': [0.6, 0.8, 1.0]
}

xgb = XGBClassifier(device="cuda",learning_rate=0.02, 
                    n_estimators=1000, 
                    objective='binary:logistic', 
                    verbosity=0, 
                    tree_method="gpu_hist", 
                    random_state= 1001) # HERE!

skf = StratifiedKFold(n_splits=folds, shuffle = True, random_state = 1001)

grid_search = GridSearchCV(estimator=xgb, param_grid=params, scoring='roc_auc', n_jobs=-1, cv=skf.split(x_train,y_train), verbose=100, return_train_score=True)

grid_search.fit(x_train, y_train)
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.