在《Hands on machine Learning with scikit-learn and tensorflow 2.0》一书的第 17 章中,他们使用 tf.data.Dataset 和 window() 方法将顺序数据集拆分为多个窗口:
n_steps = 100
window_length = n_steps + 1 # target = input shifted 1 character ahead
dataset = dataset.window(window_length, shift=1, drop_remainder=True)
Run Code Online (Sandbox Code Playgroud)
为了将顺序数据集分割成多个窗口,他们使用了以下方法:
dataset = dataset.flat_map(lambda window: window.batch(window_length))
Run Code Online (Sandbox Code Playgroud)
但是当我执行上面的那一行时,出现以下错误:
AttributeError Traceback (most recent call last)
<ipython-input-18-5b215fb4cb71> in <module>()
----> 1 dataset = dataset.flat_map(lambda window: window.batch(window_length))
10 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/ops/dataset_ops.py in flat_map(self, map_func)
1650 Dataset: A `Dataset`.
1651 """
-> 1652 return FlatMapDataset(self, map_func)
1653
1654 def interleave(self, …Run Code Online (Sandbox Code Playgroud) 我有一个按钮,它应该返回ask_wikipedia函数,所以我使用了CallbackQueryHandler,但是当我想调用ask_wikipedia函数时,我收到一个属性错误!为什么?我该如何修复它?
def Click_Button(update, context) :
query = update.callback_query
if query.data == "Research":
ask_wikipedia(update, context)
query_handler = CallbackQueryHandler(Click_Button)
dispatcher.add_handler(query_handler)
def ask_wikipedia(update, context) :
update.message.reply_text('What do you want to know about ? ')
return About
Run Code Online (Sandbox Code Playgroud)
当我点击按钮时出现此错误
AttributeError: 'NoneType' object has no attribute 'reply_text'
Run Code Online (Sandbox Code Playgroud)
我该如何解决它?
python attributeerror telegram python-telegram-bot telegram-bot
我需要将以下代码从 PyQt5 (它在那里工作)转换为 PyQt6:
self.setWindowFlags(Qt.FramelessWindowHint)
Run Code Online (Sandbox Code Playgroud)
这是错误:
AttributeError: type object 'Qt' has no attribute 'FramelessWindowHint'
Run Code Online (Sandbox Code Playgroud)
我已经尝试过这个:
self.setWindowFlags(Qt.WindowFlags.FramelessWindowHint)
Run Code Online (Sandbox Code Playgroud)
它说:
AttributeError: type object 'Qt' has no attribute 'WindowFlags'
Run Code Online (Sandbox Code Playgroud) 我正在处理加载到 Python 字典中的 JSON 数据。其中很多都有可选字段,其中可能包含字典之类的东西。
dictionary1 =
{"required": {"value1": "one", "value2": "two"},
"optional": {"value1": "one"}}
dictionary2 =
{"required": {"value1": "one", "value2": "two"}}
Run Code Online (Sandbox Code Playgroud)
如果我这样做,
dictionary1.get("required").get("value1")
Run Code Online (Sandbox Code Playgroud)
显然,这是有效的,因为场"required"总是存在的。
但是,当我使用同一行dictionary2(以获取可选字段)时,这将产生一个AttributeError
dictionary2.get("optional").get("value1")
AttributeError: 'NoneType' object has no attribute 'get'
Run Code Online (Sandbox Code Playgroud)
这是有道理的,因为第一个.get()将返回None,而第二个.get()不能调用.get()None 对象。
我可以通过提供默认值来解决这个问题,以防可选字段丢失,但是数据变得越复杂,这就会很烦人,所以我称之为“天真的修复”:
dictionary2.get("optional", {}).get("value1", " ")
Run Code Online (Sandbox Code Playgroud)
因此,第一个.get()将返回一个空字典{},可以在其上调用第二个字典.get(),并且由于它显然不包含任何内容,因此它将返回空字符串,如第二个默认值所定义的那样。
这将不再产生错误,但我想知道是否有更好的解决方案 - 特别是对于更复杂的情况(value1包含数组或另一个字典等......)
我也可以用 try - except 来解决这个问题AttributeError,但这也不是我喜欢的方式。
try:
value1 = dictionary2.get("optional").get("value1")
except AttributeError:
value1 …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用pyFirmata,但无法让它工作。即使是最基本的库也不起作用。我猜库代码有问题。
from pyfirmata import Arduino,util
import time
port = 'COM5'
board = Arduino(port)
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
Traceback (most recent call last):
File "c:\Users\Public\pythonpublic\arduino.py", line 5, in <module>
board = Arduino(port)
^^^^^^^^^^^^^
File "C:\Users\marce\AppData\Roaming\Python\Python311\site-packages\pyfirmata\__init__.py", line 19, in __init__
super(Arduino, self).__init__(*args, **kwargs)
File "C:\Users\marce\AppData\Roaming\Python\Python311\site-packages\pyfirmata\pyfirmata.py", line 101, in __init__
self.setup_layout(layout)
File "C:\Users\marce\AppData\Roaming\Python\Python311\site-packages\pyfirmata\pyfirmata.py", line 157, in setup_layout
self._set_default_handlers()
File "C:\Users\marce\AppData\Roaming\Python\Python311\site-packages\pyfirmata\pyfirmata.py", line 161, in _set_default_handlers
self.add_cmd_handler(ANALOG_MESSAGE, self._handle_analog_message)
File "C:\Users\marce\AppData\Roaming\Python\Python311\site-packages\pyfirmata\pyfirmata.py", line 185, in add_cmd_handler
len_args = len(inspect.getargspec(func)[0])
^^^^^^^^^^^^^^^^^^
AttributeError: module 'inspect' has no attribute 'getargspec'. Did …Run Code Online (Sandbox Code Playgroud) python 3.10运行venv在Windows 10 pro.
我正在尝试按照教程进行Celery集成Flask:https://flask.palletsprojects.com/en/latest/patterns/celery/
# example.py
from celery import Celery, Task
from flask import Flask
def celery_init_app(app: Flask) -> Celery:
class FlaskTask(Task):
def __call__(self, *args: object, **kwargs: object) -> object:
with app.app_context():
return self.run(*args, **kwargs)
celery_app = Celery(app.name, task_cls=FlaskTask)
celery_app.config_from_object(app.config["CELERY"])
celery_app.set_default()
app.extensions["celery"] = celery_app
return celery_app
def create_app() -> Flask:
app = Flask(__name__)
app.config.from_mapping(
CELERY=dict(
# Redis Docker container connection string
broker_url="redis://default:redispw@localhost:55000",
result_backend="redis://default:redispw@localhost:55000",
task_ignore_result=True,
),
)
app.config.from_prefixed_env()
celery_init_app(app)
return …Run Code Online (Sandbox Code Playgroud) 我按照这篇文章尝试从外部网站加载图像.我试图从外部链接拉出所有图像.我使用BeautifulSoup来解析链接并获得所有必需的链接.
在视图调用代码末尾的render()函数之前,image_list和return_dict具有所需的值.但是渲染函数似乎生成了AttributeError异常.请协助.
我收到以下错误:
AttributeError at /post/add_new/
META
Request Method: POST
Request URL: http://localhost:8000/post/add_new/
Django Version: 1.4.1
Exception Type: AttributeError
Exception Value:
META
Exception Location: C:\Python27\lib\urllib2.py in __getattr__, line 225
Python Executable: C:\Python27\python.exe
Python Version: 2.7.3
Python Path:
['C:\\Users\\Talal\\Python Workspace\\talal_ynd',
'C:\\Python27\\lib\\site-packages\\ipython-0.13-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\pyreadline-2.0_dev1-py2.7-win32.egg',
'C:\\Python27\\lib\\site-packages\\pil-1.1.7-py2.7-win32.egg',
'C:\\Python27\\lib\\site-packages\\setuptools-0.6c11-py2.7.egg',
'C:\\windows\\system32\\python27.zip',
'C:\\Python27\\DLLs',
'C:\\Python27\\lib',
'C:\\Python27\\lib\\plat-win',
'C:\\Python27\\lib\\lib-tk',
'C:\\Python27',
'C:\\Python27\\lib\\site-packages']
Server time: Thu, 13 Sep 2012 17:30:45 +0500
Run Code Online (Sandbox Code Playgroud)
这是我的视图文件:
# Create your views here.
from django.shortcuts import render
from django.http import HttpResponseRedirect
from posts.models import Post, PostForm
from …Run Code Online (Sandbox Code Playgroud) 我发现了很多关于这个问题的线索,但所有这些问题都是命名空间.我的问题与命名空间无关.
一个小例子:
import cPickle as pickle
from uncertainties import Variable
class value(Variable):
def __init__(self, args, showing=False):
self.show = showing
Variable.__init__(self, args[0], args[1])
val = value((3,1), True)
print val.nominal_value, val.std_dev(), val.show
fobj = file("pickle.file", "w")
pickle.dump(val, fobj)
fobj.close()
fobj = file("pickle.file", "r")
val = pickle.load(fobj)
fobj.close()
print val.nominal_value, val.std_dev(), val.show
Run Code Online (Sandbox Code Playgroud)
这段代码的输出:
3.0 1.0 True
3.0 1.0
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/usr/lib/python2.7/dist-packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
173 else:
174 filename = fname
--> 175 __builtin__.execfile(filename, *where)
/home/markus/pickle.py in <module>()
19 …Run Code Online (Sandbox Code Playgroud) 我有一个问题 - 当我运行我的pygame程序时,它有一个我在代码中找不到的错误.Error: 'module' object has no attribute 'copy'.我发现代码没有任何问题.有人可以帮我修复错误/错误吗?
我是nltk的新手.我正在尝试一些基础知识.
import nltk
nltk.word_tokenize("Tokenize me")
Run Code Online (Sandbox Code Playgroud)
给我以下错误
Traceback (most recent call last):
File "<pyshell#27>", line 1, in <module>
nltk.word_tokenize("hi im no onee")
File "C:\Python27\lib\site-packages\nltk\tokenize\__init__.py", line 101, in word_tokenize
return [token for sent in sent_tokenize(text, language)
File "C:\Python27\lib\site-packages\nltk\tokenize\__init__.py", line 85, in sent_tokenize
tokenizer = load('tokenizers/punkt/{0}.pickle'.format(language))
File "C:\Python27\lib\site-packages\nltk\data.py", line 786, in load
resource_val = pickle.load(opened_resource)
AttributeError: 'module' object has no attribute 'defaultdict'
Run Code Online (Sandbox Code Playgroud)
请有人帮忙.请告诉我如何解决此错误.
attributeerror ×10
python ×7
arduino ×1
celery ×1
defaultdict ×1
dictionary ×1
django ×1
enums ×1
flask ×1
json ×1
module ×1
nltk ×1
pickle ×1
pyfirmata ×1
pygame ×1
pyqt ×1
pyqt6 ×1
python-2.5 ×1
python-3.11 ×1
redis ×1
telegram ×1
telegram-bot ×1
traceback ×1
urllib2 ×1