我尝试将数据填充到numpy数组中。然而,对于更高的索引,它需要越来越多的时间。为什么?
我怎样才能防止这种情况? 我已经在最终维度中创建了数组
import random
import numpy as np
# p = [ ... 2200 values in a python list ... ]
iterations = 1000
max_draws = len(p)-1
percentiles = np.zeros(max_draws)
money_list = np.zeros(iterations)
invest = 100
for k in range(1,max_draws):
print(k)
for j in range(0,iterations):
money_list[j] = (invest * np.random.choice(p, k)).sum()
percentiles[k] = np.percentile(money_list, 5)
我有一个代表股票市场交易收益的因素清单。现在我想知道我必须做多少交易(从可能的交易列表中选择),这样我就有95%的概率赚钱而不赔钱(如果我做了所有的交易,我就赚钱而不赔钱)
在所有建议的改进之后,可以再做一个非常有效的改进
如果您不介意安装和使用非常繁重的额外python pip包numba(通过
python -m pip install numba
),那么您可以大大提高速度,如下面的代码所示Numba设计用于将Python函数预编译为高效的机器代码,也设计用于NumPy。它将python循环转换为快速C代码,并使用LLVM对其进行编译
下一个代码在外循环的
2199
次迭代中实现了4.18x
倍的加速,就像在代码中一样,在5-20次迭代中实现了100x
倍的加速。在我的慢速PC上,使用Numba在90秒内完成所有2199次迭代Try next code here online too!
产出:
相关问题 更多 >
编程相关推荐