{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30664,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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","_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-03-19T15:20:40.005483Z","iopub.execute_input":"2024-03-19T15:20:40.006732Z","iopub.status.idle":"2024-03-19T15:20:40.857434Z","shell.execute_reply.started":"2024-03-19T15:20:40.006662Z","shell.execute_reply":"2024-03-19T15:20:40.856579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2024-03-19T15:20:40.859270Z","iopub.execute_input":"2024-03-19T15:20:40.859916Z","iopub.status.idle":"2024-03-19T15:20:40.863635Z","shell.execute_reply.started":"2024-03-19T15:20:40.859886Z","shell.execute_reply":"2024-03-19T15:20:40.862888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-03-19T15:20:40.864692Z","iopub.execute_input":"2024-03-19T15:20:40.865128Z","iopub.status.idle":"2024-03-19T15:20:45.237706Z","shell.execute_reply.started":"2024-03-19T15:20:40.865102Z","shell.execute_reply":"2024-03-19T15:20:45.236526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow\nfrom tensorflow import keras\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, Dropout","metadata":{"execution":{"iopub.status.busy":"2024-03-19T15:20:45.241304Z","iopub.execute_input":"2024-03-19T15:20:45.241786Z","iopub.status.idle":"2024-03-19T15:20:56.250896Z","shell.execute_reply.started":"2024-03-19T15:20:45.241744Z","shell.execute_reply":"2024-03-19T15:20:56.249509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **✨ Preprocessing ✨**","metadata":{}},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-19T15:20:56.252176Z","iopub.execute_input":"2024-03-19T15:20:56.252750Z","iopub.status.idle":"2024-03-19T15:20:56.274945Z","shell.execute_reply.started":"2024-03-19T15:20:56.252726Z","shell.execute_reply":"2024-03-19T15:20:56.273989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-19T15:20:56.278216Z","iopub.execute_input":"2024-03-19T15:20:56.279179Z","iopub.status.idle":"2024-03-19T15:20:56.296115Z","shell.execute_reply.started":"2024-03-19T15:20:56.279132Z","shell.execute_reply":"2024-03-19T15:20:56.295244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train['label']\ntrain.drop('label', inplace = True, axis = \"columns\")","metadata":{"execution":{"iopub.status.busy":"2024-03-19T15:20:56.297377Z","iopub.execute_input":"2024-03-19T15:20:56.297654Z","iopub.status.idle":"2024-03-19T15:20:56.363379Z","shell.execute_reply.started":"2024-03-19T15:20:56.297628Z","shell.execute_reply":"2024-03-19T15:20:56.362037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train.values.reshape((len(train), 28, 28))","metadata":{"execution":{"iopub.status.busy":"2024-03-19T15:20:56.364553Z","iopub.execute_input":"2024-03-19T15:20:56.364879Z","iopub.status.idle":"2024-03-19T15:20:56.369078Z","shell.execute_reply.started":"2024-03-19T15:20:56.364850Z","shell.execute_reply":"2024-03-19T15:20:56.368478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=0)","metadata":{"execution":{"iopub.status.busy":"2024-03-19T15:20:56.370079Z","iopub.execute_input":"2024-03-19T15:20:56.370313Z","iopub.status.idle":"2024-03-19T15:20:57.428835Z","shell.execute_reply.started":"2024-03-19T15:20:56.370293Z","shell.execute_reply":"2024-03-19T15:20:57.427900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.shape","metadata":{"execution":{"iopub.status.busy":"2024-03-19T15:20:57.432660Z","iopub.execute_input":"2024-03-19T15:20:57.432923Z","iopub.status.idle":"2024-03-19T15:20:57.439639Z","shell.execute_reply.started":"2024-03-19T15:20:57.432900Z","shell.execute_reply":"2024-03-19T15:20:57.438531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train/255\nX_test = X_test/255","metadata":{"execution":{"iopub.status.busy":"2024-03-19T15:20:57.441313Z","iopub.execute_input":"2024-03-19T15:20:57.441951Z","iopub.status.idle":"2024-03-19T15:20:57.528145Z","shell.execute_reply.started":"2024-03-19T15:20:57.441921Z","shell.execute_reply":"2024-03-19T15:20:57.526742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#X_train[0]","metadata":{"execution":{"iopub.status.busy":"2024-03-19T15:20:57.529766Z","iopub.execute_input":"2024-03-19T15:20:57.530123Z","iopub.status.idle":"2024-03-19T15:20:57.533772Z","shell.execute_reply.started":"2024-03-19T15:20:57.530094Z","shell.execute_reply":"2024-03-19T15:20:57.533072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **📈 Prediction using simple ANN 🚀**","metadata":{}},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Flatten(input_shape=(28,28)))\nmodel.add(Dropout(0.3))\nmodel.add(Dense(128,activation='relu'))\nmodel.add(Dropout(0.3))\nmodel.add(Dense(32,activation='relu'))\nmodel.add(Dropout(0.3))\nmodel.add(Dense(10,activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2024-03-19T15:20:57.534544Z","iopub.execute_input":"2024-03-19T15:20:57.534788Z","iopub.status.idle":"2024-03-19T15:20:57.629041Z","shell.execute_reply.started":"2024-03-19T15:20:57.534769Z","shell.execute_reply":"2024-03-19T15:20:57.628215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-03-19T15:20:57.630203Z","iopub.execute_input":"2024-03-19T15:20:57.630484Z","iopub.status.idle":"2024-03-19T15:20:57.649564Z","shell.execute_reply.started":"2024-03-19T15:20:57.630455Z","shell.execute_reply":"2024-03-19T15:20:57.649026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='sparse_categorical_crossentropy',optimizer='Adam',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-03-19T15:20:57.650320Z","iopub.execute_input":"2024-03-19T15:20:57.650830Z","iopub.status.idle":"2024-03-19T15:20:57.662622Z","shell.execute_reply.started":"2024-03-19T15:20:57.650809Z","shell.execute_reply":"2024-03-19T15:20:57.661735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train,y_train,epochs=108,validation_split=0.2, verbose=0)","metadata":{"execution":{"iopub.status.busy":"2024-03-19T15:20:57.663834Z","iopub.execute_input":"2024-03-19T15:20:57.664050Z","iopub.status.idle":"2024-03-19T15:26:03.498696Z","shell.execute_reply.started":"2024-03-19T15:20:57.664031Z","shell.execute_reply":"2024-03-19T15:26:03.497440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_prob = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-03-19T15:26:03.499799Z","iopub.execute_input":"2024-03-19T15:26:03.500094Z","iopub.status.idle":"2024-03-19T15:26:04.016939Z","shell.execute_reply.started":"2024-03-19T15:26:03.500066Z","shell.execute_reply":"2024-03-19T15:26:04.015974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = y_prob.argmax(axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-03-19T15:26:04.018393Z","iopub.execute_input":"2024-03-19T15:26:04.018702Z","iopub.status.idle":"2024-03-19T15:26:04.024027Z","shell.execute_reply.started":"2024-03-19T15:26:04.018678Z","shell.execute_reply":"2024-03-19T15:26:04.023146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy_score(y_test,y_pred)","metadata":{"execution":{"iopub.status.busy":"2024-03-19T15:36:43.023231Z","iopub.execute_input":"2024-03-19T15:36:43.023658Z","iopub.status.idle":"2024-03-19T15:36:43.035122Z","shell.execute_reply.started":"2024-03-19T15:36:43.023623Z","shell.execute_reply":"2024-03-19T15:36:43.033973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])","metadata":{"execution":{"iopub.status.busy":"2024-03-19T15:36:46.639821Z","iopub.execute_input":"2024-03-19T15:36:46.640220Z","iopub.status.idle":"2024-03-19T15:36:46.828615Z","shell.execute_reply.started":"2024-03-19T15:36:46.640189Z","shell.execute_reply":"2024-03-19T15:36:46.827652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-03-19T15:26:04.248051Z","iopub.execute_input":"2024-03-19T15:26:04.248414Z","iopub.status.idle":"2024-03-19T15:26:04.425286Z","shell.execute_reply.started":"2024-03-19T15:26:04.248387Z","shell.execute_reply":"2024-03-19T15:26:04.424271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X_test[18])","metadata":{"execution":{"iopub.status.busy":"2024-03-19T15:26:04.426614Z","iopub.execute_input":"2024-03-19T15:26:04.426873Z","iopub.status.idle":"2024-03-19T15:26:04.597547Z","shell.execute_reply.started":"2024-03-19T15:26:04.426848Z","shell.execute_reply":"2024-03-19T15:26:04.596405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X_test[18])","metadata":{"execution":{"iopub.status.busy":"2024-03-19T15:26:04.599313Z","iopub.execute_input":"2024-03-19T15:26:04.599688Z","iopub.status.idle":"2024-03-19T15:26:04.774008Z","shell.execute_reply.started":"2024-03-19T15:26:04.599659Z","shell.execute_reply":"2024-03-19T15:26:04.772960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.predict(X_test[18].reshape(1,28,28)).argmax(axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-03-19T15:26:04.775141Z","iopub.execute_input":"2024-03-19T15:26:04.775889Z","iopub.status.idle":"2024-03-19T15:26:04.839347Z","shell.execute_reply.started":"2024-03-19T15:26:04.775865Z","shell.execute_reply":"2024-03-19T15:26:04.838624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test.values.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2024-03-19T15:26:04.841774Z","iopub.execute_input":"2024-03-19T15:26:04.842044Z","iopub.status.idle":"2024-03-19T15:26:04.846058Z","shell.execute_reply.started":"2024-03-19T15:26:04.842021Z","shell.execute_reply":"2024-03-19T15:26:04.845443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.predict(test)\n\n# select the indix with the maximum probability\nresults = np.argmax(results,axis = 1)\n\nresults = pd.Series(results,name=\"Label\")\n\nsubmission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\n\nsubmission.to_csv(\"digit_recognation.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2024-03-19T15:26:04.846846Z","iopub.execute_input":"2024-03-19T15:26:04.847521Z","iopub.status.idle":"2024-03-19T15:26:06.228970Z","shell.execute_reply.started":"2024-03-19T15:26:04.847491Z","shell.execute_reply":"2024-03-19T15:26:06.227998Z"},"trusted":true},"execution_count":null,"outputs":[]}]}