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

新手上路,请多包涵

我知道之前有人问过这个问题,但我似乎无法让我的工作。

 import numpy as np

def 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_test

X_train, y_train, X_test, y_test = load_dataset()

import matplotlib
matplotlib.use("TkAgg")

import matplotlib.pyplot as plt
plt.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

我是机器学习的新手。我只是错过了一些简单的事情吗?我正在为我的学校科学展览尝试一个手写数字识别项目。

原文由 Neha Devi Shakya 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.6k
2 个回答

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

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

原文由 Amaras 发布,翻译遵循 CC BY-SA 4.0 许可协议

当您对 None (属于 NoneType )执行多重赋值时,会出现此错误。例如:

 X_train, y_train, X_test, y_test = None

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

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

原文由 Nicolas Gervais 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题