-
-
Save e9t/5d4c2b48d8eca8c662ef to your computer and use it in GitHub Desktop.
MNIST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python | |
# -*- coding: utf-8 -*- | |
import numpy as np | |
from sklearn import datasets | |
d = datasets.fetch_mldata('MNIST original', data_home='.') | |
X, y = d['data'], d['target'] | |
X_y0 = X[y==0] | |
X_y1 = X[y==1] | |
X_bin = np.concatenate((X_y0, X_y1)) | |
np.savetxt('data-mnist-x-bin.csv', X_bin, delimiter=',', fmt='%d') | |
print(X_bin.shape) | |
y_bin = np.concatenate(([0]*len(X_y0), [1]*len(X_y1))) | |
np.savetxt('data-mnist-y-bin.csv', y_bin, delimiter=',', fmt='%d') | |
print(y_bin.shape) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python | |
# -*- coding: utf-8 -*- | |
from matplotlib import pyplot as plt | |
from sklearn import cross_validation, datasets | |
from sklearn.linear_model import LogisticRegression | |
from time import time | |
print('1. data acquisition') | |
''' | |
d = datasets.fetch_mldata('MNIST original', data_home='.') | |
X = d['data'] | |
y = d['target'] | |
''' | |
from numpy import genfromtxt | |
X = genfromtxt('data-mnist-x-bin.csv', delimiter=',') | |
y = genfromtxt('data-mnist-y-bin.csv', delimiter=',') | |
print('2. data exploration') | |
print("X[0]:", X[0]) | |
print("y[0]:", y[0]) | |
print("min/max(X[0]):", min(X[0]), max(X[0])) | |
print("All classes of y:", set(y)) | |
X0 = X[0].reshape(28, 28) # reshape 1*784 array to 28*28 array | |
plt.rc('image', cmap='binary') # set runtime configurations (rc) for color maps | |
plt.matshow(X0) # plot matrix | |
plt.savefig('X0.png') # save plot to image file | |
print('3. data partitioning') | |
X_train, X_test, y_train, y_test =\ | |
cross_validation.train_test_split(X, y, test_size=0.4, random_state=1234) | |
print('4. training') | |
lr = LogisticRegression(random_state=1234) # LR instance 생성 | |
lr.fit(X_train, y_train) # LR 학습 | |
print('5. testing') | |
print("Training accuracy:", lr.score(X_train, y_train)) | |
print("Testing accuracy:", lr.score(X_test, y_test)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment