Created
April 23, 2018 04:10
-
-
Save okdolly-001/c257d750ca72c69b1e416bbeba2adf51 to your computer and use it in GitHub Desktop.
One-hot encoding #numpy
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
| def get_one_hot(targets, nb_classes): | |
| return np.eye(nb_classes)[np.array(targets).reshape(-1)] | |
| This is what happens: | |
| np.eye(3) | |
| array([[ 1., 0., 0.], | |
| [ 0., 1., 0.], | |
| [ 0., 0., 1.]]) | |
| np.eye(3)[2] | |
| array([ 0., 0., 1.]) | |
| target = np.array([1,2]) | |
| np.eye(3)[target] | |
| array([ 0., 1., 0.],[ 0., 0., 1.]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment