AttributeError Traceback (most recent call last) Cell In[88], line 87 85 real_y=a 86 real_predict=test_predict ---> 87 print("置信水平:{},预测准确率:{}".format(0.2,round(comput_acc(real_y,real_predict,0.2)* 100,2)),"%") 89 print("预测下一个月的销量为:", predicted_sales) Cell In[88], line 80, in comput_acc(real, predict, level) 78 num_error=0 79 for i in range(len(real)): ---> 80 if abs(real[i]-predict[i])/real[i]>level.all(): 81 num_error+=1 82 return 1-num_error/len(real) AttributeError: 'float' object has no attribute 'all'
时间: 2023-12-11 15:03:06 浏览: 62
test_XML.rar_c 读写xml_test_xml_test_xml.rar_读写xml
这个错误是由于你在使用numpy的all函数时,将level参数传递为float类型,而all函数要求参数为数组类型。你需要将level参数设置为数组类型,例如:np.array([level]).all()。你可以尝试修改代码如下:
```
import numpy as np
def comput_acc(real, predict, level):
num_error = 0
for i in range(len(real)):
if abs(real[i] - predict[i]) / real[i] > np.array([level]).all():
num_error += 1
return 1 - num_error / len(real)
real_y = [1, 2, 3, 4, 5]
real_predict = [0.9, 2.1, 2.8, 4.2, 4.6]
print("置信水平:{},预测准确率:{}".format(0.2, round(comput_acc(real_y, real_predict, 0.2) * 100, 2)), "%")
```
这样修改后,你就可以成功地运行代码了。
阅读全文