# feature\_columns

## Feature columns

### 输入, 特征与模型

回想使用xgboost等机器学习模型训练时, 拿到的数据集中有各种**数值型**和**类别型**等初始特征. 一般来说, 我们拿到数据集后, 不会直接就上模型开始训练了, 而是在经过细致的数据分析后, 确定特征工程的思路, 对每个原始特征进行转换, 得到新的特征. 例如:

* 数值型特征
  * Normalization
  * 分箱, 离散化
* 类别型特征
  * one-hot
  * multi-hot
  * 查找Embedding矩阵, 将每种类型转换为对应的Embedding向量
* 交叉: 两两特征之间交叉产生新的特征

经过转化得到新特征, 此时的数据集是一个**数值类型的稠密矩阵**, 才能feed给神经网络. 从数据流的角度来看, 输入是原始数据中的每一列, 而模型需要是的稠密矩阵作为输入层, 而**原始输入**和**稠密矩阵输入层**之间的连接, 或者说特征工程部分, 可以通过`tf.feature_columns`模块完成.

因此总结为feature\_columns的作用总结为: feature\_columns是原始数据和模型之间的桥梁. 这里的模型可以是Estimator API构建的模型, 也可以是用朴素的tensorflow或keras构建的模型, 两者相比只是在使用`tf.feature_columns`的方法上有所区别. 后续内容会体现出这个细节, 整体来是`tf.feature_columns`与Estimator API配合使用更为方便.

![](https://1942165044-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MI7KRyeBH5dlW-CkUtn%2Fsync%2Fc386231355ab6bcdd8bd43c64cee704efb8f6ee8.png?generation=1618938331386699\&alt=media)

## 参考资料

* [tensorflow feature\_column详解](https://github.com/AlbertBJ/tensorflow-summary/blob/master/feature_column/feature_column.ipynb)
* [tensorflow 特征工程: feature\_column](https://blog.csdn.net/cjopengler/article/details/78161748)
* [tensorflow feature\_column踩坑合集](https://www.cnblogs.com/gogoSandy/p/12435372.html)
* [Tensorflow-FeutureColumns-数据格式](https://www.jianshu.com/p/fceb64c790f3)
* [TF1.15 - Module: tf.feature\_column](https://tensorflow.google.cn/versions/r1.15/api_docs/python/tf/feature_column)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kerasnoone.gitbook.io/garnet/gong-cheng-zhan/wang-luo-kuang-jia/tensorflow/featurecolumns-jian-yao-shuo-ming.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
