【问题标题】:Getting categorical related error when trying to fit XGBoost model when there are no categorical cols在没有分类列时尝试拟合 XGBoost 模型时出现分类相关错误
【发布时间】:2022-07-12 00:19:49
【问题描述】:

我有一个包含以下列 dtype 的数据框

{Int64Dtype(), UInt8Dtype(), dtype('float64'), dtype('int64')}

当我尝试适应 xgb.XGBClassifier() 时,出现以下错误

ValueError: DataFrame.dtypes for data must be int, float, bool or category.  When
categorical type is supplied, DMatrix parameter `enable_categorical` must
be set to `True`. Invalid columns: NAME OF COLS THAT ARE UINT TYPE

【问题讨论】:

    标签: python machine-learning xgboost


    【解决方案1】:

    这是触发警告的代码:

    def _invalid_dataframe_dtype(data: DataType) -> None:
        # pandas series has `dtypes` but it's just a single object
        # cudf series doesn't have `dtypes`.
        if hasattr(data, "dtypes") and hasattr(data.dtypes, "__iter__"):
            bad_fields = [
                str(data.columns[i])
                for i, dtype in enumerate(data.dtypes)
                if dtype.name not in _pandas_dtype_mapper
            ]
            err = " Invalid columns:" + ", ".join(bad_fields)
        else:
            err = ""
    
        type_err = "DataFrame.dtypes for data must be int, float, bool or category."
        msg = f"""{type_err} {_ENABLE_CAT_ERR} {err}"""
        raise ValueError(msg)
    

    (Source.)

    它引用另一个变量_pandas_dtype_mapper,用于决定如何匹配每种数据类型。这是它的定义方式:

    _pandas_dtype_mapper = {
        'int8': 'int',
        'int16': 'int',
        'int32': 'int',
        'int64': 'int',
        'uint8': 'int',
        'uint16': 'int',
        'uint32': 'int',
        'uint64': 'int',
        'float16': 'float',
        'float32': 'float',
        'float64': 'float',
        'bool': 'i',
        # nullable types
        "Int16": "int",
        "Int32": "int",
        "Int64": "int",
        "boolean": "i",
    }
    

    (Source.)

    所以,我们在这里找到了问题所在。它支持 uint 数据类型。它支持可为空的数据类型。但它似乎不支持可为空的 uint 数据类型。

    这提出了两种可能的解决方法:

    1. 使用 int 代替 uint。
    2. 在该列中填写您的空值,并将该列转换为不可为空的数据类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-25
      • 2021-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-12
      • 2022-07-21
      相关资源
      最近更新 更多