以下是我尝试用来获取结果的代码,但出现错误:
import numpy
array = [-1000,0,0,1094.60,0,0,1094.60]
for b in array:
a = round(numpy.irr(array), b-1) * 100
print (round(a,2))
错误:
TypeError: 'float' object cannot be interpreted as an integer
但是,只需替换“b-1”即可使我的代码正常工作,但我无法使用它,因为数组可以尽可能大或尽可能小。我不允许手动输入替换“b-1”的数字。以下是相同的工作代码。
import numpy
array = [-1000,0,0,1094.60,0,0,1094.60]
for b in array:
a = round(numpy.irr(array), 6) * 100
print (round(a,2))
我需要一种方法来自动处理任何大小的数组。
最佳答案
您收到错误是因为您尝试将比率四舍五入到小数点后 1093.60 位(即第四次迭代的 b-1)。
numpy.arr适用于任何大小的数组。你不必给它一个尺寸(你也没有)。据我所知,您正在尝试将利率打印为百分比,四舍五入到小数点后两位。改用这个:
rate = numpy.irr(array) * 100
print "rate =", round(rate, 2)
在我之前给您的代码末尾使用它会给出以下输出进行比较:
Present Value (P) at rate 0.0125 = 0.217803365612
Present Value (P) at rate 0.01875 = -0.143198101517
Present Value (P) at rate 0.015625 = 0.0349275148787
Present Value (P) at rate 0.0171875 = -0.0547196000329
Present Value (P) at rate 0.01640625 = -0.0100432897584
Present Value (P) at rate 0.016015625 = 0.0124051532756
Present Value (P) at rate 0.0162109375 = 0.00117171042648
rate = 1.62
关于python - 计算内部 yield 百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35663778/