In [1]:
from time import time
import numpy as np
from sklearn.cross_validation import train_test_split
from sklearn.datasets import fetch_mldata
from sklearn.metrics import classification_report
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from sknn.mlp import Classifier, Layer

In [2]:
np.set_printoptions(precision=4)
np.set_printoptions(suppress=True)

In [3]:
mnist = fetch_mldata('mnist-original')
X_train, X_test, y_train, y_test = train_test_split(
        (mnist.data / 255.0).astype(np.float32),
        mnist.target.astype(np.int32),
        test_size=0.33, random_state=1234)

In [4]:
print "test data :  %s rows  %s columns" %(X_train.shape[0], X_train.shape[1])
print "test data :  %s rows  %s columns" %(X_test.shape[0], X_test.shape[1])


test data :  46900 rows  784 columns
test data :  23100 rows  784 columns

In [5]:
clf = Classifier(
        layers=[Layer("Rectifier", units=300), Layer("Softmax")],
        learning_rate=0.02,
        learning_rule='momentum',
        learning_momentum=0.9,
        batch_size=25,
        valid_size=0.0,
        n_stable=10,
        n_iter=10,
        verbose=1,
)

In [8]:
t0 = time()

clf.fit(X_train, y_train)
print "escape time : ", round(time()-t0, 3), "s"

y_pred = clf.predict(X_test)

print "accuracy score : %s" %(accuracy_score(y_test, y_pred))
print "classification report : "
print classification_report(y_test, y_pred)
print "confusion matrix : "
print confusion_matrix(y_test, y_pred)


escape time :  45.685 s
accuracy score : 0.981298701299
classification report : 
             precision    recall  f1-score   support

          0       0.99      0.99      0.99      2312
          1       0.99      0.99      0.99      2589
          2       0.98      0.98      0.98      2241
          3       0.98      0.97      0.98      2430
          4       0.98      0.99      0.99      2253
          5       0.97      0.98      0.98      2051
          6       0.98      0.99      0.98      2246
          7       0.98      0.98      0.98      2374
          8       0.98      0.97      0.97      2230
          9       0.98      0.98      0.98      2374

avg / total       0.98      0.98      0.98     23100

confusion matrix : 
[[2289    0    0    1    0    2   12    2    6    0]
 [   0 2562    9    5    1    0    3    2    6    1]
 [   4    5 2197   10    6    1    2    6    9    1]
 [   3    1   10 2363    2   19    0   14   10    8]
 [   2    4    0    1 2227    0    6    3    0   10]
 [   4    2    1   12    0 2012   10    0    6    4]
 [   3    5    2    1    3   12 2220    0    0    0]
 [   4    5   18    3    5    1    1 2320    3   14]
 [   6    9    8   10    4   11   10    0 2161   11]
 [   7    5    0    7   15    6    1   12    4 2317]]

In [ ]: