In [103]:
%load_ext autoreload
%autoreload 2
from sklearn.datasets import fetch_mldata
from simple_ml.nn import nn
from sklearn.preprocessing import StandardScaler
mnist = fetch_mldata('MNIST original')
data = mnist.data
label = mnist.target
X_train, X_test, y_train, y_test = train_test_split(data, label, test_size=1.0/7, random_state=42)

scalar = StandardScaler()
X_train = scalar.fit_transform(X_train)
X_test = scalar.transform(X_test)


The autoreload extension is already loaded. To reload it, use:
  %reload_ext autoreload
/Library/Python/2.7/site-packages/sklearn/utils/validation.py:420: DataConversionWarning: Data with input dtype uint8 was converted to float64 by StandardScaler.
  warnings.warn(msg, DataConversionWarning)
/Library/Python/2.7/site-packages/sklearn/utils/validation.py:420: DataConversionWarning: Data with input dtype uint8 was converted to float64 by StandardScaler.
  warnings.warn(msg, DataConversionWarning)
/Library/Python/2.7/site-packages/sklearn/utils/validation.py:420: DataConversionWarning: Data with input dtype uint8 was converted to float64 by StandardScaler.
  warnings.warn(msg, DataConversionWarning)

In [ ]:
from sklearn.linear_model import LogisticRegression
lr = LogisticRegression()
lr.fit(X_train, y_train)
y_pred = lr.predict(X_test)
accuracy_score(y_test, y_pred)

In [2]:
from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier(n_estimators=100)
rf.fit(X_train, y_train)
y_pred = rf.predict(X_test)
accuracy_score(y_test, y_pred)


Out[2]:
0.96989999999999998

In [56]:
X_train.shape, X_test.shape, y_train.shape, y_test.shape
accuracy_score(y_test, y_pred)


Out[56]:
0.96989999999999998

In [60]:
from simple_ml.nn import nn
from sklearn.preprocessing import StandardScaler

In [70]:


In [57]:


In [136]:
est = nn.NeurualNetworkClassifier(hidden_layer_size=100, 
                                  learning_rate=1e-1, 
                                  weight_std = 1e-2,
                                  alpha=0.0, 
                                  sgd_epoch=10, 
                                  sgd_batch_size=64,
                                  print_every = 1)
est.X_test = X_test
est.y_test = y_test
est.fit(X_train, y_train)


[epoch 0], cost: 0.005532, accur: 0.953500
accuracy on testing set: 0.9488
[epoch 1], cost: 0.002346, accur: 0.968733
accuracy on testing set: 0.9573
[epoch 2], cost: 0.001663, accur: 0.977300
accuracy on testing set: 0.9645
[epoch 3], cost: 0.001242, accur: 0.982917
accuracy on testing set: 0.9688
[epoch 4], cost: 0.000982, accur: 0.986483
accuracy on testing set: 0.9709
[epoch 5], cost: 0.000770, accur: 0.990183
accuracy on testing set: 0.9712
[epoch 6], cost: 0.000602, accur: 0.993067
accuracy on testing set: 0.9725
[epoch 7], cost: 0.000496, accur: 0.993767
accuracy on testing set: 0.9729
[epoch 8], cost: 0.000399, accur: 0.995083
accuracy on testing set: 0.9732
[epoch 9], cost: 0.000333, accur: 0.996100
accuracy on testing set: 0.9739

In [139]:
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))

y_pred = est.predict(X_test)
accuracy_score(y_test, y_pred)


Out[139]:
0.97389999999999999

In [36]:
t == t1


Out[36]:
array([ True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,  True], dtype=bool)

In [35]:
t1


Out[35]:
[3.0,
 5.0,
 3.0,
 3.0,
 1.0,
 5.0,
 2.0,
 6.0,
 4.0,
 3.0,
 8.0,
 6.0,
 0.0,
 0.0,
 3.0,
 9.0,
 5.0,
 5.0,
 4.0,
 9.0,
 1.0,
 3.0,
 9.0,
 7.0,
 4.0,
 3.0,
 1.0,
 9.0,
 9.0,
 9.0,
 8.0,
 7.0,
 0.0,
 9.0,
 9.0,
 8.0,
 5.0,
 3.0,
 0.0,
 4.0,
 7.0,
 5.0,
 2.0,
 4.0,
 8.0,
 2.0,
 9.0,
 2.0,
 0.0,
 7.0,
 5.0,
 3.0,
 6.0,
 3.0,
 8.0,
 0.0,
 5.0,
 6.0,
 9.0,
 6.0,
 8.0,
 8.0,
 2.0,
 7.0,
 5.0,
 9.0,
 5.0,
 2.0,
 3.0,
 7.0,
 6.0,
 3.0,
 1.0,
 7.0,
 5.0,
 1.0,
 9.0,
 8.0,
 2.0,
 8.0,
 5.0,
 6.0,
 1.0,
 3.0,
 7.0,
 7.0,
 8.0,
 2.0,
 9.0,
 5.0,
 6.0,
 9.0,
 5.0,
 3.0,
 9.0,
 4.0,
 4.0,
 6.0,
 0.0,
 9.0]

In [20]:
y_pred = rf.predict(X_test)

In [21]:
accuracy_score(y_test, y_pred)


Out[21]:
0.97040000000000004

In [36]:
t = X_train[0]
t = t.reshape(28, -1)

In [48]:
idx = 3230
plt.imshow(X_test[idx].reshape(28, -1))
print y_pred[idx], y_test[idx]
plt.show()


5.0 5.0

In [40]:
%matplotlib inline

In [ ]: