【问题标题】:Using Catboost Classifier to convert categorical columns使用 Catboost 分类器转换分类列
【发布时间】:2021-07-08 06:33:54
【问题描述】:

我正在尝试将 CatBoost 应用于我的分类特征列之一,但出现以下错误:

CatBoostError: Invalid type for cat_feature[non-default value idx=0,feature_idx=2]=68892500.0 : cat_features must be integer or string, real number values and NaN values should be converted to string.

我可以使用 one-hot 编码,但这里的许多人说 CatBoost 似乎更擅长处理这个问题并且不太容易过度拟合模型。

我的数据由三列组成,“国家”、“年份”、“电话用户”。 Target 是“Country”,“year”和“phone users”是 Feature。

数据:

Country   year   phone users
Ireland   1989   978
France    1990   854
Spain     1991   882
Turkey    1992   457
...       ...    ...

到目前为止我的代码:

X = df.loc[115:305]
y = df.loc[80:, 0]

cat_features = list(range(0, X_pool.shape[1]))
Output: [0, 1, 2]

X_train, X_val, y_train, y_val = train_test_split(X_pool, y_pool, 
test_size=0.2, random_state=0)

cbc = CatBoostClassifier(iterations=5, learning_rate=0.1)

cbc.fit(X_train, y_train, eval_set=(X_val, y_val), 
cat_features=cat_features, verbose=False)

print("Model Evaluation Stage")

在适应 catboost 模型之前,我需要运行 LabelEncoder 吗?我在这里错过了什么?

【问题讨论】:

    标签: python machine-learning train-test-split catboost


    【解决方案1】:

    正如您问题中包含的错误消息中所述,所有分类特征都必须是字符串类型。要将'phone users'(或任何其他数据框列)转换为字符串,您可以使用df['phone users'] = df['phone users'].astype(str)

    然后,CatBoost 将使用 one-hot 编码或目标编码对每个分类特征进行内部编码,具体取决于它采用的唯一值的数量。无需使用LabelEncoderOneHotEncoder 预先对分类特征进行编码,有关详细信息,请参阅CatBoost documentation

    【讨论】:

      猜你喜欢
      • 2018-04-12
      • 1970-01-01
      • 2021-01-31
      • 1970-01-01
      • 2020-06-11
      • 1970-01-01
      • 2020-03-08
      • 2013-03-10
      • 1970-01-01
      相关资源
      最近更新 更多