kaggle course for feature_Engineering_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.

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值