tf.keras.utils.to_categorical
Stay organized with collections
Save and categorize content based on your preferences.
Converts a class vector (integers) to binary class matrix.
tf.keras.utils.to_categorical(
y, num_classes=None, dtype='float32'
)
E.g. for use with categorical_crossentropy.
Usage Example:
y = [0, 1, 2, 3]
tf.keras.utils.to_categorical(y, num_classes=4)
array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]], dtype=float32)
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' .
|
Returns |
A binary matrix representation of the input. The classes axis is placed
last.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2020-10-01 UTC.
[null,null,["Last updated 2020-10-01 UTC."],[],[],null,["# tf.keras.utils.to_categorical\n\n\u003cbr /\u003e\n\n|---------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------|\n| [TensorFlow 1 version](/versions/r1.15/api_docs/python/tf/keras/utils/to_categorical) | [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.2.0/tensorflow/python/keras/utils/np_utils.py#L24-L61) |\n\nConverts a class vector (integers) to binary class matrix.\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.keras.utils.to_categorical`](/api_docs/python/tf/keras/utils/to_categorical)\n\n\u003cbr /\u003e\n\n tf.keras.utils.to_categorical(\n y, num_classes=None, dtype='float32'\n )\n\nE.g. for use with categorical_crossentropy.\n\n#### Usage Example:\n\n y = [0, 1, 2, 3]\n tf.keras.utils.to_categorical(y, num_classes=4)\n array([[1., 0., 0., 0.],\n [0., 1., 0., 0.],\n [0., 0., 1., 0.],\n [0., 0., 0., 1.]], dtype=float32)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Arguments --------- ||\n|---------------|------------------------------------------------------------------------------|\n| `y` | class vector to be converted into a matrix (integers from 0 to num_classes). |\n| `num_classes` | total number of classes. |\n| `dtype` | The data type expected by the input. Default: `'float32'`. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A binary matrix representation of the input. The classes axis is placed last. ||\n\n\u003cbr /\u003e"]]