{"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-09-23T18:18:36.357378Z","iopub.execute_input":"2023-09-23T18:18:36.357825Z","iopub.status.idle":"2023-09-23T18:18:36.713763Z","shell.execute_reply.started":"2023-09-23T18:18:36.357782Z","shell.execute_reply":"2023-09-23T18:18:36.712767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Import Libraries","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport plotly.express as px\n\nfrom sklearn.cluster import MiniBatchKMeans\nfrom sklearn.decomposition import PCA\n\nimport tensorflow as tf\nimport tensorflow.keras.layers as tfl\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\n\nfrom tensorflow.keras.callbacks import EarlyStopping","metadata":{"execution":{"iopub.status.busy":"2023-09-23T18:18:36.715658Z","iopub.execute_input":"2023-09-23T18:18:36.716771Z","iopub.status.idle":"2023-09-23T18:18:40.809900Z","shell.execute_reply.started":"2023-09-23T18:18:36.716734Z","shell.execute_reply":"2023-09-23T18:18:40.808888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Take a look at Data","metadata":{}},{"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":"2023-09-23T18:18:40.811553Z","iopub.execute_input":"2023-09-23T18:18:40.812356Z","iopub.status.idle":"2023-09-23T18:18:45.431466Z","shell.execute_reply.started":"2023-09-23T18:18:40.812318Z","shell.execute_reply":"2023-09-23T18:18:45.430282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(1)","metadata":{"execution":{"iopub.status.busy":"2023-09-23T18:18:45.434370Z","iopub.execute_input":"2023-09-23T18:18:45.434762Z","iopub.status.idle":"2023-09-23T18:18:45.458081Z","shell.execute_reply.started":"2023-09-23T18:18:45.434726Z","shell.execute_reply":"2023-09-23T18:18:45.457221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-23T18:18:45.459760Z","iopub.execute_input":"2023-09-23T18:18:45.460684Z","iopub.status.idle":"2023-09-23T18:18:45.466700Z","shell.execute_reply.started":"2023-09-23T18:18:45.460650Z","shell.execute_reply":"2023-09-23T18:18:45.465787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = train.iloc[:,1:].iloc[780].to_numpy() \nimg = img.reshape((28, 28))\nplt.imshow(img, cmap= plt.cm.binary)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-23T18:18:45.468189Z","iopub.execute_input":"2023-09-23T18:18:45.468601Z","iopub.status.idle":"2023-09-23T18:18:45.700999Z","shell.execute_reply.started":"2023-09-23T18:18:45.468569Z","shell.execute_reply":"2023-09-23T18:18:45.699988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.bar(y = train['label'].value_counts(), color= train['label'].value_counts(), \n       title=\"Number of digit classes\",\n       labels={\"y\":\"Counts\", \"x\":\"Label\"})\nfig.update_coloraxes(showscale=False)\nfig.update_layout(title_x=0.5, title_font=dict(size=25),\n                 xaxis=dict(tickmode='linear'))","metadata":{"execution":{"iopub.status.busy":"2023-09-23T18:18:45.702478Z","iopub.execute_input":"2023-09-23T18:18:45.702795Z","iopub.status.idle":"2023-09-23T18:18:46.067405Z","shell.execute_reply.started":"2023-09-23T18:18:45.702770Z","shell.execute_reply":"2023-09-23T18:18:46.066491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Spliting","metadata":{}},{"cell_type":"code","source":"X = train.drop(\"label\", axis=1)\ny = train['label']","metadata":{"execution":{"iopub.status.busy":"2023-09-23T18:18:46.068766Z","iopub.execute_input":"2023-09-23T18:18:46.069492Z","iopub.status.idle":"2023-09-23T18:18:46.152504Z","shell.execute_reply.started":"2023-09-23T18:18:46.069456Z","shell.execute_reply":"2023-09-23T18:18:46.151297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X, y, \n                                                    test_size=0.2,\n                                                    shuffle=True,\n                                                    random_state=42 )","metadata":{"execution":{"iopub.status.busy":"2023-09-23T18:18:46.154369Z","iopub.execute_input":"2023-09-23T18:18:46.155540Z","iopub.status.idle":"2023-09-23T18:18:46.371435Z","shell.execute_reply.started":"2023-09-23T18:18:46.155502Z","shell.execute_reply":"2023-09-23T18:18:46.370322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape, X_val.shape, y_train.shape, y_val.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-23T18:18:46.376008Z","iopub.execute_input":"2023-09-23T18:18:46.376367Z","iopub.status.idle":"2023-09-23T18:18:46.385817Z","shell.execute_reply.started":"2023-09-23T18:18:46.376340Z","shell.execute_reply":"2023-09-23T18:18:46.384628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data preparing","metadata":{}},{"cell_type":"markdown","source":"#### Encode Labels","metadata":{}},{"cell_type":"code","source":"def encode(y_train, y_val):\n    y_train = to_categorical(y_train, num_classes=10)\n    y_val = to_categorical(y_val, num_classes=10)\n    \n    return y_train, y_val","metadata":{"execution":{"iopub.status.busy":"2023-09-23T18:18:46.387553Z","iopub.execute_input":"2023-09-23T18:18:46.388247Z","iopub.status.idle":"2023-09-23T18:18:46.394429Z","shell.execute_reply.started":"2023-09-23T18:18:46.388212Z","shell.execute_reply":"2023-09-23T18:18:46.393186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train, y_val= encode(y_train, y_val)","metadata":{"execution":{"iopub.status.busy":"2023-09-23T18:18:46.395837Z","iopub.execute_input":"2023-09-23T18:18:46.396310Z","iopub.status.idle":"2023-09-23T18:18:46.407261Z","shell.execute_reply.started":"2023-09-23T18:18:46.396231Z","shell.execute_reply":"2023-09-23T18:18:46.406312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Normalize Images","metadata":{}},{"cell_type":"code","source":"def normalizeImgs(X_train, X_val, test):\n    X_train /= 255\n    X_val /= 255\n    test /= 255","metadata":{"execution":{"iopub.status.busy":"2023-09-23T18:18:46.408526Z","iopub.execute_input":"2023-09-23T18:18:46.409845Z","iopub.status.idle":"2023-09-23T18:18:46.416979Z","shell.execute_reply.started":"2023-09-23T18:18:46.409811Z","shell.execute_reply":"2023-09-23T18:18:46.415970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normalizeImgs(X_train, X_val, test)","metadata":{"execution":{"iopub.status.busy":"2023-09-23T18:18:46.419564Z","iopub.execute_input":"2023-09-23T18:18:46.419826Z","iopub.status.idle":"2023-09-23T18:18:46.619349Z","shell.execute_reply.started":"2023-09-23T18:18:46.419803Z","shell.execute_reply":"2023-09-23T18:18:46.618296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Reshape Images","metadata":{}},{"cell_type":"code","source":"def reshape(X_train, X_val, test):\n    X_train = np.array(X_train).reshape(X_train.shape[0],28,28,1)\n    X_val = X_val.to_numpy().reshape(X_val.shape[0],28,28,1)\n    test = test.to_numpy().reshape(test.shape[0],28,28,1)\n    return X_train, X_val, test","metadata":{"execution":{"iopub.status.busy":"2023-09-23T18:18:46.620624Z","iopub.execute_input":"2023-09-23T18:18:46.620987Z","iopub.status.idle":"2023-09-23T18:18:46.627962Z","shell.execute_reply.started":"2023-09-23T18:18:46.620954Z","shell.execute_reply":"2023-09-23T18:18:46.626922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, test = reshape(X_train, X_val, test)","metadata":{"execution":{"iopub.status.busy":"2023-09-23T18:18:46.629496Z","iopub.execute_input":"2023-09-23T18:18:46.630158Z","iopub.status.idle":"2023-09-23T18:18:46.713567Z","shell.execute_reply.started":"2023-09-23T18:18:46.630125Z","shell.execute_reply":"2023-09-23T18:18:46.712550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape, X_val.shape,test.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-23T18:18:46.715207Z","iopub.execute_input":"2023-09-23T18:18:46.715571Z","iopub.status.idle":"2023-09-23T18:18:46.722316Z","shell.execute_reply.started":"2023-09-23T18:18:46.715538Z","shell.execute_reply":"2023-09-23T18:18:46.721382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"g = plt.imshow(X_train[1][:,:,0])","metadata":{"execution":{"iopub.status.busy":"2023-09-23T18:18:46.723770Z","iopub.execute_input":"2023-09-23T18:18:46.724569Z","iopub.status.idle":"2023-09-23T18:18:46.979905Z","shell.execute_reply.started":"2023-09-23T18:18:46.724535Z","shell.execute_reply":"2023-09-23T18:18:46.978949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Net Architecture","metadata":{}},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(tfl.Conv2D(filters = 45, kernel_size = (3,3),padding = 'Same', \n                 activation ='relu', input_shape = (28,28,1)))\nmodel.add(tfl.Conv2D(filters = 40, kernel_size = (3,3),padding = 'Same', \n                 activation ='relu'))\nmodel.add(tfl.MaxPool2D(pool_size=(2,2)))\nmodel.add(tfl.Dropout(0.25))\n\n\nmodel.add(tfl.Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', \n                 activation ='relu'))\n\nmodel.add(tfl.MaxPool2D(pool_size=(2,2), strides=(2,2)))\nmodel.add(tfl.Dropout(0.25))\n\n\nmodel.add(tfl.Flatten())\nmodel.add(tfl.Dense(256, activation = \"relu\"))\nmodel.add(tfl.Dropout(0.5))\n\nmodel.add(tfl.Dense(128, activation='relu'))\nmodel.add(tfl.Dense(10, activation = \"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2023-09-23T18:18:46.981284Z","iopub.execute_input":"2023-09-23T18:18:46.981639Z","iopub.status.idle":"2023-09-23T18:18:48.373595Z","shell.execute_reply.started":"2023-09-23T18:18:46.981606Z","shell.execute_reply":"2023-09-23T18:18:48.372611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss=\"categorical_crossentropy\",\n              optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-09-23T18:18:48.374782Z","iopub.execute_input":"2023-09-23T18:18:48.375152Z","iopub.status.idle":"2023-09-23T18:18:48.393370Z","shell.execute_reply.started":"2023-09-23T18:18:48.375118Z","shell.execute_reply":"2023-09-23T18:18:48.392488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-09-23T18:18:48.395579Z","iopub.execute_input":"2023-09-23T18:18:48.396273Z","iopub.status.idle":"2023-09-23T18:18:48.430275Z","shell.execute_reply.started":"2023-09-23T18:18:48.396240Z","shell.execute_reply":"2023-09-23T18:18:48.429547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stop = EarlyStopping(monitor='val_loss',patience=15)","metadata":{"execution":{"iopub.status.busy":"2023-09-23T18:18:48.431149Z","iopub.execute_input":"2023-09-23T18:18:48.431447Z","iopub.status.idle":"2023-09-23T18:18:48.435959Z","shell.execute_reply.started":"2023-09-23T18:18:48.431418Z","shell.execute_reply":"2023-09-23T18:18:48.435260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epoch = 100\nmodel.fit(X_train, y_train, batch_size=32, epochs=epoch, \n          validation_data=(X_val,y_val),callbacks=[early_stop])","metadata":{"execution":{"iopub.status.busy":"2023-09-23T18:18:48.437097Z","iopub.execute_input":"2023-09-23T18:18:48.437407Z","iopub.status.idle":"2023-09-23T18:21:49.271730Z","shell.execute_reply.started":"2023-09-23T18:18:48.437377Z","shell.execute_reply":"2023-09-23T18:21:49.270782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.history.history","metadata":{"execution":{"iopub.status.busy":"2023-09-23T18:22:38.508393Z","iopub.execute_input":"2023-09-23T18:22:38.508773Z","iopub.status.idle":"2023-09-23T18:22:38.518077Z","shell.execute_reply.started":"2023-09-23T18:22:38.508743Z","shell.execute_reply":"2023-09-23T18:22:38.517009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.graph_objs as go\nfrom plotly.subplots import make_subplots\n\n# Create a subplot with two rows and one column\nfig = make_subplots(rows=2, cols=1, subplot_titles=(\"Accuracy\", \"Loss\"))\n\n# Add training accuracy to the first subplot\nfig.add_trace(go.Scatter(x=list(range(epoch)), y=model.history.history['accuracy'], mode='lines', name='Train Accuracy'), row=1, col=1)\n\n# Add validation accuracy to the first subplot\nfig.add_trace(go.Scatter(x=list(range(epoch)), y=model.history.history['val_accuracy'], mode='lines', name='Val Accuracy'), row=1, col=1)\n\n# Add training loss to the second subplot\nfig.add_trace(go.Scatter(x=list(range(epoch)), y=model.history.history['loss'], mode='lines', name='Train Loss'), row=2, col=1)\n\n# Add validation loss to the second subplot\nfig.add_trace(go.Scatter(x=list(range(epoch)), y=model.history.history['val_loss'], mode='lines', name='Val Loss'), row=2, col=1)\n\n# Update the layout with the title\nfig.update_layout(title='Model Accuracy and Loss')\n\n# Show the figure\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-23T18:22:59.281977Z","iopub.execute_input":"2023-09-23T18:22:59.282926Z","iopub.status.idle":"2023-09-23T18:22:59.323560Z","shell.execute_reply.started":"2023-09-23T18:22:59.282895Z","shell.execute_reply":"2023-09-23T18:22:59.322505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Get prediction for test data\ny_pred_final = model.predict(test)\ny_pred_final","metadata":{"execution":{"iopub.status.busy":"2023-09-23T18:23:01.918649Z","iopub.execute_input":"2023-09-23T18:23:01.919316Z","iopub.status.idle":"2023-09-23T18:23:04.288140Z","shell.execute_reply.started":"2023-09-23T18:23:01.919283Z","shell.execute_reply":"2023-09-23T18:23:04.287081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_classes = np.argmax(y_pred_final,axis = 1) \ny_pred_classes","metadata":{"execution":{"iopub.status.busy":"2023-09-23T18:23:04.290139Z","iopub.execute_input":"2023-09-23T18:23:04.290498Z","iopub.status.idle":"2023-09-23T18:23:04.307404Z","shell.execute_reply.started":"2023-09-23T18:23:04.290464Z","shell.execute_reply":"2023-09-23T18:23:04.304807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ImageID = np.arange(len(y_pred_classes))+1\nOut = pd.DataFrame([ImageID,y_pred_classes]).T\nOut.rename(columns = {0:'ImageId', 1:'Label'})\n#Out\nOut.to_csv('Submission.csv', header =  ['ImageId', 'Label' ], index = None)","metadata":{"execution":{"iopub.status.busy":"2023-09-23T18:23:07.715502Z","iopub.execute_input":"2023-09-23T18:23:07.715881Z","iopub.status.idle":"2023-09-23T18:23:08.629499Z","shell.execute_reply.started":"2023-09-23T18:23:07.715851Z","shell.execute_reply":"2023-09-23T18:23:08.628477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}