# tf.keras.utils.to_categorical

<br />

|---|---|
| [![](https://www.tensorflow.org/images/tf_logo_32px.png) TensorFlow 2 version](https://www.tensorflow.org/api_docs/python/tf/keras/utils/to_categorical) | [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/python/keras/utils/np_utils.py#L24-L52) |

Converts a class vector (integers) to binary class matrix.

#### View aliases


**Compat aliases for migration**

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

[`tf.compat.v1.keras.utils.to_categorical`](https://www.tensorflow.org/api_docs/python/tf/keras/utils/to_categorical), \`tf.compat.v2.keras.utils.to_categorical\`

<br />

    tf.keras.utils.to_categorical(
        y, num_classes=None, dtype='float32'
    )

E.g. for use with categorical_crossentropy.

<br />

<br />

<br />

| ## Arguments ||
|---|---|
| `y` | class vector to be converted into a matrix (integers from 0 to num_classes). |
| `num_classes` | total number of classes. |
| `dtype` | The data type expected by the input. Default: `'float32'`. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| A binary matrix representation of the input. The classes axis is placed last. ||

<br />