以下是我试图用来获取结果的代码,我收到错误:
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))
我需要一种方法来自动执行任何大小的数组。
答案 0 :(得分:1)
您收到错误是因为您试图将费率四舍五入到1093.60位小数(第4次迭代时为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