5 examples of 'keras.utils.to_categorical' in Python

Every line of 'keras.utils.to_categorical' 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
20def to_categorical(y, n_class):
21 return keras.utils.to_categorical(y, n_class)
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
35def _to_categorical(x, n_classes):
36 x = np.array(x, dtype=int).ravel()
37 n = x.shape[0]
38 ret = np.zeros((n, n_classes))
39 ret[np.arange(n), x] = 1
40 return ret
137def to_categorical(y, nb_classes=None):
138 y = np.asarray(y, dtype='int32')
139
140 if not nb_classes:
141 nb_classes = np.max(y) + 1
142
143 Y = np.zeros((len(y), nb_classes))
144 for i in range(len(y)):
145 Y[i, y[i]] = 1.
146
147 return Y
27def preprocess_dataset_labels(y): # Do not preprocess labels here! => it's done in another script
28 y = to_categorical(y, num_classes)
29 return y

Related snippets