其他分享
首页 > 其他分享> > kaggle course for feature_Engineering_Skill 1 Categorical Encodings

kaggle course for feature_Engineering_Skill 1 Categorical Encodings

作者:互联网

Skill 1 Categorical Encodings


)

预先的操作

使用LGB进行预测

import lightgbm as lgb

feature_cols = train.columns.drop(‘outcome’)

dtrain = lgb.Dataset(train[feature_cols], label=train[‘outcome’])
dvalid = lgb.Dataset(valid[feature_cols], label=valid[‘outcome’])

param = {‘num_leaves’: 64, ‘objective’: ‘binary’}
param[‘metric’] = ‘auc’
num_round = 1000
bst = lgb.train(param, dtrain, num_round, valid_sets=[dvalid], early_stopping_rounds=10, verbose_eval=False)

以ROC来进行模型的评价

from sklearn import metrics
ypred = bst.predict(test[feature_cols])

score = metrics.roc_auc_score(test[‘outcome’] , ypred)
score

1) Construct features from timestamps

#将时间的变量拆分timestamp data在这里插入图片描述

2) Label Encoding(preprocessing.LabelEncoder()函数,直接用数字不同代表不同)

在这里插入图片描述

3.Create train/validation/test splits

在这里插入图片描述

Skill 1 Categorical Encodings

You are already familiar with the most basic encodings: one-hot encoding and label encoding. In this tutorial, you’ll learn about count encoding, target encoding, and CatBoost encoding.

count_encoded.add_suffix("_count") # 在columns的后面直接加上我们想要的_count

way 1 count encoding

Count Encoding

Count encoding replaces each categorical value with the number of times it appears in the dataset. For example, if the value “GB” occured 10 times in the country feature, then each “GB” would be replaced with the number 10.

We’ll use the categorical-encodings package to get this encoding. The encoder itself is available as CountEncoder. This encoder and the others in categorical-encodings work like scikit-learn transformers with .fit and .transform methods.
在这里插入图片描述
在这里插入图片描述

way 2

Target Encoding

Target encoding replaces a categorical value with the average value of the target for that value of the feature. For example, given the country value “CA”, you’d calculate the average outcome for all the rows with country == 'CA', around 0.28. This is often blended with the target probability over the entire dataset to reduce the variance of values with few occurences.

This technique uses the targets to create new features. So including the validation or test data in the target encodings would be a form of target leakage. Instead, you should learn the target encodings from the training dataset only and apply it to the other datasets.

The category_encoders package provides TargetEncoder for target encoding. The implementation is similar to CountEncoder.

在这里插入图片描述

way 3

CatBoost Encoding

Finally, we’ll look at CatBoost encoding. This is similar to target encoding in that it’s based on the target probablity for a given value. However with CatBoost, for each row, the target probability is calculated only from the rows before it.

在这里插入图片描述

标签:target,encoding,Categorical,feature,value,kaggle,train,Encoding
来源: https://blog.csdn.net/qq_42839893/article/details/112514408