【问题标题】:What are the best ways to process continues variable and categorical variable in xgboost?在 xgboost 中处理连续变量和分类变量的最佳方法是什么?
【发布时间】:2017-04-26 09:03:28
【问题描述】:

我的数据集有连续变量,比如年龄从 0 到 100,数据也有分类变量,比如有 50 个类的省份。所以我不知道是否需要将 continue 变量处理到 bin 中。以及处理省份的最佳方式是什么。 Xgboost 不能处理字符串类型的变量。对于这么多类型的省份,我应该使用one-hot编码吗?

【问题讨论】:

    标签: python pandas machine-learning feature-selection xgboost


    【解决方案1】:

    IIUC 你可以factorize你的类别:

    In [1]: prv = pd.Series(['C','A','C','B','A'], dtype='category')
    
    In [2]: prv
    Out[2]:
    0    C
    1    A
    2    C
    3    B
    4    A
    dtype: category
    Categories (3, object): [A, B, C]
    
    In [3]: pd.factorize(prv)[0].astype(np.uint8)
    Out[3]: array([0, 1, 0, 2, 1], dtype=uint8)
    

    如果您想在分解类别之前对其进行排序(即“A”- 0、“B”- 1 等):

    In [4]: pd.factorize(prv, sort=True)[0].astype(np.uint8)
    Out[4]: array([2, 0, 2, 1, 0], dtype=uint8)
    

    您也可以将类别转换为数字:

    In [12]: prv.cat.rename_categories(range(prv.nunique()))
    Out[12]:
    0    2
    1    0
    2    2
    3    1
    4    0
    dtype: category
    Categories (3, int64): [0, 1, 2]
    

    【讨论】:

      【解决方案2】:

      您应该对分类值进行 One Hot Encode。

      XGBoost 和一般的 GBM 将难以处理高维类别。

      在这种情况下,您可以将这些分类值提供给预模型,然后使用 xgboost

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-10-18
        • 2021-10-30
        • 2020-08-30
        • 1970-01-01
        • 2022-08-16
        • 2013-02-27
        • 1970-01-01
        相关资源
        最近更新 更多