为了账号安全,请及时绑定邮箱和手机立即绑定

类型错误:无法解包不可迭代的 NoneType 对象

类型错误:无法解包不可迭代的 NoneType 对象

慕桂英4014372 2021-09-11 14:57:05
我知道以前有人问过这个问题,但我似乎无法让我的工作。import numpy as npdef load_dataset():    def download(filename, source="http://yaan.lecun.com/exdb/mnist/"):        print ("Downloading ",filename)        import urllib        urllib.urlretrieve(source+filename,filename)    import gzip        def load_mnist_images(filename):        if not os.path.exists(filename):            download(filename)        with gzip.open(filename,"rb") as f:            data=np.frombuffer(f.read(), np.uint8, offset=16)                        data = data.reshape(-1,1,28,28)                        return data/np.float32(256)        def load_mnist_labels(filename):            if not os.path.exists(filename):                download(filename)            with gzip.open(filename,"rb") as f:                data = np.frombuffer(f.read(), np.uint8, offset=8)            return data        X_train = load_mnist_images("train-images-idx3-ubyte.gz")        y_train = load_mnist_labels("train-labels-idx1-ubyte.gz")        X_test = load_mnist_images("t10k-images-idx3-ubyte.gz")        y_test = load_mnist_labels("t10k-labels-idx1-ubyte.gz")        return X_train, y_train, X_test, y_testX_train, y_train, X_test, y_test = load_dataset()import matplotlibmatplotlib.use("TkAgg")import matplotlib.pyplot as pltplt.show(plt.imshow(X_train[3][0]))这是我得到的错误:Traceback (most recent call last):  File "C:\Users\nehad\Desktop\Neha\Non-School\Python\Handwritten Digits Recognition.py", line 38, in <module>    X_train, y_train, X_test, y_test = load_dataset()TypeError: cannot unpack non-iterable NoneType object我是机器学习的新手。我只是错过了一些简单的事情吗?我正在为我的学校科学展览尝试手写数字识别项目。
查看完整描述

3 回答

?
慕工程0101907

TA贡献1887条经验 获得超5个赞

我认为你X_train, y_train, X_test, y_test是在你的load_mnist_images函数中定义的,因此不是为你的load_dataset函数定义的。

您应该从X_train = ...to取消缩进 5 行,return X_train, ...然后您的代码可能会更好地工作。


查看完整回答
反对 回复 2021-09-11
?
翻翻过去那场雪

TA贡献2065条经验 获得超14个赞

当您对None( 的NoneType)执行多重分配时,您会收到此错误。例如:

X_train, y_train, X_test, y_test = None

类型错误:无法解包不可迭代的 NoneType 对象

所以如果你得到这个,错误很可能是赋值的右手部分不是你所期望的(没什么)。


查看完整回答
反对 回复 2021-09-11
?
DIEA

TA贡献1820条经验 获得超2个赞

您会收到该错误,因为load_dataset()返回None.

运行这个,你会得到同样的错误:

X_train, y_train, X_test, y_test = None


查看完整回答
反对 回复 2021-09-11
  • 3 回答
  • 0 关注
  • 546 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号