{"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 matplotlib.pyplot as plt\n%matplotlib inline\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n\n# Machine Learning \nfrom tensorflow import keras\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, Dense, Dropout, Flatten, MaxPooling2D\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 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-02-09T15:57:50.019301Z","iopub.execute_input":"2023-02-09T15:57:50.019776Z","iopub.status.idle":"2023-02-09T15:57:50.034877Z","shell.execute_reply.started":"2023-02-09T15:57:50.019742Z","shell.execute_reply":"2023-02-09T15:57:50.033578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Importing and EDA","metadata":{}},{"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')\n\nprint(train.shape)\nprint(test.shape)\nprint(train)","metadata":{"execution":{"iopub.status.busy":"2023-02-09T16:01:03.540852Z","iopub.execute_input":"2023-02-09T16:01:03.541742Z","iopub.status.idle":"2023-02-09T16:01:07.790004Z","shell.execute_reply.started":"2023-02-09T16:01:03.541699Z","shell.execute_reply":"2023-02-09T16:01:07.788784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### *Checking for null values just to be sure*","metadata":{}},{"cell_type":"code","source":"for col in train_data.columns:\n    if train_data[col].isna().sum() > 0:\n        print(train_data[col].isna().sum())\n        \n# No null values","metadata":{"execution":{"iopub.status.busy":"2023-02-09T15:57:52.583528Z","iopub.status.idle":"2023-02-09T15:57:52.584679Z","shell.execute_reply.started":"2023-02-09T15:57:52.584277Z","shell.execute_reply":"2023-02-09T15:57:52.584419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Pre-Processing","metadata":{}},{"cell_type":"markdown","source":"#### *Separating labels from the data*","metadata":{}},{"cell_type":"code","source":"X = train_data.drop(['label'], axis=1).values\nX.shape","metadata":{"execution":{"iopub.status.busy":"2023-02-09T15:57:52.586061Z","iopub.status.idle":"2023-02-09T15:57:52.587191Z","shell.execute_reply.started":"2023-02-09T15:57:52.586947Z","shell.execute_reply":"2023-02-09T15:57:52.586972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train_data.label.values\ny.shape","metadata":{"execution":{"iopub.status.busy":"2023-02-09T15:57:52.588143Z","iopub.status.idle":"2023-02-09T15:57:52.588961Z","shell.execute_reply.started":"2023-02-09T15:57:52.588748Z","shell.execute_reply":"2023-02-09T15:57:52.588768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test_data.values\ntest.shape","metadata":{"execution":{"iopub.status.busy":"2023-02-09T15:57:52.590068Z","iopub.status.idle":"2023-02-09T15:57:52.59046Z","shell.execute_reply.started":"2023-02-09T15:57:52.590264Z","shell.execute_reply":"2023-02-09T15:57:52.590281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### *Transferring the data to a numpy array*","metadata":{}},{"cell_type":"markdown","source":"## Visualisation","metadata":{}},{"cell_type":"code","source":"# Reshaping our data\nX = X.reshape(-1,28,28,1)\ntest = test.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2023-02-09T15:57:52.591647Z","iopub.status.idle":"2023-02-09T15:57:52.592401Z","shell.execute_reply.started":"2023-02-09T15:57:52.5922Z","shell.execute_reply":"2023-02-09T15:57:52.592221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,10))\nfor i, img in enumerate(X[:25]):\n    plt.subplot(5,5,i+1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.grid(False)\n    plt.imshow(img, cmap='gray')\n    plt.xlabel(train_data['label'][i])\nplt.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2023-02-09T15:57:52.594376Z","iopub.status.idle":"2023-02-09T15:57:52.595046Z","shell.execute_reply.started":"2023-02-09T15:57:52.594752Z","shell.execute_reply":"2023-02-09T15:57:52.59478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model Building","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report","metadata":{"execution":{"iopub.status.busy":"2023-02-09T15:57:52.597484Z","iopub.status.idle":"2023-02-09T15:57:52.598084Z","shell.execute_reply.started":"2023-02-09T15:57:52.597791Z","shell.execute_reply":"2023-02-09T15:57:52.597818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Splitting into our training and test set\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-02-09T15:57:52.599525Z","iopub.status.idle":"2023-02-09T15:57:52.600089Z","shell.execute_reply.started":"2023-02-09T15:57:52.599809Z","shell.execute_reply":"2023-02-09T15:57:52.599837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Converting vector to binary class matrices\n\ny_train = keras.utils.to_categorical(y_train, 10)\ny_test = keras.utils.to_categorical(y_test, 10)","metadata":{"execution":{"iopub.status.busy":"2023-02-09T15:57:52.601651Z","iopub.status.idle":"2023-02-09T15:57:52.602243Z","shell.execute_reply.started":"2023-02-09T15:57:52.601931Z","shell.execute_reply":"2023-02-09T15:57:52.601959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### *Defining our model / compiling and fitting*","metadata":{}},{"cell_type":"code","source":"# Defining our model\ninput_shape = (28, 28, 1)\nmodel = Sequential()\n\nmodel.add(Conv2D(32, activation='relu', kernel_size=(3, 3), input_shape=(input_shape)))\nmodel.add(MaxPooling2D(2, 2))\nmodel.add(Conv2D(64, activation='relu', kernel_size=(3, 3)))\nmodel.add(MaxPooling2D(2, 2))\nmodel.add(Flatten())\nmodel.add(Dropout(0.6))\nmodel.add(Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2023-02-09T15:57:52.604439Z","iopub.status.idle":"2023-02-09T15:57:52.60504Z","shell.execute_reply.started":"2023-02-09T15:57:52.604753Z","shell.execute_reply":"2023-02-09T15:57:52.60478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compiling and fitting to the train set\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\nhistory = model.fit(X_train, y_train, epochs=20, batch_size=128)","metadata":{"execution":{"iopub.status.busy":"2023-02-09T15:57:52.608281Z","iopub.status.idle":"2023-02-09T15:57:52.608756Z","shell.execute_reply.started":"2023-02-09T15:57:52.608548Z","shell.execute_reply":"2023-02-09T15:57:52.608568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### *Evaluating* ","metadata":{}},{"cell_type":"code","source":"# Evaluating our model\nresults = model.evaluate(X_test, y_test, batch_size=20, verbose=0)\nprint(f'Test loss:{results[0]} \\nTest accuracy:{results[1]}')","metadata":{"execution":{"iopub.status.busy":"2023-02-09T15:57:52.610688Z","iopub.status.idle":"2023-02-09T15:57:52.611284Z","shell.execute_reply.started":"2023-02-09T15:57:52.610984Z","shell.execute_reply":"2023-02-09T15:57:52.611013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print keys for pretrained_cnn_history dict\nprint(history.history.keys())\n\nfig = plt.figure(1)\nplt.subplot(211)\n# plot the validation accuracy\nplt.plot(history.history['accuracy'])\nplt.title('Validation accuracy and loss')\nplt.ylabel('Accuracy')\nplt.subplot(212)\n# plot the validation loss\nplt.plot(history.history['loss'], 'r')\nplt.xlabel('Epoch')\nplt.ylabel('Loss value');","metadata":{"execution":{"iopub.status.busy":"2023-02-09T15:57:52.614209Z","iopub.status.idle":"2023-02-09T15:57:52.614637Z","shell.execute_reply.started":"2023-02-09T15:57:52.614432Z","shell.execute_reply":"2023-02-09T15:57:52.614466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### *Predictions and Submission*","metadata":{}},{"cell_type":"code","source":"# Making our predictions\ny_pred = model.predict(test)\ny_pred = np.argmax(y_pred, axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-02-09T15:57:52.616121Z","iopub.status.idle":"2023-02-09T15:57:52.616822Z","shell.execute_reply.started":"2023-02-09T15:57:52.616619Z","shell.execute_reply":"2023-02-09T15:57:52.616639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#submission_data = {'Id': test_data(range(1,28001)), 'label': y_pred}\n#digit_recognized = pd.DataFrame(submission_data)\n\n#digit_recognized.to_csv('digit_recognizer.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-02-09T15:57:52.618317Z","iopub.status.idle":"2023-02-09T15:57:52.618998Z","shell.execute_reply.started":"2023-02-09T15:57:52.618783Z","shell.execute_reply":"2023-02-09T15:57:52.618809Z"},"trusted":true},"execution_count":null,"outputs":[]}]}