Search code examples
utf-8mnist

Error ! MNIST.npz is not UTF-8 encoded error saving disabled


When I want to load the MNIST.npz dataset with this format (mnist. npz) in Jupyter; I use from below codes to load the file but this error appeared that showed my file does not exist [FileNotFoundError: [Errno 2] No such file or directory: 'MNIST_data/mnist.npz']. When I checked it from my files and tried to open it, another error appeared that said mnist.npz is not UTF-8 encoded saving disabled.

How can I fix it? Help me pls.

My codes in Jupyter:

import numpy as np
path = "MNIST_data/mnist.npz"
with np.load(path, allow_pickle=True) as f:
   print(type(f))
   x_train, y_train = f\['x_train'\], f\['y_train'\]
   x_test, y_test = f\['x_test'\], f\['y_test'\]

Solution

  • The MNIST.npz dataset with npz format is not UTF-8 encoding and for that reason, I couldn't load it with the codes which I shared in my question box. But finally, I found the solution; If you want to solve the error you should use 'ASCII' encoding like the below codes:

    import numpy as np
    path = "File address in your system"
    with np.load(path, allow_pickle=True, encoding='ASCII' ) as f:
      print(type(f))
      x_train, y_train = f\['x_train'\], f\['y_train'\]
      x_test, y_test = f\['x_test'\], f\['y_test'\]
    

    Good luck