8

I'm using genfromtxt to import essentially a 2D array that has all its values listed in a text file of the form (x's and y's are integers):

    x1   y1   z1
    x2   y2   z2
    :    :    :

I'm using the for loop below but I'm pretty sure there must be a one line way to do it. What would be a more efficient way to do this conversion?

raw = genfromtxt(file,skip_header = 6)

xrange = ( raw[:,0].min() , raw[:,0].max() )
yrange = ( raw[:,1].min() , raw[:,1].max() )

Z = zeros(( xrange[1] - xrange[0] +1 , yrange[1] - yrange[0] +1 ))

for row in raw:
    Z[ row[0]-xrange[0] , row[1]-yrange[0] ] = row[2]
1
  • The first question should be why are you using genfromtxt? Are there missing values in the input? Do you really want the starting point for these calculations to be a masked array? Commented May 18, 2011 at 16:30

4 Answers 4

3

You can replace the for loop with the following:

xidx = (raw[:,0]-xrange[0]).astype(int)
yidx = (raw[:,1]-yrange[0]).astype(int)

Z[xidx, yidx] = raw[:,2]
Sign up to request clarification or add additional context in comments.

Comments

0

To import a matrix from a file you can just split the lines and then convert to int.

[[int(i) for i in j.split()] for j in open('myfile').readlines()]

of course, I'm supposing your file contains only the matrix.

At the end, you can convert this 2-D array to numpy.

1 Comment

Why is this better than the easier import function genfromtxt (or loadtxt will do in this case) of numpy?
0

You may try something like this:

>>> Z = zeros((3, 3))
>>> test = array([[0, 1, 2], [1, 1, 6], [2, 0, 4]])
>>> Z[test[:, 0:2].T.tolist()]
array([ 0.,  0.,  0.])
>>> Z[test[:, 0:2].T.tolist()] = test[:, 2]
>>> Z
array([[ 0.,  2.,  0.],
       [ 0.,  6.,  0.],
       [ 4.,  0.,  0.]])

In your case:

Z[(raw[:, 0:2] - minimum(raw[:, 0:2], axis=0)).T.tolist()] = raw[:, 2]

Comments

0

You could also go with numpy.searchsorted which will also allow for non-equally spaced / float data:

raw = genfromtxt(file,skip_header = 6)

xvalues = numpy.sorted(set(raw[:,0]))
xidx = numpy.searchsorted(xvalues, raw[:,0])

yvalues = numpy.sorted(set(raw[:,1]))
yidx = numpy.searchsorted(yvalues, raw[:,1])

Z = numpy.zeros((len(xvalues), len(yvalues)))
Z[xidx, yidx] = raw[:,2]

Otherwise, I would be following Simon's answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.