gpt4 book ai didi

python - 提高掩蔽性能,然后加权平均

转载 作者:行者123 更新时间:2023-12-01 01:21:49 25 4
gpt4 key购买 nike

下面定义的函数func()被深深地埋藏在马尔可夫链蒙特卡罗过程中,这意味着它被调用了数百万次。我需要尽可能地提高它的性能,但我还没有找到方法。

掩码 (msk) 的定义和加权平均值大约占用相同的时间。可能没有办法比 numpy 更快地获得加权平均值,但至少可以改进掩码定义吗?

def func(weights, x1, x2, x3, x4):
for x in (x2, x3, x4):
# Mask of the distance between the column '-6' of x1 versus arrays
# x2,x3,x4
msk = abs(x1[-6] - x[-6]) > 0.01
# If the distance in this array is larger than the maximum allowed,
# mask with the values taken from 'x1'.
x[:, msk] = x1[:, msk]

# Weighted average for all the arrays.
avrg_data = np.average(np.array([x1, x2, x3, x4]), weights=weights, axis=0)

return avrg_data


# Random data with proper shape
x1, x2, x3, x4 = np.random.uniform(1., 10., (4, 10, 1000))
weights = np.random.uniform(0.01, .5, 4)

# Call many times and time it
s = t.time()
for _ in range(10000):
func(weights, x1, x2, x3, x4)
print(t.time() - s)

最佳答案

我玩弄了你的代码,碰巧尝试不使用 np.average 直接进行加权平均是否会更快,看起来确实如此。在我的平台上,速度大约快了 40%。

import time as t
import numpy as np

def func(weights, x1, x2, x3, x4):
for x in (x2, x3, x4):
# Mask of the distance between the column '-6' of x1 versus arrays
# x2,x3,x4
msk = abs(x1[-6] - x[-6]) > 0.01

# If the distance in this array is larger than the maximum allowed,
# mask with the values taken from 'x1'.
x[:, msk] = x1[:, msk]

# Weighted average for all the arrays.
avrg_data = np.average(np.array([x1, x2, x3, x4]), weights=weights, axis=0)

return avrg_data

def faster_func(weights, x1, x2, x3, x4):
for x in (x2, x3, x4):
# Mask of the distance between the column '-6' of x1 versus arrays
# x2,x3,x4
msk = abs(x1[-6] - x[-6]) > 0.01

# If the distance in this array is larger than the maximum allowed,
# mask with the values taken from 'x1'.
x[:, msk] = x1[:, msk]

# Scale weights so they add up to 1, then add based on them
weights = weights / np.mean(weights) / 4
avrg_data = x1*weights[0] + x2*weights[1] + x3*weights[2] + x4*weights[3]

return avrg_data

# Random data with proper shape
x1, x2, x3, x4 = np.random.uniform(1., 10., (4, 10, 1000))
weights = np.random.uniform(0.01, .5, 4)

# Call many times and time it
for method in (func, faster_func):
s = t.time()
for _ in range(10000):
method(weights, x1, x2, x3, x4)
print(method, t.time() - s)

# Test that the results are still the same
result1 = func(weights, x1, x2, x3, x4)
result2 = faster_func(weights, x1, x2, x3, x4)
biggest_difference = np.max(abs(result1-result2))
print("Biggest difference between two methods was %.8f" % biggest_difference)

关于python - 提高掩蔽性能,然后加权平均,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53747277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com