{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import train_test_split,GridSearchCV,cross_val_score\nfrom sklearn.linear_model import LogisticRegression\nfrom keras.utils import np_utils\nimport matplotlib.pyplot as plt\nfrom sklearn.svm import SVC\nfrom sklearn.metrics import confusion_matrix,accuracy_score,classification_report\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D,MaxPooling2D,Activation,Dropout,Flatten,Dense","metadata":{"execution":{"iopub.status.busy":"2021-12-26T21:35:12.870063Z","iopub.execute_input":"2021-12-26T21:35:12.870466Z","iopub.status.idle":"2021-12-26T21:35:12.880242Z","shell.execute_reply.started":"2021-12-26T21:35:12.870424Z","shell.execute_reply":"2021-12-26T21:35:12.87928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Get Data From Kaggle**","metadata":{}},{"cell_type":"code","source":"train=pd.read_csv('../input/digit-recognizer/train.csv')\ntest=pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2021-12-26T22:56:52.019760Z","iopub.execute_input":"2021-12-26T22:56:52.020022Z","iopub.status.idle":"2021-12-26T22:56:56.504276Z","shell.execute_reply.started":"2021-12-26T22:56:52.019993Z","shell.execute_reply":"2021-12-26T22:56:56.503528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Split Data as train and test**","metadata":{}},{"cell_type":"code","source":"train,val=train_test_split(train)","metadata":{"execution":{"iopub.status.busy":"2021-12-26T22:58:45.427838Z","iopub.execute_input":"2021-12-26T22:58:45.428109Z","iopub.status.idle":"2021-12-26T22:58:45.667649Z","shell.execute_reply.started":"2021-12-26T22:58:45.428077Z","shell.execute_reply":"2021-12-26T22:58:45.666867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train=train['label']\nx_train=train.drop(['label'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-12-26T22:58:53.846789Z","iopub.execute_input":"2021-12-26T22:58:53.847468Z","iopub.status.idle":"2021-12-26T22:58:53.915012Z","shell.execute_reply.started":"2021-12-26T22:58:53.847428Z","shell.execute_reply":"2021-12-26T22:58:53.914170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test=val.drop(['label'],axis=1)\ny_test=val['label']","metadata":{"execution":{"iopub.status.busy":"2021-12-26T22:59:01.387931Z","iopub.execute_input":"2021-12-26T22:59:01.388200Z","iopub.status.idle":"2021-12-26T22:59:01.408078Z","shell.execute_reply.started":"2021-12-26T22:59:01.388150Z","shell.execute_reply":"2021-12-26T22:59:01.407305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train=np.array(x_train)/255 # Normalization\nY_train=np_utils.to_categorical(y_train) #One-hot encoding\n\nX_test=np.array(X_test)/255 # Normalization\nY_test=np_utils.to_categorical(y_test) #One-hot encoding","metadata":{"execution":{"iopub.status.busy":"2021-12-26T22:59:07.873683Z","iopub.execute_input":"2021-12-26T22:59:07.873933Z","iopub.status.idle":"2021-12-26T22:59:08.077080Z","shell.execute_reply.started":"2021-12-26T22:59:07.873905Z","shell.execute_reply":"2021-12-26T22:59:08.076271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Show data as an image**","metadata":{}},{"cell_type":"code","source":"img=X_train[0].reshape(28,28)\n\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2021-12-26T22:59:09.867626Z","iopub.execute_input":"2021-12-26T22:59:09.868489Z","iopub.status.idle":"2021-12-26T22:59:10.144858Z","shell.execute_reply.started":"2021-12-26T22:59:09.868437Z","shell.execute_reply":"2021-12-26T22:59:10.144171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Create Support Vector Classifier and fit the model**","metadata":{}},{"cell_type":"code","source":"svc_model=SVC(kernel=\"linear\").fit(X_train,y_train)\ny_pred=svc_model.predict(X_test)\nprint(accuracy_score(y_test,y_pred))","metadata":{"execution":{"iopub.status.busy":"2021-12-26T22:59:50.807805Z","iopub.execute_input":"2021-12-26T22:59:50.808087Z","iopub.status.idle":"2021-12-26T23:03:21.993730Z","shell.execute_reply.started":"2021-12-26T22:59:50.808052Z","shell.execute_reply":"2021-12-26T23:03:21.991514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**The accuracy is 93% with SVM,\n  Now try to train CNN**","metadata":{}},{"cell_type":"code","source":"X_train=X_train.reshape(X_train.shape[0],28,28,1) # Change input shape for the CNN\nX_test=X_test.reshape(X_test.shape[0],28,28,1) # Change input shape for the CNN","metadata":{"execution":{"iopub.status.busy":"2021-12-26T23:03:58.267625Z","iopub.execute_input":"2021-12-26T23:03:58.267882Z","iopub.status.idle":"2021-12-26T23:03:58.275849Z","shell.execute_reply.started":"2021-12-26T23:03:58.267854Z","shell.execute_reply":"2021-12-26T23:03:58.275154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Crate CNN model with keras**","metadata":{}},{"cell_type":"code","source":"model=Sequential()\nmodel.add(Conv2D(32,(3,3),input_shape=X_train[0].shape))\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D())\n\nmodel.add(Flatten())\nmodel.add(Dense(1024))\nmodel.add(Activation(\"relu\"))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10))#output layer\nmodel.add(Activation(\"softmax\"))\n\nmodel.compile(loss=\"categorical_crossentropy\",\n              optimizer=\"adam\",\n              metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2021-12-26T23:04:02.330509Z","iopub.execute_input":"2021-12-26T23:04:02.330774Z","iopub.status.idle":"2021-12-26T23:04:02.381683Z","shell.execute_reply.started":"2021-12-26T23:04:02.330745Z","shell.execute_reply":"2021-12-26T23:04:02.380977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Fitting**","metadata":{}},{"cell_type":"code","source":"hist=model.fit(X_train,Y_train, epochs=10, batch_size=64, validation_data=(X_test,Y_test))","metadata":{"execution":{"iopub.status.busy":"2021-12-26T23:04:35.325516Z","iopub.execute_input":"2021-12-26T23:04:35.326243Z","iopub.status.idle":"2021-12-26T23:04:57.347972Z","shell.execute_reply.started":"2021-12-26T23:04:35.326194Z","shell.execute_reply":"2021-12-26T23:04:57.347286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**The accuracy with CNN is 98.94%**","metadata":{}},{"cell_type":"markdown","source":"**Show Loss and Acc graph**","metadata":{}},{"cell_type":"code","source":"loss=hist.history['loss']\nacc=hist.history['accuracy']\nval_loss=hist.history['val_loss']\nval_acc=hist.history['val_accuracy']\n\nplt.figure()\nplt.plot(loss,label=\"Train loss\")\nplt.plot(val_loss,label=\"validatıon loss\")\nplt.legend()\nplt.figure()\nplt.plot(acc,label=\"Train acc\")\nplt.plot(val_acc,label=\"validatıon acc\")\nplt.legend()\nplt.show()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-26T23:06:26.076068Z","iopub.execute_input":"2021-12-26T23:06:26.076806Z","iopub.status.idle":"2021-12-26T23:06:26.432466Z","shell.execute_reply.started":"2021-12-26T23:06:26.076755Z","shell.execute_reply":"2021-12-26T23:06:26.431797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Testing**","metadata":{}},{"cell_type":"code","source":"test=np.array(test)\ntest=test.reshape(test.shape[0],28,28,1)/255","metadata":{"execution":{"iopub.status.busy":"2021-12-26T23:08:20.846827Z","iopub.execute_input":"2021-12-26T23:08:20.847109Z","iopub.status.idle":"2021-12-26T23:08:20.981440Z","shell.execute_reply.started":"2021-12-26T23:08:20.847080Z","shell.execute_reply":"2021-12-26T23:08:20.980649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds=model.predict(test)\ny_preds=np.argmax(preds,axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-12-26T23:08:31.508136Z","iopub.execute_input":"2021-12-26T23:08:31.508413Z","iopub.status.idle":"2021-12-26T23:08:33.042085Z","shell.execute_reply.started":"2021-12-26T23:08:31.508383Z","shell.execute_reply":"2021-12-26T23:08:33.041346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_preds[:10]","metadata":{"execution":{"iopub.status.busy":"2021-12-26T23:08:41.379481Z","iopub.execute_input":"2021-12-26T23:08:41.379740Z","iopub.status.idle":"2021-12-26T23:08:41.385649Z","shell.execute_reply.started":"2021-12-26T23:08:41.379711Z","shell.execute_reply":"2021-12-26T23:08:41.384970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Generate Submission File \nsubmission = pd.DataFrame({'ImageId':pd.Series(list(range(1, len(y_preds)+1))),\n                                   'Label':pd.Series(y_preds)})\n# save the df\nsubmission.to_csv(\"Submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2021-12-26T22:27:09.229639Z","iopub.execute_input":"2021-12-26T22:27:09.229907Z","iopub.status.idle":"2021-12-26T22:27:15.873466Z","shell.execute_reply.started":"2021-12-26T22:27:09.229878Z","shell.execute_reply":"2021-12-26T22:27:15.872571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}