Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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 np.2D数组_Python_Arrays_Numpy - Fatal编程技术网

Python np.2D数组

Python np.2D数组,python,arrays,numpy,Python,Arrays,Numpy,我已使用以下数据创建了一个numpy数组: [[-180. -80. 20. 1. 10. ] [-180. -80. 30.23255814 1. 10. ] [-180. -80. 40.46511628 1. 10. ] ..., [

我已使用以下数据创建了一个numpy数组:

[[-180.          -80.           20.            1.           10.        ]
 [-180.          -80.           30.23255814    1.           10.        ]
 [-180.          -80.           40.46511628    1.           10.        ]
 ..., 
 [  90.           70.          439.53488372    1.           10.        ]
 [  90.           70.          449.76744186    1.           10.        ]
 [  90.           70.          460.            1.           10.        ]]
然后我运行:

print a[np.where(a[-1])]
我希望它能再次打印整个数组,但它只返回5行:

[[-180.          -80.           20.            1.           10.        ]
 [-180.          -80.           30.23255814    1.           10.        ]
 [-180.          -80.           40.46511628    1.           10.        ]
 [-180.          -80.           50.69767442    1.           10.        ]
 [-180.          -80.           60.93023256    1.           10.        ]]
我错过了什么

阵列是使用以下内容创建的:

x = np.linspace(-180,90,27)
y = np.linspace(-80,70,30)
z = np.linspace(20,460,44)
a = np.vstack(np.meshgrid(x,y,z,[1],[10])).reshape(5,-1).T
编辑:
这里的目标是识别最后一个元素大于0的行。正在对数据进行某些处理,最后一列将被更改。

要回答更新后的问题,您可以执行以下操作:

a[a[..., -1] > 0]
中间步骤的示例:

>>> a
array([[4, 0, 3],
       [2, 1, 3],
       [3, 3, 3],
       [4, 2, 2],
       [2, 0, 0],
       [0, 2, 2],
       [0, 4, 2],
       [2, 1, 1],
       [0, 3, 1],
       [3, 2, 0]])
>>> a[..., -1]
array([3, 3, 3, 2, 0, 2, 2, 1, 1, 0])
>>> a[..., -1] > 0
array([ True,  True,  True,  True, False,  True,  True,  True,  True, False], dtype=bool)
>>> a[a[..., -1] > 0]
array([[4, 0, 3],
       [2, 1, 3],
       [3, 3, 3],
       [4, 2, 2],
       [0, 2, 2],
       [0, 4, 2],
       [2, 1, 1],
       [0, 3, 1]])
>>>
回答原问题:

首先,
a[-1]
返回最后一行(不是最后一列):

然后
np。其中
返回此行非零元素的索引,在本例中为每个元素:

>>> np.where(a[-1])
(array([0, 1, 2, 3, 4]),)
索引为
a[index]
将返回与
索引中的值对应的行,在本例中为前五行

要返回整个数组,当前必须跳过一些环,因为默认情况下是“笛卡尔”索引:

a[np.arange(a.shape[0])[..., None], np.arange(a.shape[1])]


NumPy将来可能支持“正交”索引(以及NumPy讨论邮件列表上的长讨论),这将允许更灵活的索引。

如果目标是确定最后一列内容优于0的行,则可以尝试:

a[a[:,-1] > 0] 
或与np相同,其中:

a[np.where(a[:, -1] > 0)]

啊好的。我怎样才能实现我想做的事?你有没有问过你真正想做的事?如果要打印整个阵列,可以编写
print(a)
。。。
a[a[:,-1] > 0] 
a[np.where(a[:, -1] > 0)]