SoFunction
Updated on 2024-11-15

An introduction to .to_categorical usage in keras

As shown below:

to_categorical(y, num_classes=None, dtype='float32')

Converts integer labels to onehot. y is an array of int's, num_classes is the total number of label categories, greater than max(y) (labels starting at 0).

Return: if num_classes=None, return len(y) * [max(y)+1] (dimension, m*n means m rows and n columns matrix, the same below), otherwise len(y) * num_classes. it seems complicated to say it out loud, please see the following example.

import keras

ohl=.to_categorical([1,3])
# ohl=.to_categorical([[1],[3]])
print(ohl)
"""
[[0. 1. 0. 0.]
 [0. 0. 0. 1.]]
"""
ohl=.to_categorical([1,3],num_classes=5)
print(ohl)
"""
[[0. 1. 0. 0. 0.]
 [0. 0. 0. 1. 0.]]
"""

The keras source code for this section is below:

def to_categorical(y, num_classes=None, dtype='float32'):
  """Converts a class vector (integers) to binary class matrix.

  . for use with categorical_crossentropy.

  # 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, as a string
      (`float32`, `float64`, `int32`...)

  # Returns
    A binary matrix representation of the input. The classes axis
    is placed last.
  """
  y = (y, dtype='int')
  input_shape = 
  if input_shape and input_shape[-1] == 1 and len(input_shape) > 1:
    input_shape = tuple(input_shape[:-1])
  y = ()
  if not num_classes:
    num_classes = (y) + 1
  n = [0]
  categorical = ((n, num_classes), dtype=dtype)
  categorical[(n), y] = 1
  output_shape = input_shape + (num_classes,)
  categorical = (categorical, output_shape)
  return categorical

Additional knowledge:keras notes - .to_categoracal() function

.to_categoracal (y, num_classes=None, dtype='float32')

Convert shaped labels to onehot, y is an int array, num_classes is the total number of label categories, greater than max (y), (labels starting from 0).

Return:

If num_classes=None, return len(y)*[max(y)+1] (dimension, m*n means m rows and n columns matrix), otherwise len(y)*num_classes.

The above this talk about .to_categorical usage in keras is all I have to share with you, I hope it can give you a reference, and I hope you will support me more.