Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.