Skip to content

Instantly share code, notes, and snippets.

@youngsoul
Created April 11, 2017 02:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save youngsoul/fc69665c5d08e189c57c0db0e93017a6 to your computer and use it in GitHub Desktop.
Save youngsoul/fc69665c5d08e189c57c0db0e93017a6 to your computer and use it in GitHub Desktop.
Download the MNIST data as needed for the 'Hands-On Machine Learning with Scikit-Learn and TensorFlow' book
from six.moves import urllib
from sklearn.datasets import fetch_mldata
import requests
requests.packages.urllib3.disable_warnings()
"""
Adapted from the Github repo:
https://github.com/ageron/handson-ml
for the 03_classification notebook.
This implementation uses the 'requests' package instead of URLLIB
"""
try:
mnist = fetch_mldata('MNIST original')
except urllib.error.HTTPError as ex:
print("Could not download MNIST data from mldata.org, trying alternative...")
# Alternative method to load MNIST, if mldata.org is down
from scipy.io import loadmat
mnist_alternative_url = "https://github.com/amplab/datascience-sp14/raw/master/lab7/mldata/mnist-original.mat"
mnist_path = "./mnist-original.mat"
response = requests.get(mnist_alternative_url)
with open(mnist_path, "wb") as f:
content = response.content
f.write(content)
mnist_raw = loadmat(mnist_path)
mnist = {
"data": mnist_raw["data"].T,
"target": mnist_raw["label"][0],
"COL_NAMES": ["label", "data"],
"DESCR": "mldata.org dataset: mnist-original",
}
print("Success!")
Copy link

ghost commented Jul 22, 2017

did not work

@jaysn1
Copy link

jaysn1 commented May 20, 2018

where is the data saved
Is it saved as .mat file?

@nebi19
Copy link

nebi19 commented Apr 29, 2019

did not work for me

@C-H-I-R-A-G
Copy link

Instead Of Using this whole program use this, It works for me. Try it

from sklearn.datasets import fetch_openml
mnist = fetch_openml('mnist_784')

@Prachi-Zirape
Copy link

@ [C-H-I-R-A-G] : Thanks Your Code Worked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment