{"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-07-13T13:33:43.327941Z","iopub.execute_input":"2023-07-13T13:33:43.329073Z","iopub.status.idle":"2023-07-13T13:33:43.33907Z","shell.execute_reply.started":"2023-07-13T13:33:43.329029Z","shell.execute_reply":"2023-07-13T13:33:43.338064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom keras.models import Sequential\nfrom keras.layers import Dense,Dropout,Flatten\nfrom keras.layers.convolutional import Conv2D,MaxPooling2D\nfrom keras.utils import np_utils\nfrom keras import backend as K\n# K.set_image_dim_ordering(\"th\")","metadata":{"execution":{"iopub.status.busy":"2023-07-13T13:33:43.517774Z","iopub.execute_input":"2023-07-13T13:33:43.518106Z","iopub.status.idle":"2023-07-13T13:33:43.525662Z","shell.execute_reply.started":"2023-07-13T13:33:43.518079Z","shell.execute_reply":"2023-07-13T13:33:43.524592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-07-13T13:33:43.744758Z","iopub.execute_input":"2023-07-13T13:33:43.745129Z","iopub.status.idle":"2023-07-13T13:33:47.136005Z","shell.execute_reply.started":"2023-07-13T13:33:43.745099Z","shell.execute_reply":"2023-07-13T13:33:47.134873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=df.drop([\"label\"],axis=1)\nlabel=df[\"label\"]\n","metadata":{"execution":{"iopub.status.busy":"2023-07-13T13:33:47.138386Z","iopub.execute_input":"2023-07-13T13:33:47.139119Z","iopub.status.idle":"2023-07-13T13:33:47.225134Z","shell.execute_reply.started":"2023-07-13T13:33:47.139083Z","shell.execute_reply":"2023-07-13T13:33:47.223912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nX_train,X_test,y_train,y_test=train_test_split(data,label,test_size=0.2)\nX_train","metadata":{"execution":{"iopub.status.busy":"2023-07-13T13:33:47.227629Z","iopub.execute_input":"2023-07-13T13:33:47.228413Z","iopub.status.idle":"2023-07-13T13:33:47.468903Z","shell.execute_reply.started":"2023-07-13T13:33:47.228373Z","shell.execute_reply":"2023-07-13T13:33:47.467824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train=X_train.values.reshape(X_train.shape[0],1,28,28).astype(\"float32\")\nX_test=X_test.values.reshape(X_test.shape[0],1,28,28).astype(\"float32\")\n# X_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-07-13T13:33:47.471869Z","iopub.execute_input":"2023-07-13T13:33:47.472513Z","iopub.status.idle":"2023-07-13T13:33:47.533348Z","shell.execute_reply.started":"2023-07-13T13:33:47.472465Z","shell.execute_reply":"2023-07-13T13:33:47.532271Z"},"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":"2023-07-13T13:33:47.534969Z","iopub.execute_input":"2023-07-13T13:33:47.535358Z","iopub.status.idle":"2023-07-13T13:33:47.578441Z","shell.execute_reply.started":"2023-07-13T13:33:47.535322Z","shell.execute_reply":"2023-07-13T13:33:47.577321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train=np_utils.to_categorical(y_train)\ny_test=np_utils.to_categorical(y_test)\nnum_classes=y_test.shape[1]","metadata":{"execution":{"iopub.status.busy":"2023-07-13T13:33:47.580131Z","iopub.execute_input":"2023-07-13T13:33:47.580502Z","iopub.status.idle":"2023-07-13T13:33:47.58803Z","shell.execute_reply.started":"2023-07-13T13:33:47.580465Z","shell.execute_reply":"2023-07-13T13:33:47.585862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nmodel=Sequential()\nmodel.add(Conv2D(30,(5,5),activation=\"relu\",input_shape=(1,28,28),padding='same'))\nmodel.add(MaxPooling2D(pool_size=(2,2), padding='same'))\nmodel.add(Convolution2D(15,3,3,activation=\"relu\", padding='same'))\nmodel.add(MaxPooling2D(pool_size=(2,2), padding='same'))\nmodel.add(Dropout(0,2))\nmodel.add(Flatten())\nmodel.add(Dense(128,activation=\"relu\"))\nmodel.add(Dense(50,activation=\"relu\"))\nmodel.add(Dense(num_classes,activation=\"softmax\"))\n    \nmodel.compile(loss=\"categorical_crossentropy\",optimizer=\"adam\",metrics=[\"accuracy\"])\n    ","metadata":{"execution":{"iopub.status.busy":"2023-07-13T13:33:47.590043Z","iopub.execute_input":"2023-07-13T13:33:47.590889Z","iopub.status.idle":"2023-07-13T13:33:47.67566Z","shell.execute_reply.started":"2023-07-13T13:33:47.590851Z","shell.execute_reply":"2023-07-13T13:33:47.674755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=model.fit(X_train,y_train,validation_data=(X_test,y_test),epochs=25,batch_size=200,verbose=2)","metadata":{"execution":{"iopub.status.busy":"2023-07-13T13:33:47.677072Z","iopub.execute_input":"2023-07-13T13:33:47.67758Z","iopub.status.idle":"2023-07-13T13:34:09.219357Z","shell.execute_reply.started":"2023-07-13T13:33:47.677546Z","shell.execute_reply":"2023-07-13T13:34:09.218295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Summarize History For Accuracy","metadata":{}},{"cell_type":"code","source":"plt.plot(history.history[\"accuracy\"])\nplt.plot(history.history[\"val_accuracy\"])\nplt.title(\"Model Accuracy\")\nplt.ylabel(\"accuracy\")\nplt.xlabel(\"epochs\")\nplt.legend([\"train\",\"test\"],loc=\"upper left\")\nplt.show","metadata":{"execution":{"iopub.status.busy":"2023-07-13T13:34:09.222206Z","iopub.execute_input":"2023-07-13T13:34:09.22295Z","iopub.status.idle":"2023-07-13T13:34:09.551782Z","shell.execute_reply.started":"2023-07-13T13:34:09.222892Z","shell.execute_reply":"2023-07-13T13:34:09.550842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Summarize History For Loss","metadata":{}},{"cell_type":"code","source":"plt.plot(history.history[\"loss\"])\nplt.plot(history.history[\"val_loss\"])\nplt.title(\"Model Loss\")\nplt.ylabel(\"Loss\")\nplt.xlabel(\"epochs\")\nplt.legend([\"train\",\"test\"],loc=\"upper left\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-13T13:34:09.555467Z","iopub.execute_input":"2023-07-13T13:34:09.555757Z","iopub.status.idle":"2023-07-13T13:34:09.830056Z","shell.execute_reply.started":"2023-07-13T13:34:09.555731Z","shell.execute_reply":"2023-07-13T13:34:09.828976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data=pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-07-13T13:34:09.831551Z","iopub.execute_input":"2023-07-13T13:34:09.831886Z","iopub.status.idle":"2023-07-13T13:34:11.410524Z","shell.execute_reply.started":"2023-07-13T13:34:09.831852Z","shell.execute_reply":"2023-07-13T13:34:11.409442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data=test_data.values.reshape(-1,1,28,28)\n","metadata":{"execution":{"iopub.status.busy":"2023-07-13T13:34:11.41236Z","iopub.execute_input":"2023-07-13T13:34:11.412736Z","iopub.status.idle":"2023-07-13T13:34:11.418622Z","shell.execute_reply.started":"2023-07-13T13:34:11.4127Z","shell.execute_reply":"2023-07-13T13:34:11.41758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction=model.predict(test_data)","metadata":{"execution":{"iopub.status.busy":"2023-07-13T13:34:11.42015Z","iopub.execute_input":"2023-07-13T13:34:11.421255Z","iopub.status.idle":"2023-07-13T13:34:14.473467Z","shell.execute_reply.started":"2023-07-13T13:34:11.421195Z","shell.execute_reply":"2023-07-13T13:34:14.472364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = pd.DataFrame({'label': np.argmax(prediction,axis=1)})\npred","metadata":{"execution":{"iopub.status.busy":"2023-07-13T13:34:14.476059Z","iopub.execute_input":"2023-07-13T13:34:14.476771Z","iopub.status.idle":"2023-07-13T13:34:14.490897Z","shell.execute_reply.started":"2023-07-13T13:34:14.476733Z","shell.execute_reply":"2023-07-13T13:34:14.489424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred=pd.concat([pd.Series(range(1,28001),name='ImageId'),pred],axis=1)\npred.to_csv('/kaggle/working/submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-07-13T13:34:14.492753Z","iopub.execute_input":"2023-07-13T13:34:14.493152Z","iopub.status.idle":"2023-07-13T13:34:14.55461Z","shell.execute_reply.started":"2023-07-13T13:34:14.493114Z","shell.execute_reply":"2023-07-13T13:34:14.553605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}