{"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)\nimport seaborn as sns\nimport matplotlib.pyplot as plt\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":"2021-11-16T11:20:17.667760Z","iopub.execute_input":"2021-11-16T11:20:17.668375Z","iopub.status.idle":"2021-11-16T11:20:18.042856Z","shell.execute_reply.started":"2021-11-16T11:20:17.668258Z","shell.execute_reply":"2021-11-16T11:20:18.041902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Training Data**","metadata":{}},{"cell_type":"code","source":"df_train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-16T11:20:18.044735Z","iopub.execute_input":"2021-11-16T11:20:18.045024Z","iopub.status.idle":"2021-11-16T11:20:21.107772Z","shell.execute_reply.started":"2021-11-16T11:20:18.044990Z","shell.execute_reply":"2021-11-16T11:20:21.107099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Testing Data**","metadata":{}},{"cell_type":"code","source":"df_test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ndf_test.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-16T11:20:21.108800Z","iopub.execute_input":"2021-11-16T11:20:21.109525Z","iopub.status.idle":"2021-11-16T11:20:22.828772Z","shell.execute_reply.started":"2021-11-16T11:20:21.109485Z","shell.execute_reply":"2021-11-16T11:20:22.827889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Train and Test Images**","metadata":{}},{"cell_type":"code","source":"X_train = df_train.drop('label', axis=1)\ny_train = df_train['label']","metadata":{"execution":{"iopub.status.busy":"2021-11-16T11:20:22.830466Z","iopub.execute_input":"2021-11-16T11:20:22.830873Z","iopub.status.idle":"2021-11-16T11:20:22.917768Z","shell.execute_reply.started":"2021-11-16T11:20:22.830809Z","shell.execute_reply":"2021-11-16T11:20:22.916617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Visualizing the Data**","metadata":{}},{"cell_type":"code","source":"def viz_num(num):\n    #Reshape the 768 values to a 28x28 image\n    image = X_train.values[num].reshape([28,28])\n    plt.title('Sample: %d  Label: %d' % (num, y_train[num]))\n    plt.imshow(image, cmap=plt.get_cmap('gray'))\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-16T11:20:22.920389Z","iopub.execute_input":"2021-11-16T11:20:22.920807Z","iopub.status.idle":"2021-11-16T11:20:22.931640Z","shell.execute_reply.started":"2021-11-16T11:20:22.920676Z","shell.execute_reply":"2021-11-16T11:20:22.930982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"viz_num(1111)","metadata":{"execution":{"iopub.status.busy":"2021-11-16T11:20:22.933088Z","iopub.execute_input":"2021-11-16T11:20:22.933563Z","iopub.status.idle":"2021-11-16T11:20:23.149152Z","shell.execute_reply.started":"2021-11-16T11:20:22.933528Z","shell.execute_reply":"2021-11-16T11:20:23.148345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Formatting the Data**","metadata":{}},{"cell_type":"code","source":"X_train = X_train/255.0\ndf_test = df_test/255.0\nX_train = X_train.values.reshape(-1, 28, 28, 1)\ndf_test = df_test.values.reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2021-11-16T11:20:23.150523Z","iopub.execute_input":"2021-11-16T11:20:23.150980Z","iopub.status.idle":"2021-11-16T11:20:23.248281Z","shell.execute_reply.started":"2021-11-16T11:20:23.150945Z","shell.execute_reply":"2021-11-16T11:20:23.247459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nimport tensorflow\nfrom tensorflow import keras\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Conv2D, MaxPooling2D, Flatten\nfrom tensorflow.keras.optimizers import RMSprop","metadata":{"execution":{"iopub.status.busy":"2021-11-16T11:20:23.249315Z","iopub.execute_input":"2021-11-16T11:20:23.249568Z","iopub.status.idle":"2021-11-16T11:20:25.361232Z","shell.execute_reply.started":"2021-11-16T11:20:23.249537Z","shell.execute_reply":"2021-11-16T11:20:25.360240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_validation, y_train, y_validation = train_test_split(X_train, y_train, test_size=0.2, random_state=42, stratify=y_train)","metadata":{"execution":{"iopub.status.busy":"2021-11-16T11:20:25.362923Z","iopub.execute_input":"2021-11-16T11:20:25.363264Z","iopub.status.idle":"2021-11-16T11:20:25.803969Z","shell.execute_reply.started":"2021-11-16T11:20:25.363216Z","shell.execute_reply":"2021-11-16T11:20:25.803020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = keras.utils.to_categorical(y_train)\ny_validation = keras.utils.to_categorical(y_validation)","metadata":{"execution":{"iopub.status.busy":"2021-11-16T11:20:25.805619Z","iopub.execute_input":"2021-11-16T11:20:25.805963Z","iopub.status.idle":"2021-11-16T11:20:25.812806Z","shell.execute_reply.started":"2021-11-16T11:20:25.805918Z","shell.execute_reply":"2021-11-16T11:20:25.811721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Modeling**","metadata":{}},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(32, kernel_size=(3, 3), padding='same', activation='relu', input_shape=(28 ,28, 1)))\nmodel.add(MaxPooling2D(pool_size=(2, 2), padding='same',))\nmodel.add(Conv2D(64, kernel_size=(3, 3), padding='same', activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2), padding='same',))\nmodel.add(Conv2D(128, kernel_size=(3, 3), padding='same', activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2), padding='same',))\nmodel.add(Flatten())\nmodel.add(Dense(256, activation='relu'))\nmodel.add(Dense(10, activation='softmax'))\nmodel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-11-16T11:20:25.814454Z","iopub.execute_input":"2021-11-16T11:20:25.815005Z","iopub.status.idle":"2021-11-16T11:20:25.925113Z","shell.execute_reply.started":"2021-11-16T11:20:25.814950Z","shell.execute_reply":"2021-11-16T11:20:25.923971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping","metadata":{"execution":{"iopub.status.busy":"2021-11-16T11:20:25.926551Z","iopub.execute_input":"2021-11-16T11:20:25.927439Z","iopub.status.idle":"2021-11-16T11:20:25.931916Z","shell.execute_reply.started":"2021-11-16T11:20:25.927393Z","shell.execute_reply":"2021-11-16T11:20:25.931226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stopping = EarlyStopping(min_delta=0.001, patience=5, restore_best_weights=True)","metadata":{"execution":{"iopub.status.busy":"2021-11-16T11:20:25.933123Z","iopub.execute_input":"2021-11-16T11:20:25.933788Z","iopub.status.idle":"2021-11-16T11:20:25.943720Z","shell.execute_reply.started":"2021-11-16T11:20:25.933751Z","shell.execute_reply":"2021-11-16T11:20:25.942915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    x=X_train, \n    y=y_train, \n    batch_size=32, \n    epochs=20, \n    callbacks=[early_stopping], \n    validation_data=(X_validation, y_validation))","metadata":{"execution":{"iopub.status.busy":"2021-11-16T11:20:25.946861Z","iopub.execute_input":"2021-11-16T11:20:25.947367Z","iopub.status.idle":"2021-11-16T11:24:35.245548Z","shell.execute_reply.started":"2021-11-16T11:20:25.947236Z","shell.execute_reply":"2021-11-16T11:24:35.244870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = (max(history.history['val_accuracy']))\nprint(score)","metadata":{"execution":{"iopub.status.busy":"2021-11-16T11:24:35.247280Z","iopub.execute_input":"2021-11-16T11:24:35.247784Z","iopub.status.idle":"2021-11-16T11:24:35.253369Z","shell.execute_reply.started":"2021-11-16T11:24:35.247736Z","shell.execute_reply":"2021-11-16T11:24:35.252514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Submission**","metadata":{}},{"cell_type":"code","source":"sample_submission = pd.read_csv('../input/digit-recognizer/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-11-16T11:24:35.254954Z","iopub.execute_input":"2021-11-16T11:24:35.255424Z","iopub.status.idle":"2021-11-16T11:24:35.291779Z","shell.execute_reply.started":"2021-11-16T11:24:35.255381Z","shell.execute_reply":"2021-11-16T11:24:35.291049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission","metadata":{"execution":{"iopub.status.busy":"2021-11-16T11:24:35.292889Z","iopub.execute_input":"2021-11-16T11:24:35.293452Z","iopub.status.idle":"2021-11-16T11:24:35.306498Z","shell.execute_reply.started":"2021-11-16T11:24:35.293411Z","shell.execute_reply":"2021-11-16T11:24:35.305420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = model.predict(df_test)\ny_test = np.argmax(y_test, axis=1)\ny_test","metadata":{"execution":{"iopub.status.busy":"2021-11-16T11:24:57.081716Z","iopub.execute_input":"2021-11-16T11:24:57.082105Z","iopub.status.idle":"2021-11-16T11:25:03.725121Z","shell.execute_reply.started":"2021-11-16T11:24:57.082069Z","shell.execute_reply":"2021-11-16T11:25:03.724109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame(columns=['ImageId', 'Label'])","metadata":{"execution":{"iopub.status.busy":"2021-11-16T11:25:03.727255Z","iopub.execute_input":"2021-11-16T11:25:03.727919Z","iopub.status.idle":"2021-11-16T11:25:03.734918Z","shell.execute_reply.started":"2021-11-16T11:25:03.727877Z","shell.execute_reply":"2021-11-16T11:25:03.734042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['ImageId'] = range(1, 1+len(df_test))\nsubmission['Label'] = y_test","metadata":{"execution":{"iopub.status.busy":"2021-11-16T11:25:03.736154Z","iopub.execute_input":"2021-11-16T11:25:03.736395Z","iopub.status.idle":"2021-11-16T11:25:03.754688Z","shell.execute_reply.started":"2021-11-16T11:25:03.736364Z","shell.execute_reply":"2021-11-16T11:25:03.753507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2021-11-16T11:25:03.756937Z","iopub.execute_input":"2021-11-16T11:25:03.757262Z","iopub.status.idle":"2021-11-16T11:25:03.769491Z","shell.execute_reply.started":"2021-11-16T11:25:03.757220Z","shell.execute_reply":"2021-11-16T11:25:03.768790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submissions0.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-11-16T11:25:04.031928Z","iopub.execute_input":"2021-11-16T11:25:04.032799Z","iopub.status.idle":"2021-11-16T11:25:04.092657Z","shell.execute_reply.started":"2021-11-16T11:25:04.032756Z","shell.execute_reply":"2021-11-16T11:25:04.091676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}