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 20:03:06 浏览: 27
这个错误是由于你在使用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)), "%")
```
这样修改后,你就可以成功地运行代码了。
相关问题
AttributeError Traceback (most recent call last
This error occurs when you try to access an attribute or method that does not exist in the object you are using.
To fix this error, you should check the spelling and syntax of the attribute or method you are trying to access. Make sure that it exists in the object you are using. If you are not sure, you can use the `dir()` function to list all the attributes and methods of the object.
If the attribute or method is supposed to exist but is still giving you an error, it could be a problem with the object itself. In this case, you may need to check the documentation or seek help from the developer community.
AttributeError Traceback (most recent call last)
This error occurs when you try to access an attribute or method that does not exist in the object you are working with. It is a common error in Python and can be caused by various reasons such as typos, incorrect usage of variables or objects, or missing imports.
To fix this error, you should carefully check your code and ensure that all the attributes and methods you are calling actually exist in the object you are working with. If you are still unable to find the error, you can try printing out the object and its attributes to see what is going wrong.
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)