{"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":"import pandas as pd\n\n# load the dataset\ntrain = pd.read_csv(\"../input/digit-recognizer/train.csv\")\ntest = pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-23T11:50:50.925600Z","iopub.execute_input":"2022-07-23T11:50:50.926056Z","iopub.status.idle":"2022-07-23T11:50:56.623575Z","shell.execute_reply.started":"2022-07-23T11:50:50.925958Z","shell.execute_reply":"2022-07-23T11:50:56.622597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# take a look on some training samples\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-23T11:50:56.625100Z","iopub.execute_input":"2022-07-23T11:50:56.625561Z","iopub.status.idle":"2022-07-23T11:50:56.656727Z","shell.execute_reply.started":"2022-07-23T11:50:56.625531Z","shell.execute_reply":"2022-07-23T11:50:56.655688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# take a look on some testing samples\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-23T11:50:56.658281Z","iopub.execute_input":"2022-07-23T11:50:56.658600Z","iopub.status.idle":"2022-07-23T11:50:56.675338Z","shell.execute_reply.started":"2022-07-23T11:50:56.658562Z","shell.execute_reply":"2022-07-23T11:50:56.674600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data pre-processing\n# split the dataset into features and labels\nX_train = train.drop(['label'], axis=1).astype('float32')\ny_train = train['label'].astype('int32')\n\nX_test = test","metadata":{"execution":{"iopub.status.busy":"2022-07-23T11:50:56.677318Z","iopub.execute_input":"2022-07-23T11:50:56.677805Z","iopub.status.idle":"2022-07-23T11:50:56.877996Z","shell.execute_reply.started":"2022-07-23T11:50:56.677775Z","shell.execute_reply":"2022-07-23T11:50:56.876956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check some data statistics like min and max values present\n# in the dataset\nX_train.describe();","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\n\n# Standardize the features\n#After applying StandardScaler(), each column in X will have mean of 0 and standard deviation of 1.\nscaler = StandardScaler()\nX_train_scaled = scaler.fit_transform(X_train)\nX_test_scaled = scaler.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T11:50:56.879839Z","iopub.execute_input":"2022-07-23T11:50:56.880208Z","iopub.status.idle":"2022-07-23T11:50:58.264523Z","shell.execute_reply.started":"2022-07-23T11:50:56.880173Z","shell.execute_reply":"2022-07-23T11:50:58.263484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# reashaping the 784 pixels into 28 x 28 matrix which represents the pixels of the actual image\n# as input we will have an array of these matrices\nX_train_scaled = X_train_scaled.reshape(X_train_scaled.shape[0],28,28,1)\nX_test_scaled = X_test_scaled.reshape(X_test_scaled.shape[0],28,28,1)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T11:50:58.265709Z","iopub.execute_input":"2022-07-23T11:50:58.266014Z","iopub.status.idle":"2022-07-23T11:50:58.271895Z","shell.execute_reply.started":"2022-07-23T11:50:58.265985Z","shell.execute_reply":"2022-07-23T11:50:58.270777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Conv2D, Dropout, Flatten, MaxPool2D\n\n# Building a sequential model with convolutional layers \n# and adding MaxPools to reduce the number of network parameters\n# We are also using dropout layers to prevent overfitting\nmodel = Sequential()\n\nmodel.add(Conv2D(32, (5, 5),padding ='Same', activation='relu', input_shape=X_train_scaled.shape[1:]))\nmodel.add(Conv2D(32, (5, 5),padding ='Same', activation ='relu'))\nmodel.add(MaxPool2D((2, 2)))\nmodel.add(Dropout(.25))\n\nmodel.add(Conv2D(64,(3, 3),padding ='Same', activation ='relu'))\nmodel.add(Conv2D(64, (3, 3),padding ='Same', activation ='relu'))\nmodel.add(MaxPool2D(pool_size=(2, 2), strides=(2, 2)))\nmodel.add(Dropout(.25))\n\n# reshape the output of convolutional layers so we can feed to the dense layers\nmodel.add(Flatten())\n\nmodel.add(Dense(256, activation='relu'))\nmodel.add(Dropout(.5))\nmodel.add(Dense(10, activation='softmax'))\n\nmodel.compile(optimizer=tf.keras.optimizers.Adam(), \n              loss=tf.keras.losses.SparseCategoricalCrossentropy(), \n              metrics=['accuracy']\n             )","metadata":{"execution":{"iopub.status.busy":"2022-07-23T11:50:58.273300Z","iopub.execute_input":"2022-07-23T11:50:58.274232Z","iopub.status.idle":"2022-07-23T11:51:05.025953Z","shell.execute_reply.started":"2022-07-23T11:50:58.274195Z","shell.execute_reply":"2022-07-23T11:51:05.024637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the model for 10 epochs\nmodel.fit(x=X_train_scaled,y=y_train, epochs=10, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T11:51:05.027664Z","iopub.execute_input":"2022-07-23T11:51:05.028092Z","iopub.status.idle":"2022-07-23T12:05:34.245189Z","shell.execute_reply.started":"2022-07-23T11:51:05.028057Z","shell.execute_reply":"2022-07-23T12:05:34.244093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\n\n# pick a random sample represented by the image_index from the test dataset to check the trained model\nimage_index = 1\n\npred = model.predict(X_test_scaled)\n\npred = np.argmax(pred, axis=1)\nplt.imshow(X_test_scaled[image_index])\nprint(f'Predicted label is: {pred[image_index]}')","metadata":{"execution":{"iopub.status.busy":"2022-07-23T12:05:44.786575Z","iopub.execute_input":"2022-07-23T12:05:44.786981Z","iopub.status.idle":"2022-07-23T12:06:06.773044Z","shell.execute_reply.started":"2022-07-23T12:05:44.786946Z","shell.execute_reply":"2022-07-23T12:06:06.771804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create a submission dataframe containing the predicted values of the test dataset\nsubmission = pd.DataFrame({'ImageId': list(range(1, len(pred)+1)), 'Label': pred})\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-23T12:06:12.854271Z","iopub.execute_input":"2022-07-23T12:06:12.854725Z","iopub.status.idle":"2022-07-23T12:06:12.881889Z","shell.execute_reply.started":"2022-07-23T12:06:12.854683Z","shell.execute_reply":"2022-07-23T12:06:12.881100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# save the dataframe to a .csv file\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T12:06:15.389273Z","iopub.execute_input":"2022-07-23T12:06:15.389941Z","iopub.status.idle":"2022-07-23T12:06:15.443323Z","shell.execute_reply.started":"2022-07-23T12:06:15.389892Z","shell.execute_reply":"2022-07-23T12:06:15.441917Z"},"trusted":true},"execution_count":null,"outputs":[]}]}