{"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":"# 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\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-17T10:43:56.368504Z","iopub.execute_input":"2022-05-17T10:43:56.368849Z","iopub.status.idle":"2022-05-17T10:43:56.381562Z","shell.execute_reply.started":"2022-05-17T10:43:56.368816Z","shell.execute_reply":"2022-05-17T10:43:56.380812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd \nimport numpy as np\nfrom sklearn.metrics import classification_report, accuracy_score\nfrom sklearn.model_selection import train_test_split\nfrom sklearn import svm\nfrom sklearn.preprocessing import StandardScaler\n","metadata":{"execution":{"iopub.status.busy":"2022-05-17T10:43:56.563508Z","iopub.execute_input":"2022-05-17T10:43:56.563827Z","iopub.status.idle":"2022-05-17T10:43:56.568923Z","shell.execute_reply.started":"2022-05-17T10:43:56.563793Z","shell.execute_reply":"2022-05-17T10:43:56.567753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv('../input/digit-recognizer/train.csv')\ntest=pd.read_csv('../input/digit-recognizer/test.csv')\nsample=pd.read_csv('../input/digit-recognizer/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-05-17T10:43:56.791385Z","iopub.execute_input":"2022-05-17T10:43:56.791909Z","iopub.status.idle":"2022-05-17T10:44:01.201156Z","shell.execute_reply.started":"2022-05-17T10:43:56.791859Z","shell.execute_reply":"2022-05-17T10:44:01.200435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-17T10:44:01.202549Z","iopub.execute_input":"2022-05-17T10:44:01.202925Z","iopub.status.idle":"2022-05-17T10:44:01.221295Z","shell.execute_reply.started":"2022-05-17T10:44:01.202896Z","shell.execute_reply":"2022-05-17T10:44:01.220205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Do We have Enough Data for Each number ?**","metadata":{}},{"cell_type":"code","source":"train.label.value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2022-05-17T10:44:01.223049Z","iopub.execute_input":"2022-05-17T10:44:01.224318Z","iopub.status.idle":"2022-05-17T10:44:01.427822Z","shell.execute_reply.started":"2022-05-17T10:44:01.22427Z","shell.execute_reply":"2022-05-17T10:44:01.427155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**lets plot random number**","metadata":{}},{"cell_type":"code","source":"im=np.reshape(train.iloc[np.random.randint(0,train.shape[0]),1:].values,(28,28))\nfrom matplotlib import pyplot as plt\nplt.imshow(im)","metadata":{"execution":{"iopub.status.busy":"2022-05-17T10:44:01.429554Z","iopub.execute_input":"2022-05-17T10:44:01.429927Z","iopub.status.idle":"2022-05-17T10:44:01.616791Z","shell.execute_reply.started":"2022-05-17T10:44:01.429889Z","shell.execute_reply":"2022-05-17T10:44:01.615748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**lest look at the statistics ...**","metadata":{}},{"cell_type":"code","source":"print(train.describe().T.sort_values(by='std' , ascending=False))","metadata":{"execution":{"iopub.status.busy":"2022-05-17T10:44:01.618179Z","iopub.execute_input":"2022-05-17T10:44:01.619135Z","iopub.status.idle":"2022-05-17T10:44:04.025285Z","shell.execute_reply.started":"2022-05-17T10:44:01.619082Z","shell.execute_reply":"2022-05-17T10:44:04.024217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Do We have null values ?******","metadata":{}},{"cell_type":"code","source":"train.isna().sum().values.sum()","metadata":{"execution":{"iopub.status.busy":"2022-05-17T10:44:04.026649Z","iopub.execute_input":"2022-05-17T10:44:04.02741Z","iopub.status.idle":"2022-05-17T10:44:04.078513Z","shell.execute_reply.started":"2022-05-17T10:44:04.027369Z","shell.execute_reply":"2022-05-17T10:44:04.077523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Scale the Data**","metadata":{}},{"cell_type":"code","source":"\ntrain.iloc[:,1:]=(train.iloc[:,1:])/255\ntest=test/255\n","metadata":{"execution":{"iopub.status.busy":"2022-05-17T10:44:04.080278Z","iopub.execute_input":"2022-05-17T10:44:04.080577Z","iopub.status.idle":"2022-05-17T10:44:36.351638Z","shell.execute_reply.started":"2022-05-17T10:44:04.080537Z","shell.execute_reply":"2022-05-17T10:44:36.350622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.describe().T.sort_values(by='std' , ascending=False))","metadata":{"execution":{"iopub.status.busy":"2022-05-17T10:44:36.353111Z","iopub.execute_input":"2022-05-17T10:44:36.353442Z","iopub.status.idle":"2022-05-17T10:44:39.250656Z","shell.execute_reply.started":"2022-05-17T10:44:36.353398Z","shell.execute_reply":"2022-05-17T10:44:39.249914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# SVM_model","metadata":{}},{"cell_type":"code","source":"X=train.drop(['label'] , axis=1)\ny=train['label']\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33,shuffle=True, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-05-17T10:44:39.251897Z","iopub.execute_input":"2022-05-17T10:44:39.252284Z","iopub.status.idle":"2022-05-17T10:44:39.589519Z","shell.execute_reply.started":"2022-05-17T10:44:39.252237Z","shell.execute_reply":"2022-05-17T10:44:39.588835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"svm_model=svm.SVC(kernel='poly')\nsvm_model.fit(X_train,y_train)\ny_pred=svm_model.predict(X_test)\nreport=classification_report(y_true=y_test,y_pred=y_pred)\nprint(report)   ","metadata":{"execution":{"iopub.status.busy":"2022-05-17T10:45:44.74558Z","iopub.execute_input":"2022-05-17T10:45:44.746363Z","iopub.status.idle":"2022-05-17T10:48:03.845291Z","shell.execute_reply.started":"2022-05-17T10:45:44.746311Z","shell.execute_reply":"2022-05-17T10:48:03.844268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_final=svm_model.predict(test)\nsample['Label']=y_pred_final\nsample.to_csv('submission_SVM.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-05-17T10:48:16.487261Z","iopub.execute_input":"2022-05-17T10:48:16.487631Z","iopub.status.idle":"2022-05-17T10:49:49.371432Z","shell.execute_reply.started":"2022-05-17T10:48:16.487596Z","shell.execute_reply":"2022-05-17T10:49:49.370485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Keras_model","metadata":{}},{"cell_type":"code","source":"import keras\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten\nfrom keras.layers import Conv2D, MaxPooling2D\nfrom keras.utils.np_utils import to_categorical","metadata":{"execution":{"iopub.status.busy":"2022-05-17T10:49:49.373124Z","iopub.execute_input":"2022-05-17T10:49:49.373386Z","iopub.status.idle":"2022-05-17T10:49:49.378758Z","shell.execute_reply.started":"2022-05-17T10:49:49.373353Z","shell.execute_reply":"2022-05-17T10:49:49.377886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=train.drop(['label'] , axis=1)\ny=train['label']","metadata":{"execution":{"iopub.status.busy":"2022-05-17T10:49:49.380119Z","iopub.execute_input":"2022-05-17T10:49:49.380344Z","iopub.status.idle":"2022-05-17T10:49:49.475686Z","shell.execute_reply.started":"2022-05-17T10:49:49.380316Z","shell.execute_reply":"2022-05-17T10:49:49.474594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=X.values.reshape(-1,28,28,1)\ntest=test.values.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2022-05-17T10:49:49.477905Z","iopub.execute_input":"2022-05-17T10:49:49.478233Z","iopub.status.idle":"2022-05-17T10:49:49.489117Z","shell.execute_reply.started":"2022-05-17T10:49:49.478201Z","shell.execute_reply":"2022-05-17T10:49:49.488087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=to_categorical(y,num_classes=10)","metadata":{"execution":{"iopub.status.busy":"2022-05-17T10:49:49.490826Z","iopub.execute_input":"2022-05-17T10:49:49.491154Z","iopub.status.idle":"2022-05-17T10:49:49.50289Z","shell.execute_reply.started":"2022-05-17T10:49:49.491103Z","shell.execute_reply":"2022-05-17T10:49:49.501964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,shuffle=True, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-05-17T10:49:49.503868Z","iopub.execute_input":"2022-05-17T10:49:49.50477Z","iopub.status.idle":"2022-05-17T10:49:49.921992Z","shell.execute_reply.started":"2022-05-17T10:49:49.504697Z","shell.execute_reply":"2022-05-17T10:49:49.921078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_shape = (28, 28, 1)\nbatch_size = 128\nnum_classes = 10\nepochs = 10\n\nmodel = Sequential()\nmodel.add(Conv2D(32, kernel_size=(3, 3),activation='relu',input_shape=input_shape))\nmodel.add(Conv2D(64, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\nmodel.add(Flatten())\nmodel.add(Dense(256, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(num_classes, activation='softmax'))\n\nmodel.compile(loss=keras.losses.categorical_crossentropy,optimizer='adam',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-05-17T11:03:50.186837Z","iopub.execute_input":"2022-05-17T11:03:50.187179Z","iopub.status.idle":"2022-05-17T11:03:50.263336Z","shell.execute_reply.started":"2022-05-17T11:03:50.187142Z","shell.execute_reply":"2022-05-17T11:03:50.262431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist = model.fit(X_train, y_train,batch_size=batch_size,epochs=epochs,verbose=1,validation_data=(X_test, y_test))\nprint(\"The model has successfully trained\")\n\nmodel.save('mnist.h5')\nprint(\"Saving the model as mnist.h5\")","metadata":{"execution":{"iopub.status.busy":"2022-05-17T11:03:52.76363Z","iopub.execute_input":"2022-05-17T11:03:52.763947Z","iopub.status.idle":"2022-05-17T11:09:35.803204Z","shell.execute_reply.started":"2022-05-17T11:03:52.763913Z","shell.execute_reply":"2022-05-17T11:09:35.802309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = model.evaluate(X_test, y_test, verbose=0)\nprint('Test loss:', score[0])\nprint('Test accuracy:', score[1])","metadata":{"execution":{"iopub.status.busy":"2022-05-17T11:09:35.805135Z","iopub.execute_input":"2022-05-17T11:09:35.806173Z","iopub.status.idle":"2022-05-17T11:09:39.234104Z","shell.execute_reply.started":"2022-05-17T11:09:35.806125Z","shell.execute_reply":"2022-05-17T11:09:39.233326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.predict(test)","metadata":{"execution":{"iopub.status.busy":"2022-05-17T11:09:39.235365Z","iopub.execute_input":"2022-05-17T11:09:39.235766Z","iopub.status.idle":"2022-05-17T11:09:47.918667Z","shell.execute_reply.started":"2022-05-17T11:09:39.235689Z","shell.execute_reply":"2022-05-17T11:09:47.917819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = np.argmax(results,axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-05-17T11:09:47.921998Z","iopub.execute_input":"2022-05-17T11:09:47.92223Z","iopub.status.idle":"2022-05-17T11:09:47.927307Z","shell.execute_reply.started":"2022-05-17T11:09:47.922203Z","shell.execute_reply":"2022-05-17T11:09:47.926588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample['Label']=results\nsample.to_csv('submission_keras.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-05-17T11:09:47.928246Z","iopub.execute_input":"2022-05-17T11:09:47.928754Z","iopub.status.idle":"2022-05-17T11:09:47.988322Z","shell.execute_reply.started":"2022-05-17T11:09:47.928707Z","shell.execute_reply":"2022-05-17T11:09:47.987241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}