[英]Two for loops on Numpy array
Goodmorning.早上好。 Suppose I have a two-dimensional array (call it MAT(x,y)) created with numpy.假设我有一个使用 numpy 创建的二维数组(称为 MAT(x,y))。 On this array, I have to perform some operations.在这个数组上,我必须执行一些操作。 How can I rewrite the following 2 for loops, for example using np.nditer()
or something else that uses numpy method?如何重写以下 2 个 for 循环,例如使用np.nditer()
或其他使用 numpy 方法的东西? Thank you.谢谢你。
for i in range(x):
for j in range(y):
if i == 0: MAT[i][j] = j
elif j == 0: MAT[i][j] = i
You can simply set first row and first column like this您可以像这样简单地设置第一行和第一列
mat[:,0] = np.arange(0, mat.shape[0])
mat[0,:] = np.arange(0, mat.shape[1])
Example result示例结果
array([[0. , 1. , 2. , 3. , 4. ],
[1. , 0.30487009, 0.97179858, 0.08143348, 0.99363866],
[2. , 0.69357714, 0.98421733, 0.42032313, 0.81041628]])
Since you are simply assigning the values 0,1,... to the first row and column there is no need for a double loop with if conditions inside.由于您只是将值 0,1,... 分配给第一行和第一列,因此不需要内部带有 if 条件的双循环。
Just assign the values to the first row:只需将值分配给第一行:
MAT[0] = np.arange(len(MAT[0])
and to the first column:并到第一列:
MAT[:,0] = np.arange(len(MAT[:,0]))
You can basically assign np.arange()
to an appropriate slicing of your input.您基本上可以将np.arange()
分配给输入的适当切片。
You can do this either hard-coding the 2D nature of your input ( foo2()
), or, for arbitrary dimensions using a dynamically defined slicing ( foon()
):您可以对输入的 2D 特性进行硬编码( foo2()
),也可以使用动态定义的切片( foon()
)对任意维度执行此操作:
import numpy as np
def foo(arr):
ii, jj = arr.shape
for i in range(ii):
for j in range(jj):
if i == 0:
arr[i, j] = j
elif j == 0:
arr[i, j] = i
return arr
def foo2(arr):
ii, jj = arr.shape
arr[:, 0] = np.arange(ii)
arr[0, :] = np.arange(jj)
return arr
def foon(arr):
for i, d in enumerate(arr.shape):
slicing = tuple(slice(None) if j == i else 0 for j in range(arr.ndim))
arr[slicing] = np.arange(d)
return arr
arr = np.zeros((3, 4))
print(foo(arr))
# [[0. 1. 2. 3.]
# [1. 0. 0. 0.]
# [2. 0. 0. 0.]]
print(foo2(arr))
# [[0. 1. 2. 3.]
# [1. 0. 0. 0.]
# [2. 0. 0. 0.]]
print(foon(arr))
# [[0. 1. 2. 3.]
# [1. 0. 0. 0.]
# [2. 0. 0. 0.]]
Note that double slicing (eg MAT[i][j]
), while working, is not as efficient as slicing using a tuple (eg MAT[i, j]
).请注意,双重切片(例如MAT[i][j]
)在工作时不如使用元组切片(例如MAT[i, j]
)高效。
Finally, the nesting of the loops is largely unused in your code, and you could rewrite it with the two loops being separated (which is much more efficient):最后,循环的嵌套在您的代码中基本上没有使用,您可以通过分开的两个循环来重写它(这样效率更高):
def fool(arr):
ii, jj = arr.shape
for i in range(ii):
arr[i, 0] = i
for j in range(jj):
arr[0, j] = j
return arr
This is interesting because if we accelerate the code using Numba:这很有趣,因为如果我们使用 Numba 加速代码:
fool_nb = nb.jit(fool)
fool_nb.__name__ = 'fool_nb'
This results in the fastest approach:这导致最快的方法:
funcs = foo, foo2, foon, fool, fool_nb
shape = 300, 400
for func in funcs:
arr = np.zeros(shape)
print(func.__name__)
%timeit func(arr)
print()
# foo
# 100 loops, best of 3: 6.53 ms per loop
# foo2
# 100000 loops, best of 3: 4.28 µs per loop
# foon
# 100000 loops, best of 3: 6.99 µs per loop
# fool
# 10000 loops, best of 3: 89.8 µs per loop
# fool_nb
# 1000000 loops, best of 3: 1.01 µs per loop
You don't need a loop, just assign to slice您不需要循环,只需分配给切片
MAT[:, 0] = np.arange(x)
MAT[0, :] = np.arange(y)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.