diff --git a/tensorflow/python/keras/utils/np_utils.py b/tensorflow/python/keras/utils/np_utils.py index 6372559ecd1..f2ca5d213e8 100644 --- a/tensorflow/python/keras/utils/np_utils.py +++ b/tensorflow/python/keras/utils/np_utils.py @@ -61,6 +61,22 @@ def to_categorical(y, num_classes=None, dtype='float32'): Returns: A binary matrix representation of the input. The classes axis is placed last. + + Usage example: + + >>> y = [0, 1, 2, 3, 3, 1, 0] + >>> tf.keras.utils.to_categorical(y, 4) + array([[1., 0., 0., 0.], + [0., 1., 0., 0.], + [0., 0., 1., 0.], + [0., 0., 0., 1.], + [0., 0., 0., 1.], + [0., 1., 0., 0.], + [1., 0., 0., 0.]], dtype=float32) + + Raises: + Value Error: If input contains string value + """ y = np.array(y, dtype='int') input_shape = y.shape