Use this when your inputs are in string or integer format, and you have an
in-memory vocabulary mapping each value to an integer ID. By default,
out-of-vocabulary values are ignored. Use either (but not both) of
num_oov_buckets and default_value to specify how to include
out-of-vocabulary values.
For input dictionary features, features[key] is either Tensor or
SparseTensor. If Tensor, missing values can be represented by -1 for int
and '' for string, which will be dropped by this feature column.
Example with num_oov_buckets:
In the following example, each input in vocabulary_list is assigned an ID
0-3 corresponding to its index (e.g., input 'B' produces output 2). All other
inputs are hashed and assigned an ID 4-5.
Example with default_value:
In the following example, each input in vocabulary_list is assigned an ID
0-4 corresponding to its index (e.g., input 'B' produces output 3). All other
inputs are assigned default_value 0.
A unique string identifying the input feature. It is used as the column
name and the dictionary key for feature parsing configs, feature Tensor
objects, and feature columns.
vocabulary_list
An ordered iterable defining the vocabulary. Each feature
is mapped to the index of its value (if present) in vocabulary_list.
Must be castable to dtype.
dtype
The type of features. Only string and integer types are supported. If
None, it will be inferred from vocabulary_list.
default_value
The integer ID value to return for out-of-vocabulary feature
values, defaults to -1. This can not be specified with a positive
num_oov_buckets.
num_oov_buckets
Non-negative integer, the number of out-of-vocabulary
buckets. All out-of-vocabulary inputs will be assigned IDs in the range
[len(vocabulary_list), len(vocabulary_list)+num_oov_buckets) based on a
hash of the input value. A positive num_oov_buckets can not be specified
with default_value.
Returns
A CategoricalColumn with in-memory vocabulary.
Raises
ValueError
if vocabulary_list is empty, or contains duplicate keys.
ValueError
num_oov_buckets is a negative integer.
ValueError
num_oov_buckets and default_value are both specified.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-26 UTC."],[],[],null,["# tf.feature_column.categorical_column_with_vocabulary_list\n\n\u003cbr /\u003e\n\n|--------------------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/feature_column/feature_column_v2.py#L1322-L1438) |\n\nA `CategoricalColumn` with in-memory vocabulary. (deprecated)\n| **Warning:** tf.feature_column is not recommended for new code. Instead, feature preprocessing can be done directly using either [Keras preprocessing\n| layers](https://www.tensorflow.org/guide/migrate/migrating_feature_columns) or through the one-stop utility [`tf.keras.utils.FeatureSpace`](https://www.tensorflow.org/api_docs/python/tf/keras/utils/FeatureSpace) built on top of them. See the [migration guide](https://tensorflow.org/guide/migrate) for details.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.feature_column.categorical_column_with_vocabulary_list`](https://www.tensorflow.org/api_docs/python/tf/feature_column/categorical_column_with_vocabulary_list)\n\n\u003cbr /\u003e\n\n tf.feature_column.categorical_column_with_vocabulary_list(\n key, vocabulary_list, dtype=None, default_value=-1, num_oov_buckets=0\n )\n\n### Used in the notebooks\n\n| Used in the guide | Used in the tutorials |\n|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Migrate \\`tf.feature_column\\`s to Keras preprocessing layers](https://www.tensorflow.org/guide/migrate/migrating_feature_columns) - [Estimators](https://www.tensorflow.org/guide/estimator) | - [Classify structured data with feature columns](https://www.tensorflow.org/tutorials/structured_data/feature_columns) - [Build a linear model with Estimators](https://www.tensorflow.org/tutorials/estimator/linear) - [End to end example for BigQuery TensorFlow reader](https://www.tensorflow.org/io/tutorials/bigquery) |\n\n| **Deprecated:** THIS FUNCTION IS DEPRECATED. It will be removed in a future version. Instructions for updating: Use Keras preprocessing layers instead, either directly or via the [`tf.keras.utils.FeatureSpace`](../../tf/keras/utils/FeatureSpace) utility. Each of `tf.feature_column.*` has a functional equivalent in `tf.keras.layers` for feature preprocessing when training a Keras model.\n\nUse this when your inputs are in string or integer format, and you have an\nin-memory vocabulary mapping each value to an integer ID. By default,\nout-of-vocabulary values are ignored. Use either (but not both) of\n`num_oov_buckets` and `default_value` to specify how to include\nout-of-vocabulary values.\n\nFor input dictionary `features`, `features[key]` is either `Tensor` or\n`SparseTensor`. If `Tensor`, missing values can be represented by `-1` for int\nand `''` for string, which will be dropped by this feature column.\n\nExample with `num_oov_buckets`:\nIn the following example, each input in `vocabulary_list` is assigned an ID\n0-3 corresponding to its index (e.g., input 'B' produces output 2). All other\ninputs are hashed and assigned an ID 4-5. \n\n colors = categorical_column_with_vocabulary_list(\n key='colors', vocabulary_list=('R', 'G', 'B', 'Y'),\n num_oov_buckets=2)\n columns = [colors, ...]\n features = tf.io.parse_example(..., features=make_parse_example_spec(columns))\n linear_prediction, _, _ = linear_model(features, columns)\n\nExample with `default_value`:\nIn the following example, each input in `vocabulary_list` is assigned an ID\n0-4 corresponding to its index (e.g., input 'B' produces output 3). All other\ninputs are assigned `default_value` 0. \n\n colors = categorical_column_with_vocabulary_list(\n key='colors', vocabulary_list=('X', 'R', 'G', 'B', 'Y'), default_value=0)\n columns = [colors, ...]\n features = tf.io.parse_example(..., features=make_parse_example_spec(columns))\n linear_prediction, _, _ = linear_model(features, columns)\n\nAnd to make an embedding with either: \n\n columns = [embedding_column(colors, 3),...]\n features = tf.io.parse_example(..., features=make_parse_example_spec(columns))\n dense_tensor = input_layer(features, columns)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `key` | A unique string identifying the input feature. It is used as the column name and the dictionary key for feature parsing configs, feature `Tensor` objects, and feature columns. |\n| `vocabulary_list` | An ordered iterable defining the vocabulary. Each feature is mapped to the index of its value (if present) in `vocabulary_list`. Must be castable to `dtype`. |\n| `dtype` | The type of features. Only string and integer types are supported. If `None`, it will be inferred from `vocabulary_list`. |\n| `default_value` | The integer ID value to return for out-of-vocabulary feature values, defaults to `-1`. This can not be specified with a positive `num_oov_buckets`. |\n| `num_oov_buckets` | Non-negative integer, the number of out-of-vocabulary buckets. All out-of-vocabulary inputs will be assigned IDs in the range `[len(vocabulary_list), len(vocabulary_list)+num_oov_buckets)` based on a hash of the input value. A positive `num_oov_buckets` can not be specified with `default_value`. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A `CategoricalColumn` with in-memory vocabulary. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|------------------------------------------------------------|\n| `ValueError` | if `vocabulary_list` is empty, or contains duplicate keys. |\n| `ValueError` | `num_oov_buckets` is a negative integer. |\n| `ValueError` | `num_oov_buckets` and `default_value` are both specified. |\n| `ValueError` | if `dtype` is not integer or string. |\n\n\u003cbr /\u003e"]]