One Hot Encoding using numpy

2019-01-17 04:12发布

If the input is zero I want to make an array which looks like this:

[1,0,0,0,0,0,0,0,0,0]

and if the input is 5:

[0,0,0,0,0,1,0,0,0,0]

For the above I wrote:

np.put(np.zeros(10),5,1)

but it did not work.

Is there any way in which, this can be implemented in one line?

9条回答
Summer. ? 凉城
2楼-- · 2019-01-17 04:42

I'm not sure the performance, but the following code works and it's neat.

x = np.array([0, 5])
x_onehot = np.identity(6)[x]
查看更多
劫难
3楼-- · 2019-01-17 04:43

Taking a quick look at the manual, you will see that np.put does not return a value. While your technique is fine, you are accessing None instead of your result array.

For a 1-D array it is better to just use direct indexing, especially for such a simple case.

Here is how to rewrite your code with minimal modification:

arr = np.zeros(10)
np.put(arr, 5, 1)

Here is how to do the second line with indexing instead of put:

arr[5] = 1
查看更多
Juvenile、少年°
4楼-- · 2019-01-17 04:43

The np.put mutates its array arg in-place. It's conventional in Python for functions / methods that perform in-place mutation to return None; np.put adheres to that convention. So if a is a 1D array and you do

a = np.put(a, 5, 1)

then a will get replaced by None.

Your code is similar to that, but it passes an un-named array to np.put.

A compact & efficient way to do what you want is with a simple function, eg:

import numpy as np

def one_hot(i):
    a = np.zeros(10, 'uint8')
    a[i] = 1
    return a

a = one_hot(5) 
print(a)

output

[0 0 0 0 0 1 0 0 0 0]
查看更多
登录 后发表回答