Creating a grid type 3D data array from data points

Click For 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
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 2 ·
Replies
2
Views
1K
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 25 ·
Replies
25
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K