Re: [Numpy-discussion] behaviour of np.loadtxt

On Wed, 14 Sep 2011, Davide wrote:
Dear list,
I'm encountering a problem with np.loadtxt.
Suppose i have a file containing three columns of data (and 10 rows), like:
0.001 0.003 0.005 0.001 0.003 0.006 0.002 0.004 0.002 0.004 0.002 0.007 0.001 0.003 0.006 0.002 0.004 0.002 0.004 0.002 0.007 0.001 0.003 0.006 0.002 0.004 0.002 0.004 0.002 0.007
If i give:
len ( np.loadtxt( filename, unpack=True ) )
i get 3, since i have a three rows array. That's fine.
If instead the file has a single column, i.e.,
0.004 0.002 0.007 0.004 0.002 0.007 0.004 0.002 0.007 0.004
the command
len ( np.loadtxt( filename, unpack=True ) )
returns 10, where i would expect it to return 1, to be consistent with the behaviour when there are multiple columns.
Is there a reason for why it is not like that?
Yes, the default for loadtxt is to automatically "squeeze" the output to the minimum no. of dimensions required, thus it returns a 1D-array in the latter case. If you are using numpy 1.6 or later, you can ensure to get a consistent shape by passing the "ndmin=2" option. Cheers, Derek -- ---------------------------------------------------------------- Derek Homeier Centre de Recherche Astrophysique de Lyon ENS Lyon 46, Allée d'Italie 69364 Lyon Cedex 07, France +33 1133 47272-8894 ----------------------------------------------------------------
participants (1)
-
Derek Homeier