How to use 'convert categorical variable to numeric python sklearn' in Python

Every line of 'convert categorical variable to numeric python sklearn' 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
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
165def to_categorical(y, nb_classes):
166 """Convert class vector to binary class matrix.
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 """
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