{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"#importing required libraries\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nprint(os.listdir(\"../input\"))\nfrom keras.layers import Dense, Conv2D, Dropout, Flatten, MaxPooling2D\nfrom keras.models import Sequential\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport tensorflow as tf","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"#acquiring required data\ntrain = pd.read_csv(\"../input/train.csv\")\nprint(train.shape)\ntest= pd.read_csv(\"../input/test.csv\")\nprint(test.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8aef478924062205a71f235ff15137438e14a7d0"},"cell_type":"code","source":"#storing data into variables\nx_train = (train.iloc[:,1:].values).astype('float32') # all pixel values\ny_train = train.iloc[:,0].values.astype('int32') # only labels i.e targets digits\nx_test = test.values.astype('float32')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d17a291e8cf62cabed014314444a634d7aa013af"},"cell_type":"code","source":"# Example of a picture\nindex = 6\nplt.imshow(x_train[index].reshape((28,28)))\nprint (\"y = \" + str(y_train[index]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"95718ef3917510173030f531bcb3b2a3f834ed3d"},"cell_type":"code","source":"# Exploring the dataset \nm_train = x_train.shape[0]\nnum_px = x_train.shape[1]\nm_test = x_test.shape[0]\n\nprint (\"Number of training examples: \" + str(m_train))\nprint (\"Number of testing examples: \" + str(m_test))\nprint (\"Each image is of size: (\" + str(num_px) + \", \" + str(num_px)+\")\")\nprint (\"train_x_orig shape: \" + str(x_train.shape))\nprint (\"train_y shape: \" + str(y_train.shape))\nprint (\"test_x_orig shape: \" + str(x_test.shape))\n#print (\"test_y shape: \" + str(y_test.shape))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b27a8cfe8ff5ce40df4324acb334dddb95d3c51e"},"cell_type":"code","source":"# Reshape the training and test examples \nx_train, x_test = x_train / 255.0, x_test / 255.0\ntrain_x = x_train.reshape((m_train,28,28,1)) # The \"-1\" makes reshape flatten the remaining dimensions\ntest_x= x_test.reshape((m_test,28,28,1))\n\ninput_shape=(28,28,1)\ntrain_x=train_x.astype('float32')\ntest_x=test_x.astype('float32')\n\nprint (\"train_x's shape: \" + str(train_x.shape))\nprint (\"test_x's shape: \" + str(test_x.shape))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e0b1bfcb7cc942dfc61cb3aac3b36698c8f2d1d9"},"cell_type":"code","source":"#implementing the model of the layers\nmodel=Sequential()\nmodel.add(Conv2D(6,kernel_size=(1,1),input_shape=input_shape))\nmodel.add(MaxPooling2D(pool_size=(2,2),strides=2))\nmodel.add(Conv2D(16,kernel_size=(5,5),input_shape=(14,14,6)))\nmodel.add(MaxPooling2D(pool_size=(2,2),strides=2))\nmodel.add(Flatten())\nmodel.add(Dense(120,activation=tf.nn.relu))\nmodel.add(Dense(84,activation=tf.nn.relu))\nmodel.add(Dense(10,activation=tf.nn.softmax))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e2350de4e0631e8f6807c51dacf2b4cb3b60c4fb"},"cell_type":"code","source":"#compiling\nmodel.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"79397bf07ced4654c451cf0269b78e06161ce5ce"},"cell_type":"code","source":"#training\nmodel.fit(x=train_x,y=y_train,epochs=30)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"97ed84d8fa3e9c083c76c7514d549ad1cac81e83"},"cell_type":"code","source":"#printed example from the test database\nimage_index=231\nplt.imshow(x_test[image_index].reshape(28,28),cmap='Greys')\npred=model.predict(x_test[image_index].reshape(1, 28,28, 1))\nprint(\"Predicted No is: \",pred.argmax())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4608876edd7ab5a5d9307a129caf884153ad98c8"},"cell_type":"code","source":"X_test = x_test.reshape(x_test.shape[0], 28, 28, 1).astype('float32')\nPred = model.predict(X_test)\nPred.shape\ny_pred = Pred.argmax(axis=1)\nImageID = np.arange(len(y_pred))+1\nOut = pd.DataFrame([ImageID,y_pred]).T\nOut.rename(columns = {0:'ImageId', 1:'Label'})\n#Out\nOut.to_csv('MNIST_Prediction.csv', header =  ['ImageId', 'Label' ], index = None) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"15fb81a8c7aa9c59162649b2581b7a8bd6e9be39"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}