Skip to content

Instantly share code, notes, and snippets.

@Keiku
Created April 12, 2017 06:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Keiku/f5f9c75055645f6c6efab88561a8f302 to your computer and use it in GitHub Desktop.
Save Keiku/f5f9c75055645f6c6efab88561a8f302 to your computer and use it in GitHub Desktop.
Extract the one-hot encoding vector.
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
X_str = np.array([['a', 'dog', 'red'], ['b', 'cat', 'green']])
# transform to integer
X_int = LabelEncoder().fit_transform(X_str.ravel()).reshape(*X_str.shape)
# transform to binary
X_bin = OneHotEncoder().fit_transform(X_int).toarray()
print(X_bin)
# [[ 1. 0. 0. 1. 0. 1.]
# [ 0. 1. 1. 0. 1. 0.]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment