Numpy 如何使用 np.cumprod 重写 python for i in range 函数

Numpy how to use np.cumprod to rewrite python for i in range function

我有两个 python 函数。第一个:

mt = np.array([1, 2, 3, 4, 5, 6, 7])
age, interest = 3, 0.5

def getnpx(mt, age, interest):
    val = 1
    initval = 1
    for i in range(age, 6):
        val = val * mt[i]
        intval = val / (1 + interest) ** (i + 1 - age)
        initval = initval + intval
    return initval

输出为:

48.111111111111114

为了让它更快,我用了numpy来向量化它:

def getnpx_(mt, age, interest):
    print(np.cumprod(mt[age:6]) / (1 + interest)**np.arange(1, 7 - age))
    return 1 + (np.cumprod(mt[age:6]) / (1 + interest)**np.arange(1, 7 - age)).sum()

getnpx_(mt, age, interest)

有效,输出仍然是:

48.111111111111114

但是我不知道如何用 numpy 重写我的第二个函数:

pt1 = np.array([1, 2, 3, 4, 5, 6, 7])
pt2 = np.array([2, 4, 3, 4, 7, 4, 8])
pvaltable = np.array([0, 0, 0, 0, 0, 0, 0])

def jointpval(pt1, pt2, age1, age2):
    j = age1
    for i in range(age2, 6):
        k = min(j, 135)
        pvaltable[i] = pt1[k] * pt2[i]
        j = j + 1
    return pvaltable

jointpval(pt1, pt2, 3, 4)

输出:

array([ 0,  0,  0,  0, 28, 20,  0])

我希望能够转换循环

for i in range(age2, 6):

类似于:

np.cumprod(pt1[age:6])

最终输出应该是一样的:

array([ 0,  0,  0,  0, 28, 20,  0])

我找到了这个解决方案:

import numpy as np
pt1 = np.array([1, 2, 3, 4, 5, 6, 7])
pt2 = np.array([2, 4, 3, 4, 7, 4, 8])

def jointpval(pt1, pt2, age1, age2):
    pvaltable = np.zeros(len(pt1))
    idx2 = np.arange(age2, 6)
    idx1 = np.arange(len(idx2)) + age1
    idx1 = np.where(idx1 > 135, 135, idx1) 
    pvaltable[idx2] = pt1[idx1] * pt2[idx2]
    return pvaltable

其中 jointpval(pt1, pt2, 3, 4) returns

array([ 0.,  0.,  0.,  0., 28., 20.,  0.])

我建议不要对数组大小进行硬编码。例如:

def getnpx_(mt, age, interest):
    return 1 + (np.cumprod(mt[age:-1]) / (1 + interest)**np.arange(1, len(mt) - age)).sum()

请注意,索引 age:-1mt 的大小无关,并且 mt.size - age 为您省去了每次有不同 mt 时重新编写函数的麻烦.

您对第二种情况的解决方案非常准确。我可能建议使用 np.clip 而不是 where 也将下限设置为零:

def jointpval(pt1, pt2, age1, age2):
    pvaltable = np.zeros(len(pt1))
    idx = np.arange(len(pt2) - age2 - 1)
    idx1 = np.clip(idx + age1, 0, len(pt1) - 1)
    idx2 = idx + age2
    pvaltable[idx2] = pt1[idx1] * pt2[idx2]
    return pvaltable

您也可以使用 np.minimum 直接实现向量化 min

idx1 = np.minimum(idx + age1, len(pt1) - 1)

请记住,范围的上限是唯一的。由于这个原因,您的两个函数都截断了最后一个元素的计算。很可能 idx = np.arange(len(pt2) - age2 - 1)jointpval 中真的应该是 idx = np.arange(len(pt2) - age2),而 mt[age:-1] 应该是 mt[age:],而 np.arange(1, len(mt) - age) 应该是 np.arange(len(mt) - age) ] 在 genpx_.