MNIST

/Users/csian/CP/data_set/MNIST
import os
import struct
import numpy as np
def load_mnist(path, kind='train'):
labels_path=os.path.join(path, '%s-labels-idx1-ubyte' %kind)
images_path=os.path.join(path, '%s-images-idx3-ubyte' %kind)
with open(labels_path, 'rb') as lbpath:
magic, n=struct.unpack('>II', lbpath.read(8))
labels=np.fromfile(lbpath, dtype=np.uint8)
with open(images_path, 'rb') as imgpath:
magic, num, rows, cols=struct.unpack(">IIII", imgpath.read(16))
images=np.fromfile(imgpath, dtype=np.uint8).reshape(len(labels), 784)
images=((images/255.)-.5)*2
return images, labels
load data sets
X_train, y_train, X_test, y_test=[mnist[f] for f in mnist.files]
>>> X_train, y_train, X_test, y_test=[mnist[f] for f in mnist.files]
from sklearn fetch_openml get MNIST data set
from sklearn.datasets import fetch_openml
from sklearn.model_selection import train_test_split
X, y=fetch_openml('mnist_784', version=1, return_X_y=True)
y=y.astype(int)
X=((X/255.)-.0)*2
X_train, X_test, y_train, y_test=train_test_split(X, y, test_size=10000, random_state=123, stratify=y)