{"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\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 t/he 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-08-12T08:18:27.824632Z","iopub.execute_input":"2022-08-12T08:18:27.825037Z","iopub.status.idle":"2022-08-12T08:18:27.839069Z","shell.execute_reply.started":"2022-08-12T08:18:27.825004Z","shell.execute_reply":"2022-08-12T08:18:27.838100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom keras.utils.np_utils import to_categorical\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2022-08-12T08:18:27.881144Z","iopub.execute_input":"2022-08-12T08:18:27.882156Z","iopub.status.idle":"2022-08-12T08:18:27.887439Z","shell.execute_reply.started":"2022-08-12T08:18:27.882116Z","shell.execute_reply":"2022-08-12T08:18:27.886311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ny_data = train_data.pop('label')\n","metadata":{"execution":{"iopub.status.busy":"2022-08-12T08:18:27.918648Z","iopub.execute_input":"2022-08-12T08:18:27.919218Z","iopub.status.idle":"2022-08-12T08:18:31.423536Z","shell.execute_reply.started":"2022-08-12T08:18:27.919188Z","shell.execute_reply":"2022-08-12T08:18:31.422478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df,val_df,y_train,y_val = train_test_split(train_data,y_data,test_size = 0.25,shuffle =  False) ","metadata":{"execution":{"iopub.status.busy":"2022-08-12T08:18:31.425932Z","iopub.execute_input":"2022-08-12T08:18:31.426660Z","iopub.status.idle":"2022-08-12T08:18:31.777851Z","shell.execute_reply.started":"2022-08-12T08:18:31.426622Z","shell.execute_reply":"2022-08-12T08:18:31.776662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-12T08:18:31.780294Z","iopub.execute_input":"2022-08-12T08:18:31.781246Z","iopub.status.idle":"2022-08-12T08:18:31.796173Z","shell.execute_reply.started":"2022-08-12T08:18:31.781202Z","shell.execute_reply":"2022-08-12T08:18:31.793130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T08:18:31.800879Z","iopub.execute_input":"2022-08-12T08:18:31.802126Z","iopub.status.idle":"2022-08-12T08:18:31.833166Z","shell.execute_reply.started":"2022-08-12T08:18:31.802066Z","shell.execute_reply":"2022-08-12T08:18:31.832015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = pd.DataFrame(to_categorical(y_train,10))","metadata":{"execution":{"iopub.status.busy":"2022-08-12T08:18:31.837901Z","iopub.execute_input":"2022-08-12T08:18:31.840448Z","iopub.status.idle":"2022-08-12T08:18:31.848530Z","shell.execute_reply.started":"2022-08-12T08:18:31.840412Z","shell.execute_reply":"2022-08-12T08:18:31.847583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-12T08:18:31.853134Z","iopub.execute_input":"2022-08-12T08:18:31.855677Z","iopub.status.idle":"2022-08-12T08:18:31.866790Z","shell.execute_reply.started":"2022-08-12T08:18:31.855640Z","shell.execute_reply":"2022-08-12T08:18:31.865526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_img(df,y_df,index):\n    plt.imshow(np.array(df.loc[index].values).reshape((28,28)))\n    plt.title('Label is ' + str(y_df.loc[index]))\nshow_img(train_df,y_train,56)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T08:18:31.871123Z","iopub.execute_input":"2022-08-12T08:18:31.872184Z","iopub.status.idle":"2022-08-12T08:18:32.083361Z","shell.execute_reply.started":"2022-08-12T08:18:31.872149Z","shell.execute_reply":"2022-08-12T08:18:32.082192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.loc[19]","metadata":{"execution":{"iopub.status.busy":"2022-08-12T08:18:32.084870Z","iopub.execute_input":"2022-08-12T08:18:32.086332Z","iopub.status.idle":"2022-08-12T08:18:32.095515Z","shell.execute_reply.started":"2022-08-12T08:18:32.086291Z","shell.execute_reply":"2022-08-12T08:18:32.094267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set = tf.data.Dataset.from_tensor_slices((train_df,y_train))\ndef norm(i,j):\n    \n    return (tf.reshape(i,(28,28,1))/255,j)\ntrain_set= train_set.map(norm)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T08:18:32.097554Z","iopub.execute_input":"2022-08-12T08:18:32.097934Z","iopub.status.idle":"2022-08-12T08:18:32.618033Z","shell.execute_reply.started":"2022-08-12T08:18:32.097893Z","shell.execute_reply":"2022-08-12T08:18:32.617107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_val = pd.DataFrame(to_categorical(y_val,10))","metadata":{"execution":{"iopub.status.busy":"2022-08-12T08:18:32.623175Z","iopub.execute_input":"2022-08-12T08:18:32.623513Z","iopub.status.idle":"2022-08-12T08:18:32.631429Z","shell.execute_reply.started":"2022-08-12T08:18:32.623482Z","shell.execute_reply":"2022-08-12T08:18:32.630448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_val.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-12T08:18:32.633018Z","iopub.execute_input":"2022-08-12T08:18:32.633876Z","iopub.status.idle":"2022-08-12T08:18:32.642003Z","shell.execute_reply.started":"2022-08-12T08:18:32.633839Z","shell.execute_reply":"2022-08-12T08:18:32.640828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_set = tf.data.Dataset.from_tensor_slices((val_df,y_val))\nval_set= val_set.map(norm)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T08:18:32.643624Z","iopub.execute_input":"2022-08-12T08:18:32.644244Z","iopub.status.idle":"2022-08-12T08:18:32.741445Z","shell.execute_reply.started":"2022-08-12T08:18:32.644198Z","shell.execute_reply":"2022-08-12T08:18:32.740362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_set = val_set.batch(128) \ntrain_set = train_set.batch(128) ","metadata":{"execution":{"iopub.status.busy":"2022-08-12T08:18:32.743142Z","iopub.execute_input":"2022-08-12T08:18:32.743784Z","iopub.status.idle":"2022-08-12T08:18:32.749477Z","shell.execute_reply.started":"2022-08-12T08:18:32.743748Z","shell.execute_reply":"2022-08-12T08:18:32.748410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for (i,j) in val_set.take(1):\n    print(j)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T08:18:32.751075Z","iopub.execute_input":"2022-08-12T08:18:32.751768Z","iopub.status.idle":"2022-08-12T08:18:32.839508Z","shell.execute_reply.started":"2022-08-12T08:18:32.751732Z","shell.execute_reply":"2022-08-12T08:18:32.838472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.Sequential([\n                tf.keras.layers.Input(shape=(28,28,1)),\n                tf.keras.layers.Conv2D(32,kernel_size = (3,3),activation= 'relu',padding = 'same'),\n                tf.keras.layers.Conv2D(32,kernel_size = (3,3),activation= 'relu',padding = 'same'),\n                tf.keras.layers.MaxPool2D((2,2)),\n     tf.keras.layers.Conv2D(64,kernel_size = (3,3),activation= 'relu',padding = 'same'),\n                tf.keras.layers.MaxPool2D((2,2)),\n    tf.keras.layers.Conv2D(128,kernel_size = (3,3),activation= 'relu',padding = 'same'),\n                tf.keras.layers.MaxPool2D((2,2)),\n                tf.keras.layers.Flatten(),\n                tf.keras.layers.Dense(128,activation='relu'),\n                tf.keras.layers.Dense(10,activation='softmax')\n                ])","metadata":{"execution":{"iopub.status.busy":"2022-08-12T08:25:48.328634Z","iopub.execute_input":"2022-08-12T08:25:48.329018Z","iopub.status.idle":"2022-08-12T08:25:48.394958Z","shell.execute_reply.started":"2022-08-12T08:25:48.328985Z","shell.execute_reply":"2022-08-12T08:25:48.394110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T08:25:48.807867Z","iopub.execute_input":"2022-08-12T08:25:48.808504Z","iopub.status.idle":"2022-08-12T08:25:48.816187Z","shell.execute_reply.started":"2022-08-12T08:25:48.808463Z","shell.execute_reply":"2022-08-12T08:25:48.815207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = 'adam',loss = tf.keras.losses.CategoricalCrossentropy(),metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-08-12T08:25:49.272126Z","iopub.execute_input":"2022-08-12T08:25:49.274485Z","iopub.status.idle":"2022-08-12T08:25:49.284634Z","shell.execute_reply.started":"2022-08-12T08:25:49.274452Z","shell.execute_reply":"2022-08-12T08:25:49.283385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train_set, batch_size=128,\n    verbose=1,epochs = 40,validation_data=val_set)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T08:25:49.917837Z","iopub.execute_input":"2022-08-12T08:25:49.918219Z","iopub.status.idle":"2022-08-12T08:27:36.317025Z","shell.execute_reply.started":"2022-08-12T08:25:49.918185Z","shell.execute_reply":"2022-08-12T08:27:36.315989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = tf.data.Dataset.from_tensor_slices(test_data).map(lambda x : tf.reshape(x,(28,28,1))).batch(1)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T08:20:45.307602Z","iopub.execute_input":"2022-08-12T08:20:45.307966Z","iopub.status.idle":"2022-08-12T08:20:45.747094Z","shell.execute_reply.started":"2022-08-12T08:20:45.307932Z","shell.execute_reply":"2022-08-12T08:20:45.745954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = model.predict(test_df)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T08:20:45.749247Z","iopub.execute_input":"2022-08-12T08:20:45.749607Z","iopub.status.idle":"2022-08-12T08:21:21.631500Z","shell.execute_reply.started":"2022-08-12T08:20:45.749571Z","shell.execute_reply":"2022-08-12T08:21:21.630467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predsfinal = [i.argmax() for i in preds]","metadata":{"execution":{"iopub.status.busy":"2022-08-12T08:21:21.633610Z","iopub.execute_input":"2022-08-12T08:21:21.633988Z","iopub.status.idle":"2022-08-12T08:21:21.653953Z","shell.execute_reply.started":"2022-08-12T08:21:21.633953Z","shell.execute_reply":"2022-08-12T08:21:21.653125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_img(test_data,pd.DataFrame(predsfinal),34)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T08:21:21.655408Z","iopub.execute_input":"2022-08-12T08:21:21.655795Z","iopub.status.idle":"2022-08-12T08:21:21.848698Z","shell.execute_reply.started":"2022-08-12T08:21:21.655760Z","shell.execute_reply":"2022-08-12T08:21:21.847708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_id = pd.Series(range(1,28001),name='ImageId')\npreds = pd.Series(predsfinal,name = 'Label')\npred = pd.concat([image_id,preds],axis=1)\npred.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T08:21:21.850881Z","iopub.execute_input":"2022-08-12T08:21:21.852316Z","iopub.status.idle":"2022-08-12T08:21:21.890765Z","shell.execute_reply.started":"2022-08-12T08:21:21.852280Z","shell.execute_reply":"2022-08-12T08:21:21.889965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}