ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

kaggle course for feature_Engineering_Skill 1 Categorical Encodings

2021-01-12 13:03:31  阅读:292  来源: 互联网

标签:target encoding Categorical feature value kaggle train Encoding


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有