【发布时间】:2021-05-28 10:33:23
【问题描述】:
我正在努力提高 Python 中逻辑索引的速度。所以,目前我必须绘制一些热图,为此我将输入数据划分为指定数量的 x 和 y 箱,然后通过函数 return_val,我使用逻辑索引来计算平均值给定 bin 中的值
当我的 bin 大小很小时,这很有效,但是当我尝试增加 bin 大小时,比如 100x100,那么程序会变慢很多
我知道可以通过使用 Python 中的 stats.binned_statistic_2d 函数来提高速度。但是,我想了解如何优化我当前的代码以使平均过程更快
import numpy as np
arr_len = 932826
x = np.random.uniform(low=0, high=4496, size=arr_len)
y = np.random.uniform(low=-74, high=492, size=arr_len)
z = np.random.uniform(low=-30, high=97, size=arr_len)
# Check points
bin_x = 10
bin_y = 10
x1 = np.linspace(x.min(), x.max(), bin_x)
y1 = np.linspace(y.min(), y.max(), bin_y)
def return_val(x, y, z, x1, y1, i, j):
idx = np.logical_and(np.logical_and(x > x1[i - 1], x < x1[i]), np.logical_and(y > y1[j - 1], y < y1[j]))
if np.count_nonzero(idx) == 0:
return np.nan
else:
return np.mean(z[idx])
z1 = np.zeros((len(x1), len(y1)))
for i in range(1, len(x1)):
for j in range(1, len(y1)):
z1[i - 1, j - 1] = return_val(x, y, z, x1, y1, i, j)
z1 = z1.transpose()
【问题讨论】:
-
通常主要问题是
for-loops - 它们会大大降低代码速度。
标签: python performance numpy