向量化此计算,而不是for循环

时间:2019-04-23 15:01:19

标签: python performance loops numpy vectorization

是否有矢量解决方案来替换下面的循环?循环计算大约需要5分钟,而矢量化计算则需要几秒钟。

请注意,计算可以是任何内容(基于2个长度为31的列表)。

#ICEBAL is a PY file
from ICEBAL import get_EdF

这是下面的计算:

actls = [20.00,21.00,22.00,23.00,24.00,25.00,26.00,27.00,28.00,29.00,30.00,31.00,32.00,33.00,34.00,35.00,36.00,37.00,38.00,39.00,40.00,41.00,42.00,43.00,44.00,45.00,46.00,47.00,48.00,49.00,50.00]
preds = [19.00,20.00,21.00,22.00,23.00,24.00,25.00,26.00,27.00,28.00,29.00,30.00,31.00,32.00,33.00,34.00,35.00,36.00,37.00,38.00,39.00,40.00,41.00,42.00,43.00,44.00,45.00,46.00,47.00,48.00,49.00]
EdF = get_EdF(actls,preds)

我想将EdF计算应用于测试集。下面的循环有效,但运行速度太慢。

test['EdF'] = 0
for i in range(0,884):
    test['EdF'].iloc[i] = get_EdF(np.squeeze(actuals[i:i+1]).tolist(),np.squeeze(predictionsneuralnet[i:i+1]).tolist()))

test是标准的Pandas数据集(有884行)。

print(np.array(actuals).shape)
print(np.array(predictionsneuralnet).shape)

两者的输出均为(884,31)。

0 个答案:

没有答案