How to use 'to_categorical in python' in Python

Every line of 'to_categorical in python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
20def to_categorical(y, n_class):
21 return keras.utils.to_categorical(y, n_class)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

165def to_categorical(y, nb_classes):
166 """Convert class vector to binary class matrix.
167
168 If the input ``y`` has shape (``nb_samples``,) and contains integers from 0
169 to ``nb_classes``, the output array will be of dimension
170 (``nb_samples``, ``nb_classes``).
171 """
172
173 y = np.asarray(y, dtype='int32')
174 y_cat = np.zeros((len(y), nb_classes))
175 for i in range(len(y)):
176 y_cat[i, y[i]] = 1.
177 return y_cat

Related snippets