sklearn的XGBoost的`random_state`是什么?

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

我似乎无法让XGBoost连续两次给我相同的结果。在sklearn中,我似乎能够使用random_state,但这在XGBoost中不起作用。

我也尝试过设置seedsubsamplecolsample_bytree(将subsamplecolsample_bytree设置为1似乎没有什么不同。

关于如何重现结果的任何建议,就像在sklearn中设置random_state值一样?

这里是一些代码的完整性,但是我想您可能想在我的问题的底部仔细查看模型。

预处理

from sklearn.impute import SimpleImputer
from sklearn.preprocessing import LabelEncoder

#numerical columns
numerical_columns_list = [colname for colname in X_train.columns if
                    X_train[colname].dtypes in ['int64', 'float64']]

X_train_trf = X_train.copy()
X_valid_trf = X_valid.copy()

# Preprocessing for numerical data
num_imputer = SimpleImputer(strategy='median')
X_train_trf[numerical_columns_list] = num_imputer.fit_transform(X_train_trf[numerical_columns_list])
X_valid_trf[numerical_columns_list] = num_imputer.transform(X_valid_trf[numerical_columns_list])

# Preprocessing for categorical data

categorical_columns_list = [colname for colname in X_train.columns if
                    X_train[colname].dtypes == 'object' ]

cat_imputer = SimpleImputer(strategy='most_frequent')
X_train_trf[categorical_columns_list] = cat_imputer.fit_transform(X_train_trf[categorical_columns_list])
X_valid_trf[categorical_columns_list] = cat_imputer.transform(X_valid_trf[categorical_columns_list])

le = LabelEncoder()
for col in X_train_trf[categorical_columns_list].columns:
    X_train_trf[col] = le.fit_transform(X_train_trf[col])
    X_valid_trf[col] = le.fit_transform(X_valid_trf[col])

型号

from xgboost import XGBRegressor
from sklearn.metrics import mean_absolute_error
model = XGBRegressor(n_estimators=1000, learning_rate=0.05,
                     subsample=0.8, colsample_bytree= 0.8, seed=42)

model.fit(X_train_trf,y_train,
        early_stopping_rounds=5,
        eval_set=[(X_train_trf, y_train), (X_valid_trf, y_valid)],
        verbose=False)
preds = model.predict(X_valid_trf)
python-3.x machine-learning scikit-learn xgboost kaggle
1个回答
0
投票

作为参考,XGBoost的问题并不多,而是数据拆分。感谢Venkatachalam指出来!

我正在用train_test_split分割数据而未设置random_state

固定如下:

X_train_full, X_valid_full, y_train, y_valid = train_test_split(X_full, y,
  train_size=0.8, test_size = 0.2, random_state=1)
© www.soinside.com 2019 - 2024. All rights reserved.