Tom Ashley
Tom Ashley

Reputation: 45

Use 3 vectors as columnns in a matrix

I realize this should be really easy, but I have a large dataset (14k points) and I was having memory issues with my dumb new to coding way of doing this.

So. I have three ordered lists, xnew is x coordinates, ynew is y coordinates, and znew is z coordinates. I want an array where each row is one point, with three columns, x, y and z respectively. First I tried this:

points = []    
for point_number in range(len(xnew)):
    points.append((xnew[point_number], ynew[point_number], 
                  znew[point_number]))
    xyz_out = np.array(points)

which worked for small sections of my data, but not for the whole thing.

Right now, I have this:

xyz_out = np.array([xnew, ynew, znew])
xyz_out.transpose((1, 0))
return xyz_out

which, for some reason doesn't transpose my data even though it seems like it should from the transpose documentation. Any suggestions?

Upvotes: 0

Views: 44

Answers (3)

Warren Weckesser
Warren Weckesser

Reputation: 114921

Try column_stack:

xyz = np.column_stack((xnew, ynew, znew))

Upvotes: 1

cd98
cd98

Reputation: 3532

Most of numpy methods will only return a view and won't modify the object in place. See this question and many others for more details. Consider:

import numpy as np
xnew, ynew, znew = range(1000), range(1000), range(1000)
xyz_out = np.array([xnew, ynew, znew])
xyz_out.transpose((1, 0))

The last line only offers you a view of the transpose of your xyz_out array. You may assign it:

xyz_out = xyz_out.transpose((1, 0))

And now it would work. In a 2D case like yours, just write xyz_out = xyz_out.T for transposing.


The numpy transpose is also about 4 or 5 times faster than zip.

%%timeit
xyz_out = np.array([xnew, ynew, znew])
xyz_out = xyz_out.T

%%timeit
xyz_out = np.array(zip(xnew, ynew, znew))

Upvotes: 0

mdml
mdml

Reputation: 22902

An alternate approach to creating the matrix is to take advantage of the built-in zip function:

In [1]: import numpy as np
In [2]: xnew, ynew, znew = range(1000), range(1000), range(1000)
In [3]: xyz = np.array(zip(xnew, ynew, znew))
In [4]: xyz[1, :]
Out[4]: array([1, 1, 1])
In [5]: xyz[2, :]
Out[5]: array([2, 2, 2])

zip will take group the ith value of each of your coordinate vectors into tuples, like so:

>>> zip(xnew, ynew, znew)[0]
(0, 0, 0)

That makes it easy to convert into a numpy.array.

Upvotes: 0

Related Questions