Created
April 12, 2017 06:30
-
-
Save Keiku/f5f9c75055645f6c6efab88561a8f302 to your computer and use it in GitHub Desktop.
Extract the one-hot encoding vector.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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