{"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 matplotlib.pyplot as plt\nimport tensorflow as tf\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-08-20T06:48:44.987346Z","iopub.execute_input":"2021-08-20T06:48:44.987727Z","iopub.status.idle":"2021-08-20T06:48:45.000442Z","shell.execute_reply.started":"2021-08-20T06:48:44.987698Z","shell.execute_reply":"2021-08-20T06:48:44.998788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ndf_t = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-08-20T06:48:45.183504Z","iopub.execute_input":"2021-08-20T06:48:45.183861Z","iopub.status.idle":"2021-08-20T06:48:49.878809Z","shell.execute_reply.started":"2021-08-20T06:48:45.183831Z","shell.execute_reply":"2021-08-20T06:48:49.878027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_t.head(5)","metadata":{"execution":{"iopub.status.busy":"2021-08-20T06:48:49.879949Z","iopub.execute_input":"2021-08-20T06:48:49.880363Z","iopub.status.idle":"2021-08-20T06:48:49.901303Z","shell.execute_reply.started":"2021-08-20T06:48:49.880333Z","shell.execute_reply":"2021-08-20T06:48:49.900529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y = df[\"label\"]\nX = df.drop(\"label\",axis = 1)\nX = X.values.reshape(-1,28,28)/255\nX_t = df_t.values.reshape(-1,28,28)/255","metadata":{"execution":{"iopub.status.busy":"2021-08-20T06:48:49.902943Z","iopub.execute_input":"2021-08-20T06:48:49.903536Z","iopub.status.idle":"2021-08-20T06:48:50.192794Z","shell.execute_reply.started":"2021-08-20T06:48:49.903473Z","shell.execute_reply":"2021-08-20T06:48:50.191955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X[1])\nplt.colorbar()\nplt.grid(False)","metadata":{"execution":{"iopub.status.busy":"2021-08-20T06:48:55.384302Z","iopub.execute_input":"2021-08-20T06:48:55.384811Z","iopub.status.idle":"2021-08-20T06:48:55.62699Z","shell.execute_reply.started":"2021-08-20T06:48:55.384778Z","shell.execute_reply":"2021-08-20T06:48:55.625678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train,X_test,y_train,y_test = train_test_split(X,Y,test_size = 0.3)","metadata":{"execution":{"iopub.status.busy":"2021-08-20T06:48:59.413471Z","iopub.execute_input":"2021-08-20T06:48:59.413887Z","iopub.status.idle":"2021-08-20T06:48:59.835982Z","shell.execute_reply.started":"2021-08-20T06:48:59.413857Z","shell.execute_reply":"2021-08-20T06:48:59.83459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#let's plot some digit\nplt.figure(figsize=(10,10))\nfor i in range(25):\n    plt.subplot(5,5,i+1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.grid(False)\n    plt.imshow(X[i], cmap=plt.cm.binary)\n    plt.xlabel(Y[i])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-20T06:48:59.837682Z","iopub.execute_input":"2021-08-20T06:48:59.838016Z","iopub.status.idle":"2021-08-20T06:49:01.388077Z","shell.execute_reply.started":"2021-08-20T06:48:59.837987Z","shell.execute_reply":"2021-08-20T06:49:01.387107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.Sequential([\n    tf.keras.layers.Flatten(input_shape=(28, 28)),\n    tf.keras.layers.Dense(128, activation='relu'),\n    tf.keras.layers.Dense(128, activation='relu'),\n    tf.keras.layers.Dense(10)\n])","metadata":{"execution":{"iopub.status.busy":"2021-08-20T06:49:01.389851Z","iopub.execute_input":"2021-08-20T06:49:01.390148Z","iopub.status.idle":"2021-08-20T06:49:01.429173Z","shell.execute_reply.started":"2021-08-20T06:49:01.39012Z","shell.execute_reply":"2021-08-20T06:49:01.428131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-08-20T06:49:01.430897Z","iopub.execute_input":"2021-08-20T06:49:01.431315Z","iopub.status.idle":"2021-08-20T06:49:01.445513Z","shell.execute_reply.started":"2021-08-20T06:49:01.431272Z","shell.execute_reply":"2021-08-20T06:49:01.44465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train, y_train, epochs=20)\n","metadata":{"execution":{"iopub.status.busy":"2021-08-20T06:49:02.772155Z","iopub.execute_input":"2021-08-20T06:49:02.772701Z","iopub.status.idle":"2021-08-20T06:49:42.467538Z","shell.execute_reply.started":"2021-08-20T06:49:02.772657Z","shell.execute_reply":"2021-08-20T06:49:42.466386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss, test_acc = model.evaluate(X_test,  y_test, verbose=2)\nprint('\\nTest accuracy:', test_acc)","metadata":{"execution":{"iopub.status.busy":"2021-08-20T06:49:45.14664Z","iopub.execute_input":"2021-08-20T06:49:45.147149Z","iopub.status.idle":"2021-08-20T06:49:46.069782Z","shell.execute_reply.started":"2021-08-20T06:49:45.147104Z","shell.execute_reply":"2021-08-20T06:49:46.068589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_image(i, predictions_array, true_label, img):\n    true_label = true_label[i]\n    img        = img[i]\n    plt.grid(False)\n    plt.xticks([])\n    plt.yticks([])\n    plt.imshow(img, cmap=plt.cm.binary)\n\n    predicted_label = np.argmax(predictions_array)\n    if predicted_label == true_label:\n        color = 'blue'\n    else:\n        color = 'red'\n\n    plt.xlabel(\"{} {:2.0f}% ({})\".format(predicted_label,\n                                100*np.max(predictions_array),\n                                true_label),\n                                color=color)\n\ndef plot_value_array(i, predictions_array, true_label):\n    true_label = true_label[i]\n    plt.grid(False)\n    plt.xticks(range(10))\n    plt.yticks([])\n    thisplot = plt.bar(range(10), predictions_array, color=\"#777777\")\n    plt.ylim([0, 1])\n    predicted_label = np.argmax(predictions_array)\n\n    thisplot[predicted_label].set_color('red')\n    thisplot[true_label].set_color('blue')","metadata":{"execution":{"iopub.status.busy":"2021-08-20T06:49:47.031863Z","iopub.execute_input":"2021-08-20T06:49:47.032426Z","iopub.status.idle":"2021-08-20T06:49:47.043419Z","shell.execute_reply.started":"2021-08-20T06:49:47.032388Z","shell.execute_reply":"2021-08-20T06:49:47.042295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"probability_model = tf.keras.Sequential([model, \n                                         tf.keras.layers.Softmax()])\n\npredictions = probability_model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2021-08-20T06:49:47.704341Z","iopub.execute_input":"2021-08-20T06:49:47.705238Z","iopub.status.idle":"2021-08-20T06:49:48.395584Z","shell.execute_reply.started":"2021-08-20T06:49:47.705139Z","shell.execute_reply":"2021-08-20T06:49:48.394371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions[0]","metadata":{"execution":{"iopub.status.busy":"2021-08-20T06:49:48.511234Z","iopub.execute_input":"2021-08-20T06:49:48.511606Z","iopub.status.idle":"2021-08-20T06:49:48.517397Z","shell.execute_reply.started":"2021-08-20T06:49:48.511568Z","shell.execute_reply":"2021-08-20T06:49:48.516657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_rows = 5\nnum_cols = 3\nnum_images = num_rows*num_cols\nplt.figure(figsize=(2*2*num_cols, 2*num_rows))\nfor i in range(num_images):\n    plt.subplot(num_rows, 2*num_cols, 2*i+1)\n    plot_image(i, predictions[i], list(y_test), X_test)\n    plt.subplot(num_rows, 2*num_cols, 2*i+2)\n    plot_value_array(i, predictions[i], list(y_test))\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-20T06:49:57.953357Z","iopub.execute_input":"2021-08-20T06:49:57.95416Z","iopub.status.idle":"2021-08-20T06:50:01.003867Z","shell.execute_reply.started":"2021-08-20T06:49:57.954109Z","shell.execute_reply":"2021-08-20T06:50:01.002851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_sub = probability_model.predict(X_t)\npred_sub = [np.argmax(i) for i in pred_sub]\n","metadata":{"execution":{"iopub.status.busy":"2021-08-20T06:53:13.641833Z","iopub.execute_input":"2021-08-20T06:53:13.642332Z","iopub.status.idle":"2021-08-20T06:53:15.071782Z","shell.execute_reply.started":"2021-08-20T06:53:13.642281Z","shell.execute_reply":"2021-08-20T06:53:15.071021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"d = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")\nd[\"Label\"] = pred_sub\nd.to_csv(\"submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-08-20T06:54:20.096835Z","iopub.execute_input":"2021-08-20T06:54:20.097181Z","iopub.status.idle":"2021-08-20T06:54:20.204949Z","shell.execute_reply.started":"2021-08-20T06:54:20.097152Z","shell.execute_reply":"2021-08-20T06:54:20.203512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"d","metadata":{"execution":{"iopub.status.busy":"2021-08-20T07:03:06.978659Z","iopub.execute_input":"2021-08-20T07:03:06.979136Z","iopub.status.idle":"2021-08-20T07:03:06.992895Z","shell.execute_reply.started":"2021-08-20T07:03:06.979097Z","shell.execute_reply":"2021-08-20T07:03:06.991683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}