Tensorflow 特征列说明

总结以下类型的用法

tf.feature_column.numeric_column、

tf.feature_column.bucketized_column、

tf.feature_column.categorical_column_with_hash_bucket、

tf.feature_column.embedding_column、

tf.feature_column.shared_embedding_columns、

tf.feature_column.indicator_column、

tf.feature_column.crossed_column

以下是对每个特征列的详细说明,包括它们的作用、适用场景、示例,以及在 Wide & Deep 模型中的应用侧。

1. tf.feature_column.numeric_column

  • 作用: 表示数值特征,适用于连续值特征。
  • 应用侧: 通常用于 Deep 侧。
  • 适用场景 :
    • 例子 1: 房价预测中的面积(如平方英尺)。
    • 例子 2: 用户行为分析中的浏览时间(如秒数)。

示例:

python 复制代码
area = tf.feature_column.numeric_column("area")  # 面积
browse_time = tf.feature_column.numeric_column("browse_time")  # 浏览时间

2. tf.feature_column.bucketized_column

  • 作用: 将数值特征分桶(离散化),将连续值映射到离散的区间。
  • 应用侧: 可以用于 Wide 侧和 Deep 侧。
  • 适用场景 :
    • 例子 1: 将用户年龄分段(如18-25、26-35等)。
    • 例子 2: 将收入分段(如低收入、中等收入、高收入)。

示例:

python 复制代码
age = tf.feature_column.numeric_column("age")
age_buckets = tf.feature_column.bucketized_column(age, boundaries=[18, 25, 35, 45, 55, 65])  # 年龄分段

3. tf.feature_column.categorical_column_with_hash_bucket

  • 作用: 表示高基数的分类特征,通过哈希函数将特征值映射到固定数量的桶中。
  • 应用侧: 通常用于 Wide 侧。
  • 适用场景 :
    • 例子 1: 用户ID(如"user_12345")。
    • 例子 2: 产品ID(如"product_67890")。

示例:

python 复制代码
user_id = tf.feature_column.categorical_column_with_hash_bucket("user_id", hash_bucket_size=10000)  # 用户ID
product_id = tf.feature_column.categorical_column_with_hash_bucket("product_id", hash_bucket_size=5000)  # 产品ID

4. tf.feature_column.embedding_column

  • 作用: 将分类特征映射到低维稠密向量(嵌入向量),通常用于深度学习模型。
  • 应用侧: 通常用于 Deep 侧。
  • 适用场景 :
    • 例子 1: 推荐系统中的用户嵌入表示。
    • 例子 2: 自然语言处理中的词嵌入。

示例:

python 复制代码
user_id = tf.feature_column.categorical_column_with_hash_bucket("user_id", hash_bucket_size=10000)
user_id_embedding = tf.feature_column.embedding_column(user_id, dimension=16)  # 用户ID嵌入

5. tf.feature_column.shared_embedding_columns

  • 作用: 将多个分类特征共享同一个嵌入向量空间,适用于多个特征具有相似的语义空间。
  • 应用侧: 通常用于 Deep 侧。
  • 适用场景 :
    • 例子 1: 多语言文本处理中的词嵌入。
    • 例子 2: 推荐系统中用户和物品的共同嵌入。

示例:

python 复制代码
user_id = tf.feature_column.categorical_column_with_hash_bucket("user_id", hash_bucket_size=10000)
product_id = tf.feature_column.categorical_column_with_hash_bucket("product_id", hash_bucket_size=5000)
shared_embedding = tf.feature_column.shared_embedding_columns([user_id, product_id], dimension=16)  # 共享嵌入

6. tf.feature_column.indicator_column

  • 作用: 将分类特征转换为稀疏的独热编码(one-hot encoding),用于表示每个类别的存在与否。
  • 应用侧: 通常用于 Wide 侧。
  • 适用场景 :
    • 例子 1: 处理低基数的分类特征,如性别("male"、"female")。
    • 例子 2: 颜色特征("red"、"green"、"blue")。

示例:

python 复制代码
gender = tf.feature_column.categorical_column_with_vocabulary_list("gender", ["male", "female"])  # 性别
gender_indicator = tf.feature_column.indicator_column(gender)  # 性别独热编码

7. tf.feature_column.crossed_column

  • 作用: 创建交叉特征,用于捕捉多个特征之间的相互关系。
  • 应用侧: 通常用于 Wide 侧。
  • 适用场景 :
    • 例子 1: 捕捉用户特征和产品特征之间的关系(如用户的地理位置和购买的产品)。
    • 例子 2: 组合地理位置和时间段的特征(如"北京-早上")。

示例:

python 复制代码
location = tf.feature_column.categorical_column_with_hash_bucket("location", hash_bucket_size=1000)  # 地理位置
time_of_day = tf.feature_column.categorical_column_with_vocabulary_list("time_of_day", ["morning", "afternoon", "evening", "night"])  # 时间段
location_time_cross = tf.feature_column.crossed_column([location, time_of_day], hash_bucket_size=10000)  # 交叉特征

总结

通过明确每个特征列的作用、适用场景和示例,可以更好地理解如何在 Wide & Deep 模型中有效地使用这些特征列,以提高模型的性能和表达能力。

相关推荐
AAwangzong2 分钟前
AI时代RPA技术解析:如何构建兼顾流程执行与智能决策的自动化平台?
人工智能·经验分享·rpa·智能体
达达表哥9 分钟前
AI综合站商业分发系统v3.1纯净版发布 附完整安装教程(排版修正版)
人工智能
众壹新能源科技25 分钟前
逆变器直流开关柜温度监测与预防:红外巡检发现3个120度热点
运维·人工智能·自动化
NeoGressAI外贸数字化31 分钟前
外贸独立站robots.txt怎么写:6个AI爬虫放行规则
人工智能·爬虫
米软科技1 小时前
数字化转型:多元化集团如何构建高效的AI数字化中台?
大数据·人工智能
幼儿园蛋小黄1 小时前
从 STM32N647 的 AI 部署入门嵌入式:一条可落地的学习路线
人工智能·stm32·学习
Leo.yuan1 小时前
Dora企业级Data Agent应用指南:存量BI复用、私有化部署与权限管理
大数据·人工智能
火山引擎开发者社区1 小时前
从代码执行到任务持续运行:企业级 Agent Sandbox 的云原生实践
人工智能
Nablai1 小时前
LX系列多国语言能力技术解析:ESP32双栈架构下的语种策略设计
人工智能·架构
老牛日常随笔1 小时前
微软开源Flint Chart:AI画图表总是画不对?让编译器来画,AI只写数据就行
人工智能·microsoft