By passing in names=True
to genfromtxt
, you're creating a structured array. Your 1680 records will each have 6 fields.
Example:
oliver@armstrong:/tmp$ cat sto.txt
id,num
1,1.2
2,2.4
oliver@armstrong:/tmp$ python
Python 2.7.3 (default, Jun 22 2015, 19:33:41)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> a = np.genfromtxt('/tmp/sto.txt', names=True, delimiter=',')
>>> a.shape
(2,)
>>> a[0] # each record has 2 fields
(1.0, 1.2)
>>> a[0].dtype
dtype([('id', '<f8'), ('num', '<f8')])
>>> a[0]['num']
1.2
You can also access just those fields, and it will tell you how many elements there are per field:
>>> a['num'].shape
(2,)
>>> a['num']
array([ 1.2, 2.4])
Definitely read the documentation on structured arrays if you want to know more. The provided link is full of good examples.
If you know a priori that all elements in that file are floats (and by passing dtype=float
, you indicate that you know), you could convert the structured array to a normal ndarray
:
>>> a = np.genfromtxt('/tmp/sto.txt', names=True, delimiter=',', dtype=float) # added `dtype=float` in the call
>>> b = a.view(np.float64).reshape(a.shape[0], -1)
>>> b
array([[ 1. , 1.2],
[ 2. , 2.4]])
Note that this returns a view, so any change you make to a
will be reflected in b
and vice versa.