【问题标题】:XGBoostError: Check failed: typestr.size() == 3 (2 vs. 3) : `typestr' should be of format <endian><type><size of type in bytes>XGBoostError: Check failed: typestr.size() == 3 (2 vs. 3) : `typestr' 应该是格式 <endian><type><size of type in bytes>
【发布时间】:2021-04-14 15:55:17
【问题描述】:

我在新安装 xgboost 时遇到了一个奇怪的问题。在正常情况下它工作正常。但是,当我在以下函数中使用模型时,它会在标题中给出错误。

我使用的数据集是从 kaggle 借来的,可以在这里看到:https://www.kaggle.com/kemical/kickstarter-projects

我用来拟合模型的函数如下:

def get_val_scores(model, X, y, return_test_score=False, return_importances=False, random_state=42, randomize=True, cv=5, test_size=0.2, val_size=0.2, use_kfold=False, return_folds=False, stratify=True):
    print("Splitting data into training and test sets")
    if randomize:
        if stratify:
            X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, stratify=y, shuffle=True, random_state=random_state)
        else:
            X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, shuffle=True, random_state=random_state)
    else:
        if stratify:
            X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, stratify=y, shuffle=False)
        else:
            X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, shuffle=False)
    print(f"Shape of training data, X: {X_train.shape}, y: {y_train.shape}.  Test, X: {X_test.shape}, y: {y_test.shape}")
    if use_kfold:
        val_scores = cross_val_score(model, X=X_train, y=y_train, cv=cv)
    else:
        print("Further splitting training data into validation sets")
        if randomize:
            if stratify:
                X_train_, X_val, y_train_, y_val = train_test_split(X_train, y_train, test_size=val_size, stratify=y_train, shuffle=True)
            else:
                X_train_, X_val, y_train_, y_val = train_test_split(X_train, y_train, test_size=val_size, shuffle=True)
        else:
            if stratify:
                print("Warning! You opted to both stratify your training data and to not randomize it.  These settings are incompatible with scikit-learn.  Stratifying the data, but shuffle is being set to True")
                X_train_, X_val, y_train_, y_val = train_test_split(X_train, y_train, test_size=val_size, stratify=y_train,  shuffle=True)
            else:
                X_train_, X_val, y_train_, y_val = train_test_split(X_train, y_train, test_size=val_size, shuffle=False)
        print(f"Shape of training data, X: {X_train_.shape}, y: {y_train_.shape}.  Val, X: {X_val.shape}, y: {y_val.shape}")
        print("Getting ready to fit model.")
        model.fit(X_train_, y_train_)
        val_score = model.score(X_val, y_val)
        
    if return_importances:
        if hasattr(model, 'steps'):
            try:
                feats = pd.DataFrame({
                    'Columns': X.columns,
                    'Importance': model[-2].feature_importances_
                }).sort_values(by='Importance', ascending=False)
            except:
                model.fit(X_train, y_train)
                feats = pd.DataFrame({
                    'Columns': X.columns,
                    'Importance': model[-2].feature_importances_
                }).sort_values(by='Importance', ascending=False)
        else:
            try:
                feats = pd.DataFrame({
                    'Columns': X.columns,
                    'Importance': model.feature_importances_
                }).sort_values(by='Importance', ascending=False)
            except:
                model.fit(X_train, y_train)
                feats = pd.DataFrame({
                    'Columns': X.columns,
                    'Importance': model.feature_importances_
                }).sort_values(by='Importance', ascending=False)
            
    mod_scores = {}
    try:
        mod_scores['validation_score'] = val_scores.mean()
        if return_folds:
            mod_scores['fold_scores'] = val_scores
    except:
        mod_scores['validation_score'] = val_score
        
    if return_test_score:
        mod_scores['test_score'] =  model.score(X_test, y_test)
            
    if return_importances:
        return mod_scores, feats
    else:
        return mod_scores

我遇到的奇怪部分是,如果我在 sklearn 中创建一个管道,它会在函数之外的数据集上工作,而不是在其中。例如:

from sklearn.pipeline import make_pipeline
from category_encoders import OrdinalEncoder
from xgboost import XGBClassifier

pipe = make_pipeline(OrdinalEncoder(), XGBClassifier())

X = df.drop('state', axis=1)
y = df['state']

在这种情况下,pipe.fit(X, y) 工作得很好。但是get_val_scores(pipe, X, y) 失败并在标题中显示错误消息。更奇怪的是get_val_scores(pipe, X, y) 似乎可以与其他数据集一起使用,比如泰坦尼克号。模型拟合 X_trainy_train 时会发生错误。

在这种情况下,损失函数是binary:logisticstate 列的值是successfulfailed

【问题讨论】:

  • 您使用的是哪个版本的 XGBoost?我在 1.4.0 中遇到了类似的问题。降级到 1.3.3 解决了我的问题
  • @Kris 这为我解决了这个问题,但知道原因仍然很高兴。
  • 可能是主要版本兼容性问题。我不确定原因和后果。可能会在 github 上发布问题

标签: python scikit-learn xgboost


【解决方案1】:

xgboost 库目前正在更新以修复此错误,因此当前的解决方案是将库降级到旧版本,对我来说,我已经通过降级到 xgboost v0.90 解决了这个问题

尝试通过 cmd 检查您的 xgboost 版本:

python 

import xgboost

print(xgboost.__version__)

exit()

如果版本不是 0.90,则通过以下方式卸载当前版本:

pip uninstall xgboost

安装 xgboost 0.90 版

pip install xgboost==0.90

再次运行您的代码!

【讨论】:

    【解决方案2】:

    此错误将在 XGBoost 1.4.2 中修复

    见:https://github.com/dmlc/xgboost/pull/6927

    【讨论】:

      【解决方案3】:

      我在 macOS Big Sur 上使用 python 3.8.6,刚刚在 xgboost==1.4.0 和 1.4.1 上遇到了这个错误。当我降级到 1.3.3 时,问题就消失了。根据您当前的版本尝试升级或降级。

      【讨论】:

      • 这对我有用,但很高兴知道错误的确切原因是什么。
      【解决方案4】:

      我也遇到了同样的错误;但在我的情况下,通过将 bool 列转换为 numeric 解决了该错误。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-11-16
        • 2018-11-16
        • 2021-07-08
        • 2020-08-06
        • 2012-03-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多