在numpy数组上使用numpy.rate意外返回nan [英] Using numpy.rate, on numpy array returns nan's unexpectedly
问题描述
我正在使用np.rate函数来计算简单的增长率.我正在尝试使用向量运算.这有效:
I'm using np.rate function to calculate simple growth rates. I'm trying to use vector operations. This works:
b = np.arange(1,10)
np.rate(1,0,-b[:-1],b[1:])
这将返回:
array([1.,0.5,0.33333333,0.25,0.2,0.16666667,0.14285714,0.125])
This returns:
array([ 1. , 0.5 , 0.33333333, 0.25 , 0.2 ,
0.16666667, 0.14285714, 0.125 ])
但是,这不符合我的预期:
However, this doesn't work as I'm expecting:
b = np.arange(10)
np.rate(1,0,-b[:-1],b[1:])
返回:
数组([nan,nan,nan,nan,nan,nan,nan,nan,nan])
Returns:
array([ nan, nan, nan, nan, nan, nan, nan, nan, nan])
有什么想法可以最好地解决这个问题?我假设第一个索引位置的0给np.rate函数带来了一些问题,但是我不确定纠正此问题的最佳方法是什么.感谢您的帮助.
Any thoughts to best remedy this? I'm assuming the 0 in the first index position is giving the np.rate function some problems but I'm not sure what the best way to correct this is. Your help is appreciated.
推荐答案
第二种情况在 b
中为零.
np.rate不能在那里处理0值.
np.rate can't handle a 0 value there.
正如我从np.rate的简要说明中了解到的那样,您正在要求它找到在单个支付期内将0变成1所需的利率.这是您要解决的具有挑战性的问题.
As well as I can understand from the (brief) documentation of np.rate, you're asking it to find what interest rate is needed to turn 0 into 1 in a single pay period. This is a challenging problem you're asking it to solve.
要解决此问题,您只需要在此处不设置0.
To fix it, you simply need to not have a 0 there.
这篇关于在numpy数组上使用numpy.rate意外返回nan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!