In [105]:
import numpy as np
import matplotlib.pyplot as plt
import scipy.special
%matplotlib inline

In [14]:
with open('./mnist_train_100.csv','r') as data_file:
    data_list = data_file.readlines()
    print(len(data_list))


100

In [15]:
print(len(data_list))


100

In [64]:
all_values = data_list[0].split(',')
image_array = np.asfarray(all_values[1:]).reshape((28,28))
plt.imshow(image_array, cmap='Greens', interpolation='None')


Out[64]:
<matplotlib.image.AxesImage at 0x10a662630>

In [47]:
a = [[[1,2,3,4],[2,30,4,5]],[[6,7,8,7],[4,5,6,3]]]
plt.imshow(a, interpolation='nearest')


Out[47]:
<matplotlib.image.AxesImage at 0x109651c18>

In [48]:
a = np.arange(10).reshape(2,5)

In [55]:
plt.imshow(a)


Out[55]:
<matplotlib.image.AxesImage at 0x109de2240>

In [56]:
plt.imshow(a, interpolation='None')


Out[56]:
<matplotlib.image.AxesImage at 0x10a033b70>

In [57]:
plt.imshow(a, interpolation='nearest')


Out[57]:
<matplotlib.image.AxesImage at 0x10a0c9940>

In [44]:
np.asfarray(all_values[1:])


Out[44]:
array([   0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    3.,
         18.,   18.,   18.,  126.,  136.,  175.,   26.,  166.,  255.,
        247.,  127.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,   30.,   36.,   94.,  154.,
        170.,  253.,  253.,  253.,  253.,  253.,  225.,  172.,  253.,
        242.,  195.,   64.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,   49.,  238.,  253.,  253.,
        253.,  253.,  253.,  253.,  253.,  253.,  251.,   93.,   82.,
         82.,   56.,   39.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,   18.,  219.,  253.,
        253.,  253.,  253.,  253.,  198.,  182.,  247.,  241.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,   80.,
        156.,  107.,  253.,  253.,  205.,   11.,    0.,   43.,  154.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,   14.,    1.,  154.,  253.,   90.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,  139.,  253.,  190.,    2.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,   11.,  190.,  253.,   70.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,   35.,  241.,
        225.,  160.,  108.,    1.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
         81.,  240.,  253.,  253.,  119.,   25.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,   45.,  186.,  253.,  253.,  150.,   27.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,   16.,   93.,  252.,  253.,  187.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,  249.,  253.,
        249.,   64.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,   46.,  130.,  183.,  253.,
        253.,  207.,    2.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,   39.,  148.,  229.,  253.,  253.,
        253.,  250.,  182.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,   24.,  114.,  221.,  253.,  253.,  253.,
        253.,  201.,   78.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,   23.,   66.,  213.,  253.,  253.,  253.,  253.,
        198.,   81.,    2.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,   18.,  171.,  219.,  253.,  253.,  253.,  253.,  195.,
         80.,    9.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
         55.,  172.,  226.,  253.,  253.,  253.,  253.,  244.,  133.,
         11.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,  136.,  253.,  253.,  253.,  212.,  135.,  132.,   16.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
          0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.])

In [37]:
np.asfarray(all_values[1:]).shape


Out[37]:
(784,)

In [58]:
len(all_values)


Out[58]:
785

In [92]:
all_values = data_list[0].split(',')
image_array = np.asfarray(all_values[1:]).reshape((28,28))
plt.imshow(image_array, cmap='Greens', interpolation='None')


Out[92]:
<matplotlib.image.AxesImage at 0x10aa245f8>

In [93]:
scaled_input = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
print(scaled_input)


[ 0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.02164706
  0.07988235  0.07988235  0.07988235  0.49917647  0.538       0.68941176
  0.11094118  0.65447059  1.          0.96894118  0.50305882  0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.12647059  0.14976471  0.37494118
  0.60788235  0.67        0.99223529  0.99223529  0.99223529  0.99223529
  0.99223529  0.88352941  0.67776471  0.99223529  0.94952941  0.76705882
  0.25847059  0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.20023529
  0.934       0.99223529  0.99223529  0.99223529  0.99223529  0.99223529
  0.99223529  0.99223529  0.99223529  0.98447059  0.37105882  0.32835294
  0.32835294  0.22741176  0.16141176  0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.07988235  0.86023529  0.99223529  0.99223529  0.99223529
  0.99223529  0.99223529  0.77870588  0.71658824  0.96894118  0.94564706
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.32058824  0.61564706
  0.42541176  0.99223529  0.99223529  0.80588235  0.05270588  0.01
  0.17694118  0.60788235  0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.06435294  0.01388235  0.60788235  0.99223529  0.35941176  0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.54964706  0.99223529  0.74764706  0.01776471
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.05270588  0.74764706  0.99223529
  0.28176471  0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.14588235
  0.94564706  0.88352941  0.63117647  0.42929412  0.01388235  0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.32447059  0.94176471  0.99223529  0.99223529  0.472       0.10705882
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.18470588  0.73211765  0.99223529  0.99223529
  0.59235294  0.11482353  0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.07211765  0.37105882
  0.98835294  0.99223529  0.736       0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.97670588  0.99223529  0.97670588  0.25847059  0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.18858824  0.51470588
  0.72047059  0.99223529  0.99223529  0.81364706  0.01776471  0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.16141176  0.58458824  0.89905882
  0.99223529  0.99223529  0.99223529  0.98058824  0.71658824  0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.10317647  0.45258824  0.868       0.99223529
  0.99223529  0.99223529  0.99223529  0.79035294  0.31282353  0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.09929412  0.26623529  0.83694118  0.99223529  0.99223529
  0.99223529  0.99223529  0.77870588  0.32447059  0.01776471  0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.07988235  0.67388235  0.86023529  0.99223529  0.99223529  0.99223529
  0.99223529  0.76705882  0.32058824  0.04494118  0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01
  0.22352941  0.67776471  0.88741176  0.99223529  0.99223529  0.99223529
  0.99223529  0.95729412  0.52635294  0.05270588  0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.538       0.99223529  0.99223529  0.99223529  0.83305882
  0.53411765  0.52247059  0.07211765  0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01        0.01        0.01
  0.01        0.01        0.01        0.01        0.01      ]

In [94]:
plt.imshow(scaled_input.reshape(28,28),cmap='Greens', interpolation='None')


Out[94]:
<matplotlib.image.AxesImage at 0x10a6fce48>

In [95]:
a = np.arange(100).reshape(4,25)

In [96]:
plt.imshow(a, cmap='Purples', interpolation='None')


Out[96]:
<matplotlib.image.AxesImage at 0x10b23e978>

In [97]:
onodes = 10
targets = np.zeros(onodes) + 0.01
targets[int(all_values[0])] = 0.99

In [98]:
targets


Out[98]:
array([ 0.01,  0.01,  0.01,  0.01,  0.01,  0.99,  0.01,  0.01,  0.01,  0.01])

In [99]:
all_values[0]


Out[99]:
'5'

In [100]:
targets.shape


Out[100]:
(10,)

In [102]:
targets.reshape(1,10).shape


Out[102]:
(1, 10)

In [103]:
targets


Out[103]:
array([ 0.01,  0.01,  0.01,  0.01,  0.01,  0.99,  0.01,  0.01,  0.01,  0.01])

In [104]:
type(targets)


Out[104]:
numpy.ndarray

In [139]:
np.random.normal(0.0, pow(2,-0.5), (10,1)) #0.0을 평균으로 표준편차 pow(2,-0.5)만큼 퍼지게 숫자를 랜덤으로 10행 1열 만큼 뽑아라!


Out[139]:
array([[-0.01139062],
       [ 0.41594377],
       [-0.49843662],
       [-0.20747827],
       [ 0.12813047],
       [ 1.25018531],
       [ 0.51550486],
       [-0.10830205],
       [ 0.82907969],
       [ 0.69536477]])

In [157]:
nor = np.random.normal(0,1,(1,20))
plt.imshow(nor, cmap='Purples',interpolation='None')
plt.show()
plt.close()
plt.hist(nor.reshape(20,),histtype='bar')
plt.show()
plt.close()



In [134]:
np.array([[1,2,3,4],[3,4,5,6]], ndmin=2)


Out[134]:
array([[1, 2, 3, 4],
       [3, 4, 5, 6]])

In [112]:
class NeuralNetwork:
    def __init__(self, input_nodes, hidden_nodes, output_nodes, learning_rate):
        self.inodes = input_nodes
        self.hnodes = hidden_nodes
        self.onodes = output_nodes
        
        self.wih = np.random.normal(0.0, pow(self.hnodes, -0.5), (self.hnodes, self.inodes))
        self.who = np.random.normal(0.0, pow(self.onodes, -0.5), (self.onodes, self.hnodes))
        
        self.lr = learning_rate
        
        self.activation_function = lambda x: scipy.special.expit(x)
        
    def train(self, input_list, target_list):
        inputs = np.array(input_list, ndmin=2).T

In [ ]: