{"cells":[{"metadata":{"_uuid":"cd2351d6cfb43c73a170039137da94286d864ae7"},"cell_type":"markdown","source":" **MNIST Classifier USING KERAS CNN **\n> The following kernel is a machine learning model based on convolutional networks architecture  used to classify Handwritten digits from the MNIST dataset.\n\nThe kernel is divided into -- main parts.\n1. Importing Necessary Libraries (keras,numpy,pandas,matplotlib)\n2. Data Preprocessing\n3. Splitting into Train-Test Split\n4. Model Selection\n5. Checking Model Effectiveness."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\nimport keras\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Conv2D, MaxPooling2D, Flatten,BatchNormalization\nfrom keras.optimizers import Adam,Adagrad #optimizers\nfrom keras.datasets import mnist #Import datasets from inbulit Keras Datasets\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"5b5ea43fad896a788f84716c99033c7e7be47313"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"# Load the data\ntrain = pd.read_csv(\"../input/train.csv\")\ntest = pd.read_csv(\"../input/test.csv\")\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3009c3657af4ef938e35c5e77cca097c23c6cdc5"},"cell_type":"code","source":"s=np.array(train)\nlabels=s[:,0]\npixels=s[:,1:785]\nlabels=keras.utils.to_categorical(labels,10)\n#Data Reshaping\npixels=pixels.reshape(42000,28,28,1);\npixels=pixels/255\nfor i in range(10):\n    print(\"label is:\",labels[i])\n    plt.imshow(pixels[i])\n    plt.show()\n\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"917a480033a11c9bb8be15134c490df9d3dbbcbf"},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train,X_test,label_train,label_test=train_test_split(pixels,labels,test_size=0.2,random_state=0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"912928d6f5bb108d7c407328fefa75336b7ea1c8"},"cell_type":"code","source":"model=Sequential()\nmodel.add(Conv2D(64,kernel_size=(3,3),padding='Same',activation='relu',input_shape=(28,28,1,)))\nmodel.add(Conv2D(32,kernel_size=(2,2),padding='Same',activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(3,3),strides=(2,2)))\nmodel.add(Dropout(0.3))\nmodel.add(Conv2D(64,kernel_size=(3,3),padding='Same',activation='relu'))\nmodel.add(Conv2D(32,kernel_size=(2,2),padding='Same',activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.3))\nmodel.add(Conv2D(24,kernel_size=(3,3),padding='Same',activation='relu'))\nmodel.add(Conv2D(12,kernel_size=(2,2),padding='Same',activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(3,3),strides=(2,2)))\nmodel.add(Dropout(0.3))\nmodel.add(Flatten())\nmodel.add(Dense(64,activation='relu'))\nmodel.add(Dense(10,activation='softmax'))\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4261bde622feb13d62e81c4d4e135b7277962ff2"},"cell_type":"code","source":"model.compile(loss=\"categorical_crossentropy\",optimizer=Adam(),metrics=['accuracy'])\nmodel.fit(X_train,label_train,batch_size=64,epochs=40,validation_data=(X_test,label_test))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cefef352c33b7264f705ab943b3b6b10eeaef8c6","collapsed":true},"cell_type":"code","source":"for i in range(20):\n    print(model.predict(pixels[i].reshape(1,28,28,1)).argmax())\n    plt.imshow(pixels[i].reshape(28,28))\n    plt.show()\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8372e078a848f9235938e7051bf96228e3129435"},"cell_type":"code","source":"s1=np.array(test)\npixels1=s1[:,0:784]\n#Data Reshaping\npixels1=pixels1.reshape(28000,28,28,1);\npixels1=pixels1/255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"447be650ffe00ae890149faa2408ca509979bb50"},"cell_type":"code","source":"mad=[0]*28000\nsid=[0]*28000\nfor i in range(28000):\n    #print(model.predict(pixels1[i].reshape(1,28,28,1)).argmax(),\" \",i+1)\n    mad[i]=model.predict(pixels1[i].reshape(1,28,28,1)).argmax()\n    sid[i]=i+1\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"620adbd08cd360d03c9a8a0de30ec001f769d85d"},"cell_type":"code","source":"np.array(mad)\nnp.array(sid)\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"89f39a65caac5e371a869e73ed1c92ff1f1a37aa"},"cell_type":"code","source":"my_sub=pd.DataFrame({\"ImageId\":sid,\"Label\":mad})\nmy_sub.to_csv('submission.csv', index=False)","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}