AttributeError Traceback (most recent call last) Cell In[37], line 36 34 #%% 35 Pano_len = len(Pano_set_org) ---> 36 path_mat = np.zeros([Pano_len,Pano_len]).astype(np.bool) 37 for ind in list(Pano_set_org): 38 match_all_pd_start = match_all_pd_current[match_all_pd_current.pic_id1 == ind] File C:\Anaconda3\envs\pytorch\lib\site-packages\numpy\__init__.py:305, in __getattr__(attr) 300 warnings.warn( 301 f"In the future `np.{attr}` will be defined as the " 302 "corresponding NumPy scalar.", FutureWarning, stacklevel=2) 304 if attr in __former_attrs__: --> 305 raise AttributeError(__former_attrs__[attr]) 307 # Importing Tester requires importing all of UnitTest which is not a 308 # cheap import Since it is mainly used in test suits, we lazy import it 309 # here to save on the order of 10 ms of import time for most users 310 # 311 # The previous way Tester was imported also had a side effect of adding 312 # the full `numpy.testing` namespace 313 if attr == 'testing': AttributeError: module 'numpy' has no attribute 'bool'. `np.bool` was a deprecated alias for the builtin `bool`. To avoid this error in existing code, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here. The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
时间: 2024-02-14 19:19:03 浏览: 127
这个错误是因为你使用了已经被弃用的np.bool,你需要将其改为Python内置的bool类型。你可以将代码中的以下语句:
```
path_mat = np.zeros([Pano_len,Pano_len]).astype(np.bool)
```
改为:
```
path_mat = np.zeros([Pano_len,Pano_len], dtype=bool)
```
这样就可以避免这个错误了。
相关问题
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'
这个错误是由于你在使用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)), "%")
```
这样修改后,你就可以成功地运行代码了。
t2.train(train_sents)Traceback (most recent call last): File "<input>", line 1, in <module> AttributeError: 'BigramTagger' object has no attribute 'train'如何改正 完整代码如下:import nltk import random from nltk.corpus import brown from nltk import pos_tag, word_tokenize from nltk.tag import DefaultTagger, UnigramTagger, BigramTagger brown_tagged_sents = brown.tagged_sents(categories='news') size = int(len(brown_tagged_sents) * 0.9) train_sents = brown_tagged_sents[:size] test_sents = brown_tagged_sents[size:] t0 = DefaultTagger('NN') t1 = UnigramTagger(train_sents, backoff=t0) t2 = BigramTagger(train_sents, backoff=t1) t2.train(train_sents) Traceback (most recent call last): File "<input>", line 1, in <module> AttributeError: 'BigramTagger' object has no attribute 'train'
The error message indicates that the `BigramTagger` object does not have the `train` method. This is because `BigramTagger` is already trained during initialization.
To fix this error, you can remove the `t2.train(train_sents)` line and directly use the `t2` tagger to tag new sentences. For example:
```
sent = "This is a test sentence"
tokens = word_tokenize(sent)
tags = t2.tag(tokens)
print(tags)
```
阅读全文