{"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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2021-10-15T16:14:20.810134Z","iopub.execute_input":"2021-10-15T16:14:20.810384Z","iopub.status.idle":"2021-10-15T16:14:25.620955Z","shell.execute_reply.started":"2021-10-15T16:14:20.810320Z","shell.execute_reply":"2021-10-15T16:14:25.620196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(\"../input/digit-recognizer/test.csv\")\ntrain_df = pd.read_csv(\"../input/digit-recognizer/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-10-15T16:14:29.698901Z","iopub.execute_input":"2021-10-15T16:14:29.699227Z","iopub.status.idle":"2021-10-15T16:14:34.671284Z","shell.execute_reply.started":"2021-10-15T16:14:29.699177Z","shell.execute_reply":"2021-10-15T16:14:34.670502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.info()","metadata":{"execution":{"iopub.status.busy":"2021-10-15T16:14:41.059139Z","iopub.execute_input":"2021-10-15T16:14:41.059417Z","iopub.status.idle":"2021-10-15T16:14:41.110237Z","shell.execute_reply.started":"2021-10-15T16:14:41.059390Z","shell.execute_reply":"2021-10-15T16:14:41.109519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2021-10-15T16:14:45.117939Z","iopub.execute_input":"2021-10-15T16:14:45.118646Z","iopub.status.idle":"2021-10-15T16:14:45.163355Z","shell.execute_reply.started":"2021-10-15T16:14:45.118602Z","shell.execute_reply":"2021-10-15T16:14:45.162688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.shape","metadata":{"execution":{"iopub.status.busy":"2021-10-15T16:15:19.475224Z","iopub.execute_input":"2021-10-15T16:15:19.475501Z","iopub.status.idle":"2021-10-15T16:15:19.483221Z","shell.execute_reply.started":"2021-10-15T16:15:19.475470Z","shell.execute_reply":"2021-10-15T16:15:19.482420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2021-10-15T16:15:29.626641Z","iopub.execute_input":"2021-10-15T16:15:29.627449Z","iopub.status.idle":"2021-10-15T16:15:29.633246Z","shell.execute_reply.started":"2021-10-15T16:15:29.627408Z","shell.execute_reply":"2021-10-15T16:15:29.632367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(18,8))\nsample_data=train_df.sample(18).reset_index(drop=True)\nfor index,row in sample_data.iterrows():\n    label=row['label']\n    image_pix=row.drop('label')\n    plt.subplot(3,6,index+1)\n    plt.imshow(image_pix.values.reshape(28,28))\n    plt.title(label)\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2021-10-15T16:16:19.773402Z","iopub.execute_input":"2021-10-15T16:16:19.773992Z","iopub.status.idle":"2021-10-15T16:16:22.082357Z","shell.execute_reply.started":"2021-10-15T16:16:19.773956Z","shell.execute_reply":"2021-10-15T16:16:22.081544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train_df.drop(columns=['label']).values.reshape(train_df.shape[0],28,28,1)\ny = tf.keras.utils.to_categorical(train_df['label'])\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=101)","metadata":{"execution":{"iopub.status.busy":"2021-10-15T16:17:22.708450Z","iopub.execute_input":"2021-10-15T16:17:22.709073Z","iopub.status.idle":"2021-10-15T16:17:23.172223Z","shell.execute_reply.started":"2021-10-15T16:17:22.709036Z","shell.execute_reply":"2021-10-15T16:17:23.171361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Image Data Generator\ntrain_data_gen = tf.keras.preprocessing.image.ImageDataGenerator(rotation_range=10,rescale=1./255,\n                                                                 shear_range=0.1,zoom_range=0.1,\n                                                                 width_shift_range=0.1,\n                                                                 height_shift_range=0.1)\ntrain_data_gen.fit(X_train)\ntrain_generator = train_data_gen.flow(X_train,y_train,batch_size=32)\ndata_gen_validation = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)\ntrain_data_gen.fit(X_test)\nvalidation_gen = data_gen_validation.flow(X_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2021-10-15T16:22:10.705309Z","iopub.execute_input":"2021-10-15T16:22:10.705610Z","iopub.status.idle":"2021-10-15T16:22:10.889062Z","shell.execute_reply.started":"2021-10-15T16:22:10.705570Z","shell.execute_reply":"2021-10-15T16:22:10.888099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create neural network\nmodel = tf.keras.models.Sequential([\n    # Convolutional layer, using a 3x3 kernel\n    tf.keras.layers.Conv2D(64, (3, 3), activation=\"relu\", input_shape=(28, 28, 1)),\n    tf.keras.layers.Conv2D(32, (3, 3), activation=\"relu\"),\n    # Max-pooling layer, using 2x2 pool size\n    tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),\n    # Flatten units\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(10, activation=\"softmax\")\n])","metadata":{"execution":{"iopub.status.busy":"2021-10-15T16:23:41.791254Z","iopub.execute_input":"2021-10-15T16:23:41.791538Z","iopub.status.idle":"2021-10-15T16:23:43.647661Z","shell.execute_reply.started":"2021-10-15T16:23:41.791508Z","shell.execute_reply":"2021-10-15T16:23:43.646936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='rmsprop',\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-10-15T16:24:25.348525Z","iopub.execute_input":"2021-10-15T16:24:25.349107Z","iopub.status.idle":"2021-10-15T16:24:25.365346Z","shell.execute_reply.started":"2021-10-15T16:24:25.349070Z","shell.execute_reply":"2021-10-15T16:24:25.364658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Callbacks\ncallbacks = [\n    tf.keras.callbacks.EarlyStopping(patience=10, verbose=1),\n    tf.keras.callbacks.ReduceLROnPlateau(factor=0.1, patience=3, min_lr=0.00001, verbose=1),\n    tf.keras.callbacks.ModelCheckpoint('model.h5', verbose=1, save_best_only=True, save_weights_only=True)\n]","metadata":{"execution":{"iopub.status.busy":"2021-10-15T16:26:13.045480Z","iopub.execute_input":"2021-10-15T16:26:13.046044Z","iopub.status.idle":"2021-10-15T16:26:13.053587Z","shell.execute_reply.started":"2021-10-15T16:26:13.046009Z","shell.execute_reply":"2021-10-15T16:26:13.052747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit_generator(\n    train_generator,\n    steps_per_epoch=len(X_train) // 32, \n    validation_data=validation_gen,\n    validation_steps=len(X_test) // 32,\n    epochs=32,\n    callbacks=callbacks\n)","metadata":{"execution":{"iopub.status.busy":"2021-10-15T16:27:34.585514Z","iopub.execute_input":"2021-10-15T16:27:34.585804Z","iopub.status.idle":"2021-10-15T16:33:24.014311Z","shell.execute_reply.started":"2021-10-15T16:27:34.585772Z","shell.execute_reply":"2021-10-15T16:33:24.013548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate neural network performance\nX_test_rescaled = (X_test.astype(\"float32\") / 255)\nmodel.evaluate(X_test_rescaled,  y_test, verbose=2)","metadata":{"execution":{"iopub.status.busy":"2021-10-15T16:33:33.311313Z","iopub.execute_input":"2021-10-15T16:33:33.311613Z","iopub.status.idle":"2021-10-15T16:33:34.367543Z","shell.execute_reply.started":"2021-10-15T16:33:33.311574Z","shell.execute_reply":"2021-10-15T16:33:34.366884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# testing on test_df\ntest_digit_data = test_df.values.reshape(test_df.shape[0],28,28,1).astype(\"float32\") / 255\npredictions = model.predict(test_digit_data)\nresults = np.argmax(predictions, axis = 1) ","metadata":{"execution":{"iopub.status.busy":"2021-10-15T16:35:39.291328Z","iopub.execute_input":"2021-10-15T16:35:39.291648Z","iopub.status.idle":"2021-10-15T16:35:40.346161Z","shell.execute_reply.started":"2021-10-15T16:35:39.291616Z","shell.execute_reply":"2021-10-15T16:35:40.345372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checking the accuracy of model\nplt.figure(figsize=(18, 8))\nsample_test = test_df.head(18)\nfor index, image_pix in sample_test.iterrows():\n    label = results[index]\n    plt.subplot(3,6,index+1)\n    plt.imshow(image_pix.values.reshape(28,28))\n    plt.title(label)\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2021-10-15T16:39:28.341267Z","iopub.execute_input":"2021-10-15T16:39:28.341583Z","iopub.status.idle":"2021-10-15T16:39:30.584741Z","shell.execute_reply.started":"2021-10-15T16:39:28.341534Z","shell.execute_reply":"2021-10-15T16:39:30.584033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submissions = pd.read_csv(\"../input/digit-recognizer/sample_submission.csv\")\nsubmissions['Label'] = results\nsubmissions.to_csv('submission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2021-10-15T16:40:15.047934Z","iopub.execute_input":"2021-10-15T16:40:15.048197Z","iopub.status.idle":"2021-10-15T16:40:15.113610Z","shell.execute_reply.started":"2021-10-15T16:40:15.048170Z","shell.execute_reply":"2021-10-15T16:40:15.112933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}