{"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":"2023-05-13T07:38:47.567821Z","iopub.execute_input":"2023-05-13T07:38:47.568271Z","iopub.status.idle":"2023-05-13T07:38:47.583065Z","shell.execute_reply.started":"2023-05-13T07:38:47.568235Z","shell.execute_reply":"2023-05-13T07:38:47.582199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split as tts\nfrom sklearn.metrics import accuracy_score\n\n# For Machine Learning \nfrom sklearn.neighbors import KNeighborsClassifier as knn\nfrom sklearn.ensemble import RandomForestClassifier as RFC , GradientBoostingClassifier as GFC\n\n\n# For Deep learing\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense , Conv2D , Flatten , MaxPooling2D , Dropout","metadata":{"execution":{"iopub.status.busy":"2023-05-13T07:38:48.009627Z","iopub.execute_input":"2023-05-13T07:38:48.010456Z","iopub.status.idle":"2023-05-13T07:38:55.369947Z","shell.execute_reply.started":"2023-05-13T07:38:48.010421Z","shell.execute_reply":"2023-05-13T07:38:55.368976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reading the input data\n\ntraining_Data = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest_Data = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-05-13T07:38:55.371681Z","iopub.execute_input":"2023-05-13T07:38:55.372461Z","iopub.status.idle":"2023-05-13T07:39:01.702062Z","shell.execute_reply.started":"2023-05-13T07:38:55.372428Z","shell.execute_reply":"2023-05-13T07:39:01.700974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(training_Data.info() )","metadata":{"execution":{"iopub.status.busy":"2023-05-11T08:29:23.927545Z","iopub.execute_input":"2023-05-11T08:29:23.927870Z","iopub.status.idle":"2023-05-11T08:29:23.996394Z","shell.execute_reply.started":"2023-05-11T08:29:23.927842Z","shell.execute_reply":"2023-05-11T08:29:23.995497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_Data.columns","metadata":{"execution":{"iopub.status.busy":"2023-05-11T08:29:23.997505Z","iopub.execute_input":"2023-05-11T08:29:23.997999Z","iopub.status.idle":"2023-05-11T08:29:24.008008Z","shell.execute_reply.started":"2023-05-11T08:29:23.997969Z","shell.execute_reply":"2023-05-11T08:29:24.006822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# An example of given image & corresponding label\n\nphoto_check_number = 5\nphoto1 , label = training_Data.iloc[photo_check_number , 1 :].values.reshape( 28 , 28 ) , training_Data.iloc[photo_check_number,0]\nplt.imshow( photo1 )\nprint(\"ASSIGNED LABEL : \",label)","metadata":{"execution":{"iopub.status.busy":"2023-05-13T08:05:45.329117Z","iopub.execute_input":"2023-05-13T08:05:45.329575Z","iopub.status.idle":"2023-05-13T08:05:45.654784Z","shell.execute_reply.started":"2023-05-13T08:05:45.329539Z","shell.execute_reply":"2023-05-13T08:05:45.653754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Labels count in training data\ntraining_Data.label.value_counts().sort_index().plot( kind = \"bar\" )","metadata":{"execution":{"iopub.status.busy":"2023-05-11T08:35:02.040530Z","iopub.execute_input":"2023-05-11T08:35:02.040939Z","iopub.status.idle":"2023-05-11T08:35:02.317117Z","shell.execute_reply.started":"2023-05-11T08:35:02.040909Z","shell.execute_reply":"2023-05-11T08:35:02.315637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train Test split ( assigned random state so as to duplicate the results )\ntrain_x , test_x ,train_y , test_y = tts( training_Data.drop(\"label\",axis =1) , \n                                          training_Data.label ,test_size = 0.25 ,random_state = 0)","metadata":{"execution":{"iopub.status.busy":"2023-05-13T07:39:19.947492Z","iopub.execute_input":"2023-05-13T07:39:19.947853Z","iopub.status.idle":"2023-05-13T07:39:20.253110Z","shell.execute_reply.started":"2023-05-13T07:39:19.947821Z","shell.execute_reply":"2023-05-13T07:39:20.252159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# KNN model \nmodel1 = knn().fit( train_x , train_y )  # Default value k = 3\npred1 = model1.predict( test_x)\nprint( \" Accuracy of KNN is : \", accuracy_score( test_y , pred1 ))","metadata":{"execution":{"iopub.status.busy":"2023-05-11T08:38:25.675146Z","iopub.execute_input":"2023-05-11T08:38:25.675975Z","iopub.status.idle":"2023-05-11T08:38:39.791991Z","shell.execute_reply.started":"2023-05-11T08:38:25.675826Z","shell.execute_reply":"2023-05-11T08:38:39.790716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model2 = RFC().fit( train_x , train_y ) \npred2 = model2.predict( test_x)\nprint( \" Accuracy of RFC is : \", accuracy_score( test_y , pred2 ))","metadata":{"execution":{"iopub.status.busy":"2023-05-11T08:37:50.565344Z","iopub.execute_input":"2023-05-11T08:37:50.566031Z","iopub.status.idle":"2023-05-11T08:38:15.582043Z","shell.execute_reply.started":"2023-05-11T08:37:50.565995Z","shell.execute_reply":"2023-05-11T08:38:15.580874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### As knn is better here , continuing with it","metadata":{}},{"cell_type":"code","source":"# Trying different values of K\naccuracy= {}\n\nfor k in np.arange( 1 , 9 , 2 ) :\n    model_ = knn( k ).fit( train_x , train_y ) \n    pred_ = model_.predict( test_x)\n    accuracy[ k ] =  accuracy_score( test_y , pred_ )","metadata":{"execution":{"iopub.status.busy":"2023-05-11T08:53:01.296939Z","iopub.execute_input":"2023-05-11T08:53:01.297322Z","iopub.status.idle":"2023-05-11T08:53:55.512432Z","shell.execute_reply.started":"2023-05-11T08:53:01.297295Z","shell.execute_reply":"2023-05-11T08:53:55.511214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot( accuracy.keys() , accuracy.values() , linestyle='--', marker='o' )","metadata":{"execution":{"iopub.status.busy":"2023-05-11T08:54:03.758557Z","iopub.execute_input":"2023-05-11T08:54:03.759027Z","iopub.status.idle":"2023-05-11T08:54:03.999695Z","shell.execute_reply.started":"2023-05-11T08:54:03.758994Z","shell.execute_reply":"2023-05-11T08:54:03.998626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### lets try some deep networks in order to improve this accuracy ","metadata":{}},{"cell_type":"code","source":"model = Sequential( )\nmodel.add( Dense( units = 64 , activation = \"relu\" ,input_shape = (784, ) ) )\nmodel2.add( Dropout( 0.2 ))\nmodel.add( Dense( units = 32 , activation = \"relu\"  ) )\nmodel2.add( Dropout( 0.2 ))\nmodel.add( Dense( units = 10 , activation = \"softmax\" ))","metadata":{"execution":{"iopub.status.busy":"2023-05-13T08:16:50.543110Z","iopub.execute_input":"2023-05-13T08:16:50.543448Z","iopub.status.idle":"2023-05-13T08:16:50.590095Z","shell.execute_reply.started":"2023-05-13T08:16:50.543421Z","shell.execute_reply":"2023-05-13T08:16:50.589205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-05-13T08:16:50.991600Z","iopub.execute_input":"2023-05-13T08:16:50.991949Z","iopub.status.idle":"2023-05-13T08:16:51.018391Z","shell.execute_reply.started":"2023-05-13T08:16:50.991919Z","shell.execute_reply":"2023-05-13T08:16:51.017641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile( loss = \"categorical_crossentropy\" , optimizer = \"adam\", metrics = [\"accuracy\"] )","metadata":{"execution":{"iopub.status.busy":"2023-05-13T08:16:52.305654Z","iopub.execute_input":"2023-05-13T08:16:52.306017Z","iopub.status.idle":"2023-05-13T08:16:52.321551Z","shell.execute_reply.started":"2023-05-13T08:16:52.305978Z","shell.execute_reply":"2023-05-13T08:16:52.320319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"train_y_dum = pd.get_dummies(train_y)\ntest_y_dum = pd.get_dummies(test_y)\n\nhistory = model.fit( train_x , train_y_dum , epochs = 300 ,\n                    batch_size = 16 , validation_split = 0.2 )","metadata":{"execution":{"iopub.status.busy":"2023-05-13T08:17:06.108146Z","iopub.execute_input":"2023-05-13T08:17:06.108499Z","iopub.status.idle":"2023-05-13T08:23:22.784641Z","shell.execute_reply.started":"2023-05-13T08:17:06.108471Z","shell.execute_reply":"2023-05-13T08:23:22.782934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = history.history[\"loss\"]\nacc = history.history[\"accuracy\"]\nval_acc = history.history[\"val_accuracy\"]","metadata":{"execution":{"iopub.status.busy":"2023-05-13T08:23:22.785753Z","iopub.status.idle":"2023-05-13T08:23:22.786949Z","shell.execute_reply.started":"2023-05-13T08:23:22.786688Z","shell.execute_reply":"2023-05-13T08:23:22.786715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot( loss )\nplt.title(\"LOSS\")","metadata":{"execution":{"iopub.status.busy":"2023-05-11T14:33:03.832143Z","iopub.execute_input":"2023-05-11T14:33:03.832952Z","iopub.status.idle":"2023-05-11T14:33:04.140639Z","shell.execute_reply.started":"2023-05-11T14:33:03.832886Z","shell.execute_reply":"2023-05-11T14:33:04.139501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot( acc )\nplt.title(\"Accuracy\")","metadata":{"execution":{"iopub.status.busy":"2023-05-11T14:33:07.874874Z","iopub.execute_input":"2023-05-11T14:33:07.875328Z","iopub.status.idle":"2023-05-11T14:33:08.097974Z","shell.execute_reply.started":"2023-05-11T14:33:07.875295Z","shell.execute_reply":"2023-05-11T14:33:08.096784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_dnn = np.argmax(model.predict( test_x ,verbose = 0) , axis = 1 )\n","metadata":{"execution":{"iopub.status.busy":"2023-05-11T14:44:40.842690Z","iopub.execute_input":"2023-05-11T14:44:40.843170Z","iopub.status.idle":"2023-05-11T14:44:41.652114Z","shell.execute_reply.started":"2023-05-11T14:44:40.843134Z","shell.execute_reply":"2023-05-11T14:44:41.650922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print( \" Accuracy of Dense Dnn is : \", accuracy_score( test_y , pred_dnn))\n","metadata":{"execution":{"iopub.status.busy":"2023-05-11T14:45:42.177548Z","iopub.execute_input":"2023-05-11T14:45:42.178029Z","iopub.status.idle":"2023-05-11T14:45:42.185646Z","shell.execute_reply.started":"2023-05-11T14:45:42.177987Z","shell.execute_reply":"2023-05-11T14:45:42.184671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model2 = Sequential( )\nmodel2.add( Conv2D( 64 , (3,3) ,input_shape = (28, 28, 1 ) ) )\nmodel2.add( MaxPooling2D() )\nmodel2.add( Flatten() )\nmodel2.add( Dropout( 0.2 ))\nmodel2.add( Dense( units = 64 , activation = \"relu\"  ) )\nmodel2.add( Dropout( 0.2 ))\nmodel2.add( Dense( units = 32 , activation = \"relu\"  ) )\nmodel2.add( Dropout( 0.2 ))\nmodel2.add( Dense( units = 10 , activation = \"softmax\" ))","metadata":{"execution":{"iopub.status.busy":"2023-05-13T07:42:19.002736Z","iopub.execute_input":"2023-05-13T07:42:19.003109Z","iopub.status.idle":"2023-05-13T07:42:19.069858Z","shell.execute_reply.started":"2023-05-13T07:42:19.003077Z","shell.execute_reply":"2023-05-13T07:42:19.068858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model2.compile( loss = \"categorical_crossentropy\" , optimizer = \"adagrad\", metrics = [\"accuracy\"] )","metadata":{"execution":{"iopub.status.busy":"2023-05-13T07:42:19.770937Z","iopub.execute_input":"2023-05-13T07:42:19.771612Z","iopub.status.idle":"2023-05-13T07:42:19.783967Z","shell.execute_reply.started":"2023-05-13T07:42:19.771570Z","shell.execute_reply":"2023-05-13T07:42:19.783042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model2.summary()","metadata":{"execution":{"iopub.status.busy":"2023-05-13T07:42:20.514371Z","iopub.execute_input":"2023-05-13T07:42:20.515234Z","iopub.status.idle":"2023-05-13T07:42:20.540711Z","shell.execute_reply.started":"2023-05-13T07:42:20.515198Z","shell.execute_reply":"2023-05-13T07:42:20.539973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_x.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-13T07:39:29.984209Z","iopub.execute_input":"2023-05-13T07:39:29.984568Z","iopub.status.idle":"2023-05-13T07:39:29.992225Z","shell.execute_reply.started":"2023-05-13T07:39:29.984539Z","shell.execute_reply":"2023-05-13T07:39:29.991092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x_img = train_x.values.reshape( 31500 , 28 , 28 )\ntest_x_img = test_x.values.reshape( 10500 , 28 , 28 )","metadata":{"execution":{"iopub.status.busy":"2023-05-13T07:39:30.879561Z","iopub.execute_input":"2023-05-13T07:39:30.880446Z","iopub.status.idle":"2023-05-13T07:39:30.886184Z","shell.execute_reply.started":"2023-05-13T07:39:30.880411Z","shell.execute_reply":"2023-05-13T07:39:30.884930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_y_dum = pd.get_dummies(train_y)\ntest_y_dum = pd.get_dummies(test_y)","metadata":{"execution":{"iopub.status.busy":"2023-05-13T07:39:31.966387Z","iopub.execute_input":"2023-05-13T07:39:31.966745Z","iopub.status.idle":"2023-05-13T07:39:31.981284Z","shell.execute_reply.started":"2023-05-13T07:39:31.966717Z","shell.execute_reply":"2023-05-13T07:39:31.980298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history2 = model2.fit( train_x_img , train_y_dum , epochs = 10 ,\n                    batch_size = 64 , validation_split = 0.2)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-05-13T07:59:03.354704Z","iopub.execute_input":"2023-05-13T07:59:03.355086Z","iopub.status.idle":"2023-05-13T07:59:24.278650Z","shell.execute_reply.started":"2023-05-13T07:59:03.355054Z","shell.execute_reply":"2023-05-13T07:59:24.277659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = history2.history[\"loss\"]\nacc = history2.history[\"accuracy\"]\nval_acc = history2.history[\"val_accuracy\"]","metadata":{"execution":{"iopub.status.busy":"2023-05-13T07:59:24.902871Z","iopub.execute_input":"2023-05-13T07:59:24.903537Z","iopub.status.idle":"2023-05-13T07:59:24.907959Z","shell.execute_reply.started":"2023-05-13T07:59:24.903504Z","shell.execute_reply":"2023-05-13T07:59:24.907040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot( loss )\nplt.title(\"LOSS\")","metadata":{"execution":{"iopub.status.busy":"2023-05-13T07:59:25.770074Z","iopub.execute_input":"2023-05-13T07:59:25.770421Z","iopub.status.idle":"2023-05-13T07:59:26.138877Z","shell.execute_reply.started":"2023-05-13T07:59:25.770394Z","shell.execute_reply":"2023-05-13T07:59:26.137966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot( acc )\nplt.plot( val_acc, c=\"orange\" )\n\nplt.title(\"Accuracy\")","metadata":{"execution":{"iopub.status.busy":"2023-05-13T07:59:46.955281Z","iopub.execute_input":"2023-05-13T07:59:46.955623Z","iopub.status.idle":"2023-05-13T07:59:47.195763Z","shell.execute_reply.started":"2023-05-13T07:59:46.955595Z","shell.execute_reply":"2023-05-13T07:59:47.194875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dnn_predict2 = np.argmax(model2.predict( test_x_img ,verbose = 0) , axis = 1 )","metadata":{"execution":{"iopub.status.busy":"2023-05-13T08:01:12.532922Z","iopub.execute_input":"2023-05-13T08:01:12.533364Z","iopub.status.idle":"2023-05-13T08:01:14.194934Z","shell.execute_reply.started":"2023-05-13T08:01:12.533330Z","shell.execute_reply":"2023-05-13T08:01:14.193928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dnn_predict2 = np.argmax(model2.predict( test_Data.values.reshape( test_Data.shape[0] , 28 , 28 ) ,verbose = 0) , axis = 1 )","metadata":{"execution":{"iopub.status.busy":"2023-05-13T08:05:00.788364Z","iopub.execute_input":"2023-05-13T08:05:00.788711Z","iopub.status.idle":"2023-05-13T08:05:03.808029Z","shell.execute_reply.started":"2023-05-13T08:05:00.788684Z","shell.execute_reply":"2023-05-13T08:05:03.807050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission2 = pd.DataFrame()\nsubmission2[\"ImageId\"] = np.arange(1,1+test_Data.shape[0])\nsubmission2[\"Label\"] = dnn_predict2","metadata":{"execution":{"iopub.status.busy":"2023-05-13T08:12:40.882698Z","iopub.execute_input":"2023-05-13T08:12:40.883394Z","iopub.status.idle":"2023-05-13T08:12:40.892887Z","shell.execute_reply.started":"2023-05-13T08:12:40.883359Z","shell.execute_reply":"2023-05-13T08:12:40.891955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission2","metadata":{"execution":{"iopub.status.busy":"2023-05-13T08:12:41.990178Z","iopub.execute_input":"2023-05-13T08:12:41.990795Z","iopub.status.idle":"2023-05-13T08:12:42.001163Z","shell.execute_reply.started":"2023-05-13T08:12:41.990763Z","shell.execute_reply":"2023-05-13T08:12:42.000224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Just checking what I am going to submit\n\nphoto_check_number = 0\nphoto1 = test_Data.iloc[photo_check_number].values.reshape( 28 , 28 ) \nplt.imshow( photo1 )\nprint(\"ASSIGNED LABEL : \",label)","metadata":{"execution":{"iopub.status.busy":"2023-05-13T08:08:11.356640Z","iopub.execute_input":"2023-05-13T08:08:11.356993Z","iopub.status.idle":"2023-05-13T08:08:11.578963Z","shell.execute_reply.started":"2023-05-13T08:08:11.356953Z","shell.execute_reply":"2023-05-13T08:08:11.578053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission2.to_csv(\"/kaggle/working/submission2.csv\", index = False)","metadata":{"execution":{"iopub.status.busy":"2023-05-13T08:12:46.153595Z","iopub.execute_input":"2023-05-13T08:12:46.153938Z","iopub.status.idle":"2023-05-13T08:12:46.211204Z","shell.execute_reply.started":"2023-05-13T08:12:46.153910Z","shell.execute_reply":"2023-05-13T08:12:46.210344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}