# tf.feature_column.weighted_categorical_column

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.14.0/tensorflow/python/feature_column/feature_column_v2.py#L1770-L1845) |

Applies weight values to a `CategoricalColumn`. (deprecated)

> [!WARNING]
> **Warning:** tf.feature_column is not recommended for new code. Instead, feature preprocessing can be done directly using either [Keras preprocessing
> 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.

#### View aliases


**Compat aliases for migration**

See
[Migration guide](https://www.tensorflow.org/guide/migrate) for
more details.

[`tf.compat.v1.feature_column.weighted_categorical_column`](https://www.tensorflow.org/api_docs/python/tf/feature_column/weighted_categorical_column)

<br />

    tf.feature_column.weighted_categorical_column(
        categorical_column,
        weight_feature_key,
        dtype=https://www.tensorflow.org/versions/r2.14/api_docs/python/tf/dtypes#float32
    )

> [!WARNING]
> **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`](https://www.tensorflow.org/versions/r2.14/api_docs/python/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.

Use this when each of your sparse inputs has both an ID and a value. For
example, if you're representing text documents as a collection of word
frequencies, you can provide 2 parallel sparse input features ('terms' and
'frequencies' below).

#### Example:

Input `tf.Example` objects:

    [
      features {
        feature {
          key: "terms"
          value {bytes_list {value: "very" value: "model"} }
        }
        feature {
          key: "frequencies"
          value {float_list {value: 0.3 value: 0.1} }
        }
      },
      features {
        feature {
          key: "terms"
          value {bytes_list {value: "when" value: "course" value: "human"} }
        }
        feature {
          key: "frequencies"
          value {float_list {value: 0.4 value: 0.1 value: 0.2} }
        }
      }
    ]

    categorical_column = categorical_column_with_hash_bucket(
        column_name='terms', hash_bucket_size=1000)
    weighted_column = weighted_categorical_column(
        categorical_column=categorical_column, weight_feature_key='frequencies')
    columns = [weighted_column, ...]
    features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
    linear_prediction, _, _ = linear_model(features, columns)

This assumes the input dictionary contains a `SparseTensor` for key
'terms', and a `SparseTensor` for key 'frequencies'. These 2 tensors must have
the same indices and dense shape.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `categorical_column` | A `CategoricalColumn` created by `categorical_column_with_*` functions. |
| `weight_feature_key` | String key for weight values. |
| `dtype` | Type of weights, such as [`tf.float32`](https://www.tensorflow.org/versions/r2.14/api_docs/python/tf#float32). Only float and integer weights are supported. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| A `CategoricalColumn` composed of two sparse features: one represents id, the other represents weight (value) of the id feature in that example. ||

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `ValueError` | if `dtype` is not convertible to float. |

<br />