使用 Catboost 分类器转换分类列
Using Catboost Classifier to convert categorical columns
我正在尝试将 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 似乎更擅长处理这个问题并且不太容易过度拟合模型。
我的数据由三列组成,'Country'、'year'、'phone users'。目标是 'Country','year' 和 'phone users' 是特征。
数据:
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 吗?我在这里错过了什么?
如您问题中包含的错误消息中所述,所有分类特征都必须是字符串类型。要将 'phone users'
(或任何其他数据框列)转换为字符串,您可以使用 df['phone users'] = df['phone users'].astype(str)
.
CatBoost 然后将使用单热编码或目标编码对每个分类特征进行内部编码,具体取决于它采用的唯一值的数量。无需使用 LabelEncoder
或 OneHotEncoder
预先对分类特征进行编码,有关详细信息,请参阅 CatBoost documentation。
我正在尝试将 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 似乎更擅长处理这个问题并且不太容易过度拟合模型。
我的数据由三列组成,'Country'、'year'、'phone users'。目标是 'Country','year' 和 'phone users' 是特征。
数据:
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 吗?我在这里错过了什么?
如您问题中包含的错误消息中所述,所有分类特征都必须是字符串类型。要将 'phone users'
(或任何其他数据框列)转换为字符串,您可以使用 df['phone users'] = df['phone users'].astype(str)
.
CatBoost 然后将使用单热编码或目标编码对每个分类特征进行内部编码,具体取决于它采用的唯一值的数量。无需使用 LabelEncoder
或 OneHotEncoder
预先对分类特征进行编码,有关详细信息,请参阅 CatBoost documentation。