Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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 如何有效地将条件应用于numpy数组的索引?_Python_Arrays_Numpy_Matrix - Fatal编程技术网

Python 如何有效地将条件应用于numpy数组的索引?

Python 如何有效地将条件应用于numpy数组的索引?,python,arrays,numpy,matrix,Python,Arrays,Numpy,Matrix,我有一个2D NumPy数组,我想设置数组的值,只要它的索引满足特定条件 我可以通过以下方式使用for循环完成此操作: 但问题是,我想对尺寸约为10000或20000的大型2D阵列执行此操作。所以for循环将非常慢。 如何使用NumPy或任何其他工具在更短的时间内高效地完成这项工作 图书馆 注意:我不希望解决方案创建对角线矩阵,因为我希望将代码应用于许多不同的条件。我正在寻找“有效地将条件应用于numpy数组的索引”的解决方案,这是一种比使用for循环更快的方法。您可以获得如下数组索引: imp

我有一个2D NumPy数组,我想设置数组的值,只要它的索引满足特定条件

我可以通过以下方式使用for循环完成此操作:

但问题是,我想对尺寸约为10000或20000的大型2D阵列执行此操作。所以for循环将非常慢。 如何使用NumPy或任何其他工具在更短的时间内高效地完成这项工作 图书馆


注意:我不希望解决方案创建对角线矩阵,因为我希望将代码应用于许多不同的条件。我正在寻找“有效地将条件应用于numpy数组的索引”的解决方案,这是一种比使用for循环更快的方法。

您可以获得如下数组索引:

import numpy as np

new_a = np.ones((5,10), dtype=np.float32)
indices = np.indices(new_a.shape)
y_indices = indices[0]
x_indices = indices[1]
要获得特定比较适用的索引,您可以:

locations = np.nan_to_num(indices[0] / indices[1]) >= new_a.shape[0] / new_a.shape[1]
要应用这一点,只需:

new_a[locations] = 0
print(new_a)
返回

[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
 [0. 0. 0. 1. 1. 1. 1. 1. 1. 1.]
 [0. 0. 0. 0. 0. 1. 1. 1. 1. 1.]
 [0. 0. 0. 0. 0. 0. 0. 1. 1. 1.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]]

可以获得如下所示的数组索引:

import numpy as np

new_a = np.ones((5,10), dtype=np.float32)
indices = np.indices(new_a.shape)
y_indices = indices[0]
x_indices = indices[1]
要获得特定比较适用的索引,您可以:

locations = np.nan_to_num(indices[0] / indices[1]) >= new_a.shape[0] / new_a.shape[1]
要应用这一点,只需:

new_a[locations] = 0
print(new_a)
返回

[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
 [0. 0. 0. 1. 1. 1. 1. 1. 1. 1.]
 [0. 0. 0. 0. 0. 1. 1. 1. 1. 1.]
 [0. 0. 0. 0. 0. 0. 0. 1. 1. 1.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]]

通过使用np.meshgrid对操作进行矢量化,您可以在额外的内存消耗中获得速度:

xv, yv = np.meshgrid(np.arange(new_a.shape[1]), np.arange(new_a.shape[0]))
idx = np.nan_to_num(yv/xv) >= new_a.shape[0]/new_a.shape[1]
new_a[idx] = 0
print(new_a)
印刷品

>>> new_a
array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [0., 0., 0., 1., 1., 1., 1., 1., 1., 1.],
       [0., 0., 0., 0., 0., 1., 1., 1., 1., 1.],
       [0., 0., 0., 0., 0., 0., 0., 1., 1., 1.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]], dtype=float32)

通过使用np.meshgrid对操作进行矢量化,您可以在额外的内存消耗中获得速度:

xv, yv = np.meshgrid(np.arange(new_a.shape[1]), np.arange(new_a.shape[0]))
idx = np.nan_to_num(yv/xv) >= new_a.shape[0]/new_a.shape[1]
new_a[idx] = 0
print(new_a)
印刷品

>>> new_a
array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [0., 0., 0., 1., 1., 1., 1., 1., 1., 1.],
       [0., 0., 0., 0., 0., 1., 1., 1., 1., 1.],
       [0., 0., 0., 0., 0., 0., 0., 1., 1., 1.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]], dtype=float32)

不,它只适用于矩形阵列。另外,我想根据不同的问题改变条件。如果您告诉我们“如何有效地将条件应用于numpy数组的索引?”的方法,这将非常有用。不,它只适用于矩形数组。另外,我想根据不同的问题改变条件。如果您告诉“如何有效地将条件应用于numpy数组的索引”的方法,这将非常有用。这将导致错误:布尔索引与维度0上的索引数组不匹配;维度为1000,但相应的布尔维度为1。在new_a[idx]=0行上,当我尝试:new_a=np.ones10002000,dtype=np.float32时,我在删除硬编码值时意外删除了一个np.arange。现已修复此错误:布尔索引与维度0上的索引数组不匹配;维度为1000,但相应的布尔维度为1。在new_a[idx]=0行上,当我尝试:new_a=np.ones10002000,dtype=np.float32时,我在删除硬编码值时意外删除了一个np.arange。现在修好了