Feature Engineering 特征工程 2. Categorical Encodings
发布日期:2021-07-01 03:25:55 浏览次数:3 分类:技术文章

本文共 3077 字,大约阅读时间需要 10 分钟。

文章目录

learn from

上一篇:

下一篇:


在里介绍过了Label EncodingOne-Hot Encoding,下面将学习count encoding计数编码,target encoding目标编码、singular value decomposition奇异值分解

在上一篇中使用LabelEncoder(),得分为Validation AUC score: 0.7467

# Label encodingcat_features = ['category', 'currency', 'country']encoder = LabelEncoder()encoded = ks[cat_features].apply(encoder.fit_transform)

1. Count Encoding 计数编码

  • 计数编码,就是把该类型的value,替换为其出现的次数

    例如:一个特征中CN出现了100次,那么就将CN,替换成数值100

  • category_encoders.CountEncoder(),最终得分Validation AUC score: 0.7486

import category_encoders as cecat_features = ['category', 'currency', 'country']count_enc = ce.CountEncoder()count_encoded = count_enc.fit_transform(ks[cat_features])data = baseline_data.join(count_encoded.add_suffix("_count"))# Training a model on the baseline datatrain, valid, test = get_data_splits(data)bst = train_model(train, valid)

2. Target Encoding 目标编码

  • category_encoders.TargetEncoder(),最终得分Validation AUC score: 0.7491

Target encoding replaces a categorical value with the average value of the target for that value of the feature.

目标编码:将会用该特征值的 label 的平均值 替换 分类特征值
For example, given the country value “CA”, you’d calculate the average outcome for all the rows with country == ‘CA’, around 0.28.
举例子:特征值 “CA”,你要计算所有 “CA” 行的 label(即outcome列)的均值,用该均值来替换 “CA”
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.

这种编码方法会产生新的特征,不要把验证集和测试集拿进来fit,会产生
Instead, you should learn the target encodings from the training dataset only and apply it to the other datasets.
应该从训练集里fit,应用到其他数据集

import category_encoders as cecat_features = ['category', 'currency', 'country']# Create the encoder itselftarget_enc = ce.TargetEncoder(cols=cat_features)train, valid, _ = get_data_splits(data)# Fit the encoder using the categorical features and targettarget_enc.fit(train[cat_features], train['outcome'])# Transform the features, rename the columns with _target suffix, and join to dataframetrain = train.join(target_enc.transform(train[cat_features]).add_suffix('_target'))valid = valid.join(target_enc.transform(valid[cat_features]).add_suffix('_target'))train.head()bst = train_model(train, valid)

3. CatBoost Encoding

  • category_encoders.CatBoostEncoder(),最终得分Validation AUC score: 0.7492

This is similar to target encoding in that it’s based on the target probablity for a given value.

跟目标编码类似的点在于,它基于给定值的 label 目标概率
However with CatBoost, for each row, the target probability is calculated only from the rows before it.
计算上,对每一行,目标概率的计算只依靠它之前的行

cat_features = ['category', 'currency', 'country']target_enc = ce.CatBoostEncoder(cols=cat_features)train, valid, _ = get_data_splits(data)target_enc.fit(train[cat_features], train['outcome'])train = train.join(target_enc.transform(train[cat_features]).add_suffix('_cb'))valid = valid.join(target_enc.transform(valid[cat_features]).add_suffix('_cb'))bst = train_model(train, valid)

上一篇:

下一篇:

转载地址:https://michael.blog.csdn.net/article/details/106225290 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Feature Engineering 特征工程 3. Feature Generation
下一篇:LeetCode 416. 分割等和子集(动态规划)

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年04月30日 09时24分43秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

Spring Cloud构建微服务架构:分布式配置中心【Dalston版】 2019-05-03
Spring Cloud构建微服务架构:消息驱动的微服务(核心概念)【Dalston版】 2019-05-03
eclipse中tomcat启动maven项目出现ClassNotFoundException的解决办法 2019-05-03
Spring Cloud构建微服务架构:服务消费(Feign)【Dalston版】 2019-05-03
Spring Cloud构建微服务架构:Hystrix监控数据聚合【Dalston版】 2019-05-03
Spring Cloud构建微服务架构:服务容错保护(Hystrix依赖隔离)【Dalston版】 2019-05-03
Spring Cloud构建微服务架构:分布式服务跟踪(入门)【Dalston版】 2019-05-03
spring boot 自定义banner 2019-05-03
Spring Cloud构建微服务架构:消息驱动的微服务(消费组)【Dalston版】 2019-05-03
Spring Cloud构建微服务架构:分布式服务跟踪(整合zipkin)【Dalston版】 2019-05-03
Spring Cloud构建微服务架构:分布式服务跟踪(整合logstash)【Dalston版】 2019-05-03
Spring Cloud构建微服务架构:Hystrix监控面板【Dalston版】 2019-05-03
阿里P8Java架构师是如何规划架构体系的呢? 2019-05-03
JS正则表达式完整教程 2019-05-03
在线文档预览方案 office web apps 2019-05-03
Spring Boot属性配置文件详解 2019-05-03
Spring Boot中的缓存支持 使用Redis做集中式缓存 2019-05-03
Javascript 总结 常用工具类的封装 2019-05-03
浅谈Java游戏引擎在智能机领域的发展 2019-05-03
Java版推箱子(搬箱子)游戏开发入门示例及源码 2019-05-03