我有一个 n_years by n_repeats 计数数据数组。
对于每个元素 ( e ),我想从损失严重性数组中抽取e次并取抽取的总和。
以下是迄今为止我能做的最好的。它几乎不比forpython 中的两个嵌套循环快。在我的实际用例中,我的数组是 100,000 x 1,000。
有谁知道如何使用纯 numpy 完成此操作?
frequency = np.array(
[
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 1],
[1, 2, 1],
[1, 2, 1],
[2, 4, 2],
[2, 4, 2],
[3, 5, 2],
]
)
sev = np.array([1,1,2,2,1,2,3,4,5,1,1,2])
def calculate_insured_losses(frequency, severity_array):
def yearly_loss(element, severity_array=severity_array):
return 0 if element == 0 else np.random.choice(severity_array, size=element, replace=True).sum()
return np.vectorize(yearly_loss)(frequency.flatten()).reshape(frequency.shape)
calculate_insured_losses(freq, sev)
每个循环 291 µs ± 10.6 µs(7 次运行的平均值 ± 标准偏差,每次 1000 次循环)
编辑:带有嵌套循环的更简单的代码
def calculate_insured_losses(frequency, severity):
def yearly_loss(element, severity_array=severity):
if element == 0:
return 0
else:
return np.random.choice(severity_array, size=element, replace=True).sum()
n_years, n_repeats = frequency.shape
losses = np.empty(shape=frequency.shape)
for year in range(n_years):
for repeat in range(n_repeats):
losses[year, repeat] = yearly_loss(frequency[year, repeat])
return losses
calculate_insured_losses(freq, sev)
慕斯王
相关分类