Numpy one hot encoder is only displaying huge arrays with zeros
<p>I have a <code>(400, 1)</code> numpy array that has values such <code>4300, 4450, 4650...</code> where these values can be distributed around 20 classes i.e. <code>4300 class 1, 4450 class 2...</code> and tried the below code to transform that array to one hot encoded list. The <code>y_train_onehot</code> is showing a list of 400 arrays where each array has a size > 4000 i.e. each array has around 4000 zeros. How can I fix that in order to have one hot encoded vectors for each value i.e. <code>4300</code> can be <code>00001</code>.</p>

<pre><code>def convertOneHot(data):
    y=np.array([int(i[0]) for i in data])
    y_onehot=[0]*len(y)
    for i,j in enumerate(y):
        y_onehot[i]=[0]*(y.max() + 1)
        y_onehot[i][j]=1
    return (y,y_onehot)

y_train,y_train_onehot = convertOneHot(data)
</code></pre>

<p><a href="http://i.stack.imgur.com/LvPqs.jpg" rel="nofollow"><img src="http://i.stack.imgur.com/LvPqs.jpg" alt="sample from y_train_onehot"></a></p>

<p>I used Pandas Dummies and it's very simple.</p>

<pre><code>&gt;&gt;&gt; import pandas as pd
&gt;&gt;&gt; s = pd.Series(y_train)
</code></pre>

<p>And then</p>

<pre><code>y_train_onehot = pd.get_dummies(s)
</code></pre>

<p><a href="http://i.stack.imgur.com/Vs5Cw.png" rel="nofollow"><img src="http://i.stack.imgur.com/Vs5Cw.png" alt="hot_encode"></a></p>

python numpy