{"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)\nfrom tensorflow.keras.utils import to_categorical \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-02-01T12:42:10.397537Z","iopub.execute_input":"2022-02-01T12:42:10.398183Z","iopub.status.idle":"2022-02-01T12:42:10.409134Z","shell.execute_reply.started":"2022-02-01T12:42:10.398130Z","shell.execute_reply":"2022-02-01T12:42:10.408305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('../input/digit-recognizer/train.csv')\ntest_df = pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-02-01T12:42:11.372464Z","iopub.execute_input":"2022-02-01T12:42:11.372926Z","iopub.status.idle":"2022-02-01T12:42:15.835858Z","shell.execute_reply.started":"2022-02-01T12:42:11.372891Z","shell.execute_reply":"2022-02-01T12:42:15.834793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label = train_df['label']","metadata":{"execution":{"iopub.status.busy":"2022-02-01T12:42:31.766354Z","iopub.execute_input":"2022-02-01T12:42:31.766667Z","iopub.status.idle":"2022-02-01T12:42:31.778206Z","shell.execute_reply.started":"2022-02-01T12:42:31.766636Z","shell.execute_reply":"2022-02-01T12:42:31.777544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label = to_categorical(train_label , num_classes=10)","metadata":{"execution":{"iopub.status.busy":"2022-02-01T12:42:50.746117Z","iopub.execute_input":"2022-02-01T12:42:50.746670Z","iopub.status.idle":"2022-02-01T12:42:50.752509Z","shell.execute_reply.started":"2022-02-01T12:42:50.746637Z","shell.execute_reply":"2022-02-01T12:42:50.751560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-01T13:05:29.418845Z","iopub.execute_input":"2022-02-01T13:05:29.419112Z","iopub.status.idle":"2022-02-01T13:05:29.425273Z","shell.execute_reply.started":"2022-02-01T13:05:29.419084Z","shell.execute_reply":"2022-02-01T13:05:29.424651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = (train_df.iloc[:,1:].values.astype('float32') /255.0).reshape(-1,28,28,1)\ntest_data = (test_df.iloc[:,:].values.astype('float32') /255.0).reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2022-02-01T13:07:47.431427Z","iopub.execute_input":"2022-02-01T13:07:47.431716Z","iopub.status.idle":"2022-02-01T13:07:47.577538Z","shell.execute_reply.started":"2022-02-01T13:07:47.431687Z","shell.execute_reply":"2022-02-01T13:07:47.576553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.iloc[0:2,1:]","metadata":{"execution":{"iopub.status.busy":"2022-02-01T13:08:45.176377Z","iopub.execute_input":"2022-02-01T13:08:45.176709Z","iopub.status.idle":"2022-02-01T13:08:45.199325Z","shell.execute_reply.started":"2022-02-01T13:08:45.176673Z","shell.execute_reply":"2022-02-01T13:08:45.198443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt \n\nplt.figure(figsize = (10,10))\n\ndef show_im(data , label):\n    plt.imshow(data)\n    plt.title(label)\n    plt.axis('off')\n    plt.show()\n    \n    \nshow_im((train_df.iloc[1:2,1:].values.astype('float32')).reshape(28,28,1) ,train_df['label'][1])","metadata":{"execution":{"iopub.status.busy":"2022-02-01T13:13:53.348449Z","iopub.execute_input":"2022-02-01T13:13:53.349511Z","iopub.status.idle":"2022-02-01T13:13:53.555855Z","shell.execute_reply.started":"2022-02-01T13:13:53.349452Z","shell.execute_reply":"2022-02-01T13:13:53.554521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train , x_val , y_train ,y_val = train_test_split(train_data,train_label  ,test_size =.3 , random_state =42)","metadata":{"execution":{"iopub.status.busy":"2022-02-01T13:17:41.046709Z","iopub.execute_input":"2022-02-01T13:17:41.047872Z","iopub.status.idle":"2022-02-01T13:17:41.510584Z","shell.execute_reply.started":"2022-02-01T13:17:41.047782Z","shell.execute_reply":"2022-02-01T13:17:41.509535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import Sequential , layers ","metadata":{"execution":{"iopub.status.busy":"2022-02-01T13:18:17.292099Z","iopub.execute_input":"2022-02-01T13:18:17.294103Z","iopub.status.idle":"2022-02-01T13:18:17.298470Z","shell.execute_reply.started":"2022-02-01T13:18:17.294060Z","shell.execute_reply":"2022-02-01T13:18:17.297842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model  = Sequential()\nmodel.add (layers.Conv2D(32,3,input_shape=(28,28,1) ,activation ='relu'))\nmodel.add(layers.MaxPooling2D((2,2)))\nmodel.add (layers.Conv2D(16,3,activation ='relu'))\nmodel.add(layers.MaxPooling2D((2,2)))\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(128 ,activation = 'relu'))\nmodel.add(layers.Dense(10,activation='softmax'))\n","metadata":{"execution":{"iopub.status.busy":"2022-02-01T13:26:06.076100Z","iopub.execute_input":"2022-02-01T13:26:06.076939Z","iopub.status.idle":"2022-02-01T13:26:06.181266Z","shell.execute_reply.started":"2022-02-01T13:26:06.076890Z","shell.execute_reply":"2022-02-01T13:26:06.180324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\noptimizer=\"Adam\" ,\nloss=\"categorical_crossentropy\",\nmetrics =['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-02-01T13:28:41.598293Z","iopub.execute_input":"2022-02-01T13:28:41.598646Z","iopub.status.idle":"2022-02-01T13:28:41.616392Z","shell.execute_reply.started":"2022-02-01T13:28:41.598614Z","shell.execute_reply":"2022-02-01T13:28:41.615436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping\ncallback = EarlyStopping(monitor='loss' ,patience=3)\n\nhistory = model.fit(\nx_train, y_train ,\n    validation_data=(x_val , y_val),\n    epochs =20,\n    callbacks = [callback])","metadata":{"execution":{"iopub.status.busy":"2022-02-01T13:33:17.703949Z","iopub.execute_input":"2022-02-01T13:33:17.704258Z","iopub.status.idle":"2022-02-01T13:36:36.470735Z","shell.execute_reply.started":"2022-02-01T13:33:17.704222Z","shell.execute_reply":"2022-02-01T13:36:36.469466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(x_val ,y_val)","metadata":{"execution":{"iopub.status.busy":"2022-02-01T13:36:49.187713Z","iopub.execute_input":"2022-02-01T13:36:49.188021Z","iopub.status.idle":"2022-02-01T13:36:50.698299Z","shell.execute_reply.started":"2022-02-01T13:36:49.187990Z","shell.execute_reply":"2022-02-01T13:36:50.697152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\ndef plot_graphs(history, metric):\n  plt.plot(history.history[metric])\n  plt.plot(history.history['val_'+metric], '')\n  plt.xlabel(\"Epochs\")\n  plt.ylabel(metric)\n  plt.legend([metric, 'val_'+metric])","metadata":{"execution":{"iopub.status.busy":"2022-02-01T13:37:07.233607Z","iopub.execute_input":"2022-02-01T13:37:07.233923Z","iopub.status.idle":"2022-02-01T13:37:07.240691Z","shell.execute_reply.started":"2022-02-01T13:37:07.233893Z","shell.execute_reply":"2022-02-01T13:37:07.239436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 4))\nplt.subplot(1, 2, 1)\nplot_graphs(history, 'accuracy')\nplt.ylim(None, 1)\nplt.subplot(1, 2, 2)\nplot_graphs(history, 'loss')\nplt.ylim(0, None)","metadata":{"execution":{"iopub.status.busy":"2022-02-01T13:37:17.129264Z","iopub.execute_input":"2022-02-01T13:37:17.129631Z","iopub.status.idle":"2022-02-01T13:37:17.542945Z","shell.execute_reply.started":"2022-02-01T13:37:17.129597Z","shell.execute_reply":"2022-02-01T13:37:17.541785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions  = model.predict(test_data)","metadata":{"execution":{"iopub.status.busy":"2022-02-01T13:41:26.941707Z","iopub.execute_input":"2022-02-01T13:41:26.942040Z","iopub.status.idle":"2022-02-01T13:41:29.432910Z","shell.execute_reply.started":"2022-02-01T13:41:26.942005Z","shell.execute_reply":"2022-02-01T13:41:29.432132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = np.argmax(predictions , axis =1)","metadata":{"execution":{"iopub.status.busy":"2022-02-01T13:42:01.827168Z","iopub.execute_input":"2022-02-01T13:42:01.828162Z","iopub.status.idle":"2022-02-01T13:42:01.834217Z","shell.execute_reply.started":"2022-02-01T13:42:01.828094Z","shell.execute_reply":"2022-02-01T13:42:01.833518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-02-01T13:42:07.817393Z","iopub.execute_input":"2022-02-01T13:42:07.819663Z","iopub.status.idle":"2022-02-01T13:42:07.826624Z","shell.execute_reply.started":"2022-02-01T13:42:07.819593Z","shell.execute_reply":"2022-02-01T13:42:07.825940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission=pd.read_csv('../input/digit-recognizer/sample_submission.csv')\nsubmission['Label']=predictions\nsubmission.to_csv('prediction4.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-02-01T13:42:36.083581Z","iopub.execute_input":"2022-02-01T13:42:36.083915Z","iopub.status.idle":"2022-02-01T13:42:36.148373Z","shell.execute_reply.started":"2022-02-01T13:42:36.083879Z","shell.execute_reply":"2022-02-01T13:42:36.147516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}