numpy loadtxt  ValueError: setting an array element with a sequence.
Hi,
I am trying to load a tsv file using numpy.loadtxt:
data = np.loadtxt('data.txt',delimiter='\t',dtype=np.float)
And I get:  /usr/lib/python2.6/sitepackages/numpy/lib/io.pyc in loadtxt(fname, dtype, comments, delimiter, converters, skiprows, usecols, unpack) 503 X = X.view(dtype) 504 else: > 505 X = np.array(X, dtype) 506 507 X = np.squeeze(X)
ValueError: setting an array element with a sequence.
/usr/lib/python2.6/sitepackages/numpy/lib/io.py(505)loadtxt()
504 else: > 505 X = np.array(X, dtype) 506 
I am on archlinux using 1.3.0. The file contians integers and floats sperated by tabs.
Ideas?
Thanks! Nathan
Adter trying the same thing in matlab, I realized that my "tsv" file is not matrixstyle. But this I mean, not all lines ave the same lenght (not the same number of values).
What would be the best way to load this?
Regards, Nathan
On Thu, 29 Oct 2009 05:30:09 0700 (PDT), TheLonelyStar nabble2@lonelystar.org wrote:
Adter trying the same thing in matlab, I realized that my "tsv" file is
not
matrixstyle. But this I mean, not all lines ave the same lenght (not the same number of values).
What would be the best way to load this?
Regards, Nathan
Use the numpy fromfile function :
For instance I read the file :
5 8 5 5.5 6.1 3 5.5 2 6.5
with : x=npy.fromfile("test.txt",sep="\t")
and it returns an array x :
array([ 5. , 8. , 5. , 5.5, 6.1, 3. , 5.5, 2. , 6.5])
You can reshape this array to a 3x3 matrix using the reshape function >
x.reshape((3,3))
On 10/29/2009 07:30 AM, TheLonelyStar wrote:
Adter trying the same thing in matlab, I realized that my "tsv" file is not matrixstyle. But this I mean, not all lines ave the same lenght (not the same number of values).
What would be the best way to load this?
Regards, Nathan
Hi, Really you have to find the reason why there are extra values in some rows compared to other rows. There have been some recent changes in numpy.genfromtxt that I would strong suggest using. It will indicate any problem rows that you can fix or just ignore.
Regards Bruce
On Oct 29, 2009, at 8:30 AM, TheLonelyStar wrote:
Adter trying the same thing in matlab, I realized that my "tsv" file is not matrixstyle. But this I mean, not all lines ave the same lenght (not the same number of values).
What would be the best way to load this?
The SVN version of np.genfromtxt will let you know where some rows are longer than others. You can decide what to do from then (ignore the corresponding rows or modify your file). The .fromfile approach is a solution if you don't really care about getting a 2D array (or structured 1D array with different fields for ints and floats on a same row), as a previous poster illustrated.
participants (4)

Bruce Southey

Peter Schmidtke

Pierre GM

TheLonelyStar