Numpy dtype invalid index
<pre><code>I'm trying to load csv data file:
ACCEPT,organizer@t.net,t,p1@t.net,0,UK,3600000,3,1475917200000,1475920800000,MON,9,0,0,0
</code></pre>

<p>in following way:</p>

<pre><code>dataset = genfromtxt('./training_set.csv', delimiter=',', dtype='a20, a20, a20, a8, i8, a20, i8, i8, i8, i8, a3, i8, i8, i8, i8')
print(dataset)
target = [x[0] for x in dataset]
train = [x[1:] for x in dataset]
</code></pre>

<p>in last line above I've got an error:</p>

<pre><code>---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
&lt;ipython-input-66-5d58edf06039&gt; in &lt;module&gt;()
      4 print(dataset)
      5 target = [x[0] for x in dataset]
----&gt; 6 train = [x[1:] for x in dataset]
      7 
      8 #rf = RandomForestClassifier(n_estimators=100)

&lt;ipython-input-66-5d58edf06039&gt; in &lt;listcomp&gt;(.0)
      4 print(dataset)
      5 target = [x[0] for x in dataset]
----&gt; 6 train = [x[1:] for x in dataset]
      7 
      8 #rf = RandomForestClassifier(n_estimators=100)

IndexError: invalid index
</code></pre>

<p>How to handle this?</p>

<pre><code>n [42]: dataset = np.genfromtxt('./np_inf.txt', delimiter=',', dtype='a20, a20, a20, a8, i8, a20, i8, i8, i8, i8, a3, i8, i8, i8, i8')

In [43]: [x[0] for x in dataset]
Out[43]: ['ACCEPT', 'ACCEPT', 'ACCEPT']
</code></pre>

<p>The issue is that the entries of the <code>dataset</code> are of not very useful type <code>np.void</code>. It does not allow slicing, apparently, but you can iterate over it:</p>

<pre><code>In [56]: type(dataset[0])
Out[56]: numpy.void

In [57]: len(dataset[0])
Out[57]: 15

In [58]: z = [[y for j, y in enumerate(x) if j &gt; 0] for x in dataset]

In [59]: z[0]
Out[59]: 
['organizer@t.net',
 't',
 'p1@t.net',
 0,
 'UK',
 3600000,
 3,
 1475917200000,
 1475920800000,
 'MON',
 9,
 0,
 0,
 0]
</code></pre>

<p>However you're probably better off converting the array to a structured dtype instead of using lists.</p>

<p>Better still, consider using pandas and do <code>pd.read_csv</code>.</p>

python numpy