Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无双for循环展平numpy阵列_Python_Numpy - Fatal编程技术网

Python 无双for循环展平numpy阵列

Python 无双for循环展平numpy阵列,python,numpy,Python,Numpy,我有一个二维矩阵。在本例中,假设它是一个随机矩阵 >>> a = np.random.randn(5, 7) >>> a array([[-0.37279322, 0.28619523, -0.05309901, 0.26010327, 0.1846693 , 0.33112176, 0.75814911], [ 1.57001151, -0.86831693, -0.20576395, 1.46450855, -0.01631132,

我有一个二维矩阵。在本例中,假设它是一个随机矩阵

>>> a = np.random.randn(5, 7)
>>> a
array([[-0.37279322,  0.28619523, -0.05309901,  0.26010327,  0.1846693 , 0.33112176,  0.75814911],
       [ 1.57001151, -0.86831693, -0.20576395,  1.46450855, -0.01631132, 3.02790403, -0.65313017],
       [ 0.2362675 , -1.52190536,  0.04687194,  2.01618876,  0.03780218, -0.53041096, -0.30104844],
       [-0.5504834 ,  1.04286156,  1.12863785,  0.89583492,  0.28607363, 1.42858007,  0.28582572],
       [-0.768464  ,  0.31952554,  0.81129581,  0.26239668, -0.23242878, -1.01584339,  0.39573906]])
和两个标签向量:

label_y = np.array([23, 984, 123, 9321, 121238])
label_x = np.array([121, 31312, 9123131, 1111, 1231441, 1929313, 192312312361])
我想将a的元素展平并输出它们的标签索引和值。例如:

23,121,-0.37279322 
23,31312,0.28619523 
23,9123131,-0.05309901 
23,1111,0.26010327
23,1231441,0.1846693
23,1929313,0.33112176
23,192312312361,0.75814911 
984,121,...
...
在numpy中有没有一种不使用for循环的简单方法?

用于创建与
X
Y
标签对应的
2D
网格,然后将它们与
2D
输入数组
a
一起堆叠为列,如下所示-

X,Y = np.meshgrid(label_x,label_y)
out = np.column_stack((Y.ravel(),X.ravel(),a.ravel()))
用于创建对应于
X
Y
标签的
2D
网格,然后将其作为列与
2D
输入数组
a
一起堆叠,如下所示-

X,Y = np.meshgrid(label_x,label_y)
out = np.column_stack((Y.ravel(),X.ravel(),a.ravel()))