【发布时间】:2020-09-11 09:45:38
【问题描述】:
我想从现有数据中构建一个已知坐标的数组,有什么方法可以避免所有这些循环以使其更快?尝试使用 np.where,接近但没有到达那里。 谢谢!
import numpy as np
import matplotlib.pyplot as plt
x = np.array([[0, 1, 3, 6], [0, 1, 2, 4], [0, 1, 2, 3]])
y = np.array([[0, 2, 0, 1], [1, 2, 1, 1], [2, 3, 2, 4]])
z = np.array([[100, 100, 100, 100], [100, 150, 100, 100], [100, 100, 100, 200]])
xx = np.arange(np.min(x), np.max(x) + 1, 1)
yy = np.arange(np.min(y), np.max(y) + 1, 1)
grid_x, grid_y = np.meshgrid(xx, yy)
L1, C1 = x.shape
L2, C2 = grid_x.shape
v = np.zeros((L2, C2))
n = np.zeros((L2, C2))
for l2 in range(L2):
for c2 in range(C2):
for l1 in range(L1):
for c1 in range(C1):
if grid_x[l2, c2] == x[l1, c1] and grid_y[l2, c2] == y[l1, c1]:
v[l2, c2] = v[l2, c2] + z[l1, c1]
n[l2, c2] = n[l2, c2] + 1
v = v/n
plt.imshow(v)
plt.show()
【问题讨论】:
-
添加实际输出和预期或期望输出
-
这将返回所需的输出,处理数千行和列太慢了。
-
你能详细说明“接近但没有到达那里。”
标签: python arrays numpy search