[英]How to create a uniform grid of n-dimensional vectors using numpy (in other words, uniformly fill a n-dimensional hypercube)?
In one dimension a "grid" would be an array of numbers between let's say 0 and 100. In two dimensions the grid would be an array of points like [0, 0] [0, 1] [0, 2]... [1,0], [1, 1]... [99, 99].在一维中,“网格”将是一个介于 0 和 100 之间的数字数组。在二维中,网格将是一个点数组,例如 [0, 0] [0, 1] [0, 2]... [1,0]、[1、1]...[99、99]。 In three dimensions and more dimensions it would look similar.在三个维度和更多维度中,它看起来很相似。
My current output is like that:我现在的output是这样的:
It doesn't create every combination of values for each value in the nth - 1 column.它不会为第 n - 1 列中的每个值创建每个值组合。
The code I use is:我使用的代码是:
import numpy as np
class Cube:
side_len = 100
def __init__(self, n):
current_point = np.zeros(n)
self.arr = []
for i in range(n):
for j in range(Cube.side_len):
self.arr.append(current_point.copy())
current_point[i] += 1.0
self.arr.append([Cube.side_len for _ in range(n)])
self.arr = np.array(self.arr)
np.random.shuffle(self.arr)
if __name__ == '__main__':
cube(10)
I tried also with meshgrid but I could not understand the documentation.我也尝试使用meshgrid ,但我无法理解文档。 I wanted it to be a shallow list of points but I get X, Y and I don't get what I am supposed to do with that?我希望它是一个浅表的点列表,但我得到了 X、Y,但我不知道我应该用它做什么?
Here's the way you do it.这是你做的方式。 Meshgrid with 3 dimensions returns a list of three things, which are the values for the 3 axes to get a uniform spread of points.具有 3 个维度的 Meshgrid 返回一个包含三个东西的列表,它们是 3 个轴的值,以获得均匀分布的点。 You can then use vstack
to stack those together, and transpose to get a list of 3D coordinates:然后,您可以使用vstack
将它们堆叠在一起,并转置以获得 3D 坐标的列表:
>>> import numpy as np
>>> a = np.linspace(0,100,101)
>>> x = np.meshgrid( a, a, a )
>>> y = np.vstack(list(map(np.ravel,x))).T
>>> y
array([[ 0., 0., 0.],
[ 0., 0., 1.],
[ 0., 0., 2.],
...,
[100., 100., 98.],
[100., 100., 99.],
[100., 100., 100.]])
>>>
Credit to this post: How to convert the output of meshgrid to the corresponding array of points?感谢这篇文章: 如何将 output 的 meshgrid 转换为相应的点数组?
Remember that the grid is the size of one axis cubed, so these very quickly get large.请记住,网格的大小是一个轴的立方,所以这些很快就会变大。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.