Wednesday, 15 June 2011

python - genfromtxt generates tuples, so does recfromcsv -



python - genfromtxt generates tuples, so does recfromcsv -

i have csv-file (comma separated) of next structure:

a1,a2,a3

16516.1556163,163163.48315,41816.844334

when seek created 2d matrix out of using numpy converted structured array. according given construction of csv-file used:

y = np.genfromtxt('file.csv', delimiter=',', usecols=(0,2), names=true)

or

y = np.recfromcsv('file.csv', usecols=(0,2)

in both cases array have shape of (1,) instead of (1,2). looking @ dtypes generated genfromtxt or recfromcsv can tell of same dtype ('a1', '<f8').

can tell me on how array right format reading csv?

cheers thomas

when seek reproduce code, get:

in [71]: c = stringio("a1,a2,a3\n16516.1556163,163163.48315,41816.844334") in [72]: x = np.genfromtxt(c,delimiter=',',usecols=(0,2),names=true) out[72]: array((16516.1556163, 41816.844334), dtype=[('a1', '<f8'), ('a3', '<f8')]) in [73]: x.shape out[73]: () in [83]: x.item() out[83]: (16516.1556163, 41816.844334) in [129]: x.reshape((1,)) out[129]: array([(16516.1556163, 41816.844334)], dtype=[('a1', '<f8'), ('a3', '<f8')])

so x record array. 1 info row, has shape () ('scalar'). if had given 2 info rows, shape (2,). if 'names=false', , tell skip 1st line, result (with 2 rows) (2,2).

these '...fromtxt' functions, read file line line, parsing each line , constructing list of lists, e.g. [[1,2,3],[3,4,5]]. goes np.array (with appropriate dtype). , gets .squeeze(), removing singleton dimensions.

it's final squeeze turns array shape (1,) () (or (1,2)->(2,)). can add together dimension in reshape.

to create matrix 2 columns, need skip header , names:

in [121]: x=np.genfromtxt(c,delimiter=',',usecols=(0,2),skip_header=1) in [122]: x out[122]: array([ 16516.1556163, 41816.844334 ]) in [123]: x.shape out[123]: (2,)

python arrays csv numpy

No comments:

Post a Comment