Creating a grid type 3D data array from data points

AI Thread Summary
To create a 3D data array from specified ranges of X, Y, and Z values, a numpy array can be generated using a list comprehension. The example provided shows ranges of X from 0 to 5, Y from 0 to 3, and Z from 0 to 2, resulting in a total of 72 data points. While numpy functions like np.indices, np.mgrid, and np.ogrid offer similar functionalities, they do not produce the exact output required. The recommended approach is to use a list comprehension to construct the array in the desired format. This method effectively combines the values into a 2D numpy array with shape 72 x 3.
Arman777
Insights Author
Gold Member
Messages
2,163
Reaction score
191
I have a 3 data column ##(X, Y, Z)## ranges from ##(min, max)##. For example,

##X = (0, 5)##, ##Y=(0, 3)##, ##Z=(0, 2)##. By using them I need to create a numpy array in the form of

##[(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 1, 0), (0, 1, 1), (0, 1, 2), (0, 2, 0)...]##

So in total there will be ##6 \times 4 \times 3 = 72## data points.

Is there a simple command to do this ?
 
Technology news on Phys.org
numpy arrays cannot hold tuples

If you want to create a 2D 72 x 3 numpy array similar to that there is no simple function*; this is a typical exercise for any aspiring coder.

* the np.indices, np.mgrid and np.ogrid functions do something similar, but I don't think there is anything that does exactly this.

Edit: I suppose you could view a list comprehension as a "simple command" (but the word "command" is not appropriate here):
Python:
np.array([[x, y, z] for x in range(6) for y in range(4) for z in range(3)])
 
Last edited:
  • Like
Likes Wrichik Basu
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top