{"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-05T04:11:52.721033Z","iopub.execute_input":"2023-07-05T04:11:52.721388Z","iopub.status.idle":"2023-07-05T04:11:52.729501Z","shell.execute_reply.started":"2023-07-05T04:11:52.721357Z","shell.execute_reply":"2023-07-05T04:11:52.728504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndf_test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2023-07-05T04:11:53.243346Z","iopub.execute_input":"2023-07-05T04:11:53.243692Z","iopub.status.idle":"2023-07-05T04:11:58.714145Z","shell.execute_reply.started":"2023-07-05T04:11:53.243662Z","shell.execute_reply":"2023-07-05T04:11:58.713168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.sample(10)","metadata":{"execution":{"iopub.status.busy":"2023-07-05T04:11:58.716014Z","iopub.execute_input":"2023-07-05T04:11:58.716373Z","iopub.status.idle":"2023-07-05T04:11:58.745790Z","shell.execute_reply.started":"2023-07-05T04:11:58.716340Z","shell.execute_reply":"2023-07-05T04:11:58.744812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-07-05T04:11:58.747209Z","iopub.execute_input":"2023-07-05T04:11:58.747610Z","iopub.status.idle":"2023-07-05T04:11:58.756249Z","shell.execute_reply.started":"2023-07-05T04:11:58.747579Z","shell.execute_reply":"2023-07-05T04:11:58.755054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.columns","metadata":{"execution":{"iopub.status.busy":"2023-07-05T04:11:58.759264Z","iopub.execute_input":"2023-07-05T04:11:58.759849Z","iopub.status.idle":"2023-07-05T04:11:58.769117Z","shell.execute_reply.started":"2023-07-05T04:11:58.759814Z","shell.execute_reply":"2023-07-05T04:11:58.768016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = df_train.drop('label',axis=1)\nY_train = df_train['label']","metadata":{"execution":{"iopub.status.busy":"2023-07-05T04:13:23.720841Z","iopub.execute_input":"2023-07-05T04:13:23.721212Z","iopub.status.idle":"2023-07-05T04:13:23.806111Z","shell.execute_reply.started":"2023-07-05T04:13:23.721183Z","shell.execute_reply":"2023-07-05T04:13:23.804920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-07-05T04:11:58.862710Z","iopub.execute_input":"2023-07-05T04:11:58.863161Z","iopub.status.idle":"2023-07-05T04:11:58.875906Z","shell.execute_reply.started":"2023-07-05T04:11:58.863128Z","shell.execute_reply":"2023-07-05T04:11:58.874916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-07-05T04:11:58.878496Z","iopub.execute_input":"2023-07-05T04:11:58.878785Z","iopub.status.idle":"2023-07-05T04:11:58.886743Z","shell.execute_reply.started":"2023-07-05T04:11:58.878761Z","shell.execute_reply":"2023-07-05T04:11:58.885621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\ndef plot_image(image,cmap,label):\n    plt.imshow(image,cmap=cmap)\n    plt.title(f'Label: {label}')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-05T04:11:58.889460Z","iopub.execute_input":"2023-07-05T04:11:58.889786Z","iopub.status.idle":"2023-07-05T04:11:58.895829Z","shell.execute_reply.started":"2023-07-05T04:11:58.889756Z","shell.execute_reply":"2023-07-05T04:11:58.894920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = np.array(X_train.iloc[0,:]).reshape(-1,28)\nlabel = Y_train[0]\nplot_image(img,'gray',label)","metadata":{"execution":{"iopub.status.busy":"2023-07-05T04:11:59.211461Z","iopub.execute_input":"2023-07-05T04:11:59.212391Z","iopub.status.idle":"2023-07-05T04:11:59.474813Z","shell.execute_reply.started":"2023-07-05T04:11:59.212348Z","shell.execute_reply":"2023-07-05T04:11:59.472930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers,Sequential","metadata":{"execution":{"iopub.status.busy":"2023-07-05T04:13:05.500444Z","iopub.execute_input":"2023-07-05T04:13:05.501192Z","iopub.status.idle":"2023-07-05T04:13:05.506120Z","shell.execute_reply.started":"2023-07-05T04:13:05.501155Z","shell.execute_reply":"2023-07-05T04:13:05.505211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# noramlizing the ranges of the pixels intensty\nX_train = X_train/255\nX_test  = df_test/255\n\n# reshape the train,test\nX_train = X_train.values.reshape(-1,28,28,1)\nX_test  = X_test.values.reshape(-1,28,28,1)\n\n# convert the label into one-hot encoding \nY_train = keras.utils.to_categorical(Y_train, 10)","metadata":{"execution":{"iopub.status.busy":"2023-07-05T04:13:26.641344Z","iopub.execute_input":"2023-07-05T04:13:26.641684Z","iopub.status.idle":"2023-07-05T04:13:26.830046Z","shell.execute_reply.started":"2023-07-05T04:13:26.641655Z","shell.execute_reply":"2023-07-05T04:13:26.829090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# split the data into train , val\nfrom sklearn.model_selection import train_test_split\nx_train, x_val, y_train, y_val = train_test_split(X_train, Y_train, test_size=0.1, random_state=0)\nprint(f'Shape of x_train: {x_train.shape}')\nprint(f'Shape of x_val  : {x_val.shape}')\nprint(f'Shape of y_train: {y_train.shape}')\nprint(f'Shape of x_val  : {y_val.shape}')","metadata":{"execution":{"iopub.status.busy":"2023-07-05T04:13:28.236993Z","iopub.execute_input":"2023-07-05T04:13:28.237347Z","iopub.status.idle":"2023-07-05T04:13:28.989997Z","shell.execute_reply.started":"2023-07-05T04:13:28.237318Z","shell.execute_reply":"2023-07-05T04:13:28.989056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-07-05T03:59:39.735815Z","iopub.execute_input":"2023-07-05T03:59:39.736209Z","iopub.status.idle":"2023-07-05T03:59:39.741997Z","shell.execute_reply.started":"2023-07-05T03:59:39.736171Z","shell.execute_reply":"2023-07-05T03:59:39.740776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# build the model (Arch)\n\nmodel = Sequential([\n    # 1st layer\n    layers.Conv2D(filters = 64,kernel_size=(3,3),strides=(1, 1),padding='same',activation = 'relu',input_shape = (28,28,1)),\n    layers.MaxPooling2D(pool_size = (2,2)),\n    \n    # 2nd layer\n    layers.Conv2D(filters = 32,kernel_size=(3,3),strides=(1, 1),padding='same',activation = 'relu'),\n    layers.MaxPooling2D(pool_size = (2,2)),\n    \n    # 3rd layer\n    layers.Conv2D(filters = 16,kernel_size=(3,3),strides=(1, 1),padding='same',activation = 'relu'),\n    layers.MaxPooling2D(pool_size = (2,2)),\n    \n    #MLPs\n    layers.Flatten(),\n    layers.Dense(units = 255,activation='relu'),\n    layers.Dropout(rate =0.2),\n    layers.Dense(units = 15,activation='relu'),\n    layers.Dropout(rate =0.2),\n    layers.Dense(units = 10,activation='softmax')\n])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-07-05T04:19:21.538939Z","iopub.execute_input":"2023-07-05T04:19:21.539309Z","iopub.status.idle":"2023-07-05T04:19:21.654660Z","shell.execute_reply.started":"2023-07-05T04:19:21.539279Z","shell.execute_reply":"2023-07-05T04:19:21.653950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = tf.optimizers.Adam()\nmodel.compile(optimizer,loss ='categorical_crossentropy',metrics=['acc'])","metadata":{"execution":{"iopub.status.busy":"2023-07-05T04:19:29.668809Z","iopub.execute_input":"2023-07-05T04:19:29.669267Z","iopub.status.idle":"2023-07-05T04:19:29.684286Z","shell.execute_reply.started":"2023-07-05T04:19:29.669233Z","shell.execute_reply":"2023-07-05T04:19:29.683459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(x=x_train,y=y_train,batch_size = 64,epochs = 20,validation_data = (x_val,y_val))","metadata":{"execution":{"iopub.status.busy":"2023-07-05T04:19:40.915677Z","iopub.execute_input":"2023-07-05T04:19:40.916037Z","iopub.status.idle":"2023-07-05T04:20:40.863895Z","shell.execute_reply.started":"2023-07-05T04:19:40.916006Z","shell.execute_reply":"2023-07-05T04:20:40.863011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history.history.keys()","metadata":{"execution":{"iopub.status.busy":"2023-07-05T04:20:44.195726Z","iopub.execute_input":"2023-07-05T04:20:44.196734Z","iopub.status.idle":"2023-07-05T04:20:44.203045Z","shell.execute_reply.started":"2023-07-05T04:20:44.196687Z","shell.execute_reply":"2023-07-05T04:20:44.202064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nhistory_frame = pd.DataFrame(history.history)\nhistory_frame.loc[:, ['loss', 'val_loss']].plot()\nhistory_frame.loc[:, ['acc', 'val_acc']].plot();","metadata":{"execution":{"iopub.status.busy":"2023-07-05T04:20:45.603616Z","iopub.execute_input":"2023-07-05T04:20:45.604314Z","iopub.status.idle":"2023-07-05T04:20:46.154400Z","shell.execute_reply.started":"2023-07-05T04:20:45.604273Z","shell.execute_reply":"2023-07-05T04:20:46.153486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction  = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-07-05T04:22:11.692693Z","iopub.execute_input":"2023-07-05T04:22:11.693072Z","iopub.status.idle":"2023-07-05T04:22:14.607222Z","shell.execute_reply.started":"2023-07-05T04:22:11.693040Z","shell.execute_reply":"2023-07-05T04:22:14.606198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = np.argmax(prediction,axis = 1)\n","metadata":{"execution":{"iopub.status.busy":"2023-07-05T04:23:21.178801Z","iopub.execute_input":"2023-07-05T04:23:21.179539Z","iopub.status.idle":"2023-07-05T04:23:21.185603Z","shell.execute_reply.started":"2023-07-05T04:23:21.179503Z","shell.execute_reply":"2023-07-05T04:23:21.184700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction.shape","metadata":{"execution":{"iopub.status.busy":"2023-07-05T04:23:46.224053Z","iopub.execute_input":"2023-07-05T04:23:46.224408Z","iopub.status.idle":"2023-07-05T04:23:46.230390Z","shell.execute_reply.started":"2023-07-05T04:23:46.224378Z","shell.execute_reply":"2023-07-05T04:23:46.229176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission  = pd.DataFrame({'ImageId':range(1,28001),'Label':prediction})\nsubmission.set_index('ImageId',inplace=True)\nsubmission","metadata":{"execution":{"iopub.status.busy":"2023-07-05T04:29:27.023559Z","iopub.execute_input":"2023-07-05T04:29:27.023947Z","iopub.status.idle":"2023-07-05T04:29:27.040296Z","shell.execute_reply.started":"2023-07-05T04:29:27.023915Z","shell.execute_reply":"2023-07-05T04:29:27.038767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv',index=True)","metadata":{"execution":{"iopub.status.busy":"2023-07-05T04:30:23.271206Z","iopub.execute_input":"2023-07-05T04:30:23.271581Z","iopub.status.idle":"2023-07-05T04:30:23.339503Z","shell.execute_reply.started":"2023-07-05T04:30:23.271549Z","shell.execute_reply":"2023-07-05T04:30:23.338624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ! kaggle competitions submit -c digit-recognizer -f submission.csv -m \"Message\"","metadata":{"execution":{"iopub.status.busy":"2023-07-05T04:38:34.258050Z","iopub.execute_input":"2023-07-05T04:38:34.258989Z","iopub.status.idle":"2023-07-05T04:38:34.264203Z","shell.execute_reply.started":"2023-07-05T04:38:34.258938Z","shell.execute_reply":"2023-07-05T04:38:34.263145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}