Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I know this question has been asked before but I can't seem to get mine to work.

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]))

This is the error I am getting:

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

I am new to machine learning. Did I just miss something simple? I am trying a Handwritten Digit Recognition project for my school Science Exhibition.

load_dataset() doesn't return anything, so by default, it returns None, which you can't unpack. Maybe you're thinking about doing return load_mnist_images("""some-filename""");? But then you'd need a file name. – TrebledJ Dec 8, 2018 at 8:49

You get this error when you perform a multiple assignment to None (which is of NoneType). For instance:

X_train, y_train, X_test, y_test = None
  

TypeError: cannot unpack non-iterable NoneType object

So if you get this, the error is most likely that the right-hand part of the assignment is not what you expected (it's nothing).

If you still want to use one line, you can do this: X_train, y_train, X_test, y_test = None, None, None, None. – Gabriel Mar 8, 2022 at 21:01

I think your X_train, y_train, X_test, y_test are defined inside your load_mnist_imagesfunction, and are thus not defined for your load_dataset function.

You should de-indent your 5 lines from X_train = ... to return X_train, ... and your code might work better then.

Declaring multiple variable in a row comma separated was the issue for me. In other languages like VB using the comma syntax makes the variables variant types (not a bunch of ints). – Jeremy Thompson Sep 7, 2019 at 13:35

Splitting/breaking multiple assignment to None resolves this issue.

e.g. This DOESN'T work:

test_set_raw, test_set_transformed, train_set_raw, train_set_transformed = None

This DOES work (no error):

test_set_raw = None
test_set_transformed = None
train_set_raw = None
train_set_transformed = None

You get that error because load_dataset() returns None.

run this and you get the same error:

X_train, y_train, X_test, y_test = None

In your code: plt.show(plt.imshow(X_train[3][0])) Add: coma before the last closing bracket, this error is showing because you're using a tuple and tuple receive coma after the last element or item in it. Do this: plt.show(plt.imshow(X_train[3][0]),)

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.