{"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\n%matplotlib inline\nimport tensorflow as tf\nimport warnings\nwarnings.filterwarnings('ignore')\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-08-01T09:12:56.293717Z","iopub.execute_input":"2023-08-01T09:12:56.294608Z","iopub.status.idle":"2023-08-01T09:12:56.306717Z","shell.execute_reply.started":"2023-08-01T09:12:56.294570Z","shell.execute_reply":"2023-08-01T09:12:56.305527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv(r'/kaggle/input/digit-recognizer/train.csv')\ntest_data = pd.read_csv(r'/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2023-08-01T09:13:36.707454Z","iopub.execute_input":"2023-08-01T09:13:36.707920Z","iopub.status.idle":"2023-08-01T09:13:43.385794Z","shell.execute_reply.started":"2023-08-01T09:13:36.707885Z","shell.execute_reply":"2023-08-01T09:13:43.384686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data","metadata":{"execution":{"iopub.status.busy":"2023-08-01T09:14:02.429220Z","iopub.execute_input":"2023-08-01T09:14:02.429670Z","iopub.status.idle":"2023-08-01T09:14:02.469456Z","shell.execute_reply.started":"2023-08-01T09:14:02.429634Z","shell.execute_reply":"2023-08-01T09:14:02.468269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = train_data['label']\nx_train = train_data.drop(columns=['label']).values/255.\nx_test = test_data.values/255.","metadata":{"execution":{"iopub.status.busy":"2023-08-01T09:15:26.746334Z","iopub.execute_input":"2023-08-01T09:15:26.747132Z","iopub.status.idle":"2023-08-01T09:15:27.102677Z","shell.execute_reply.started":"2023-08-01T09:15:26.747067Z","shell.execute_reply":"2023-08-01T09:15:27.101399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(2,5, figsize = (12,5))\naxes = axes.flatten()\nidx = np.random.randint(0, 42000, size=10)\nfor i in range(10):\n    axes[i].imshow(x_train[idx[i], :].reshape(28,28), cmap = 'gray')\n    axes[i].axis('off')\n    axes[i].set_title(str(int(y_train[idx[i]])),color='black', fontsize=25)\n    \nplt.show()\n    ","metadata":{"execution":{"iopub.status.busy":"2023-08-01T09:19:34.194410Z","iopub.execute_input":"2023-08-01T09:19:34.194860Z","iopub.status.idle":"2023-08-01T09:19:34.935285Z","shell.execute_reply.started":"2023-08-01T09:19:34.194826Z","shell.execute_reply":"2023-08-01T09:19:34.934058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train.reshape(-1,28,28,1)\n\nx_test = x_test.reshape(-1,28,28,1)\n\ny_train = tf.keras.utils.to_categorical(y_train,num_classes=10)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-01T09:42:01.113332Z","iopub.execute_input":"2023-08-01T09:42:01.113857Z","iopub.status.idle":"2023-08-01T09:42:01.122974Z","shell.execute_reply.started":"2023-08-01T09:42:01.113809Z","shell.execute_reply":"2023-08-01T09:42:01.121787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Simple DNN","metadata":{}},{"cell_type":"code","source":"model = tf.keras.Sequential([\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(256,'relu'),\n    tf.keras.layers.Dense(128,'relu'),\n    tf.keras.layers.Dense(10,'softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2023-08-01T09:44:38.140808Z","iopub.execute_input":"2023-08-01T09:44:38.141250Z","iopub.status.idle":"2023-08-01T09:44:38.868523Z","shell.execute_reply.started":"2023-08-01T09:44:38.141214Z","shell.execute_reply":"2023-08-01T09:44:38.867457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    optimizer='adam',\n    loss = 'categorical_crossentropy',\n    metrics='accuracy'\n)","metadata":{"execution":{"iopub.status.busy":"2023-08-01T09:45:17.307976Z","iopub.execute_input":"2023-08-01T09:45:17.308425Z","iopub.status.idle":"2023-08-01T09:45:17.339693Z","shell.execute_reply.started":"2023-08-01T09:45:17.308392Z","shell.execute_reply":"2023-08-01T09:45:17.338780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(x_train, y_train, epochs=15)","metadata":{"execution":{"iopub.status.busy":"2023-08-01T09:45:41.660159Z","iopub.execute_input":"2023-08-01T09:45:41.660574Z","iopub.status.idle":"2023-08-01T09:46:58.857668Z","shell.execute_reply.started":"2023-08-01T09:45:41.660544Z","shell.execute_reply":"2023-08-01T09:46:58.856694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['accuracy']\nloss = history.history['loss']\nepochs = range(len(acc))\nplt.plot(epochs, acc, 'b', label='Training accuracy')\nplt.title('Training accuracy')\n\nplt.figure()\n\nplt.plot(epochs, loss, 'b', label='Training Loss')\nplt.title('Training loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-01T10:42:26.485555Z","iopub.execute_input":"2023-08-01T10:42:26.486006Z","iopub.status.idle":"2023-08-01T10:42:27.097214Z","shell.execute_reply.started":"2023-08-01T10:42:26.485975Z","shell.execute_reply":"2023-08-01T10:42:27.096026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DNN_pred = np.argmax(model.predict(x_test), axis=1)\nDNN_pred","metadata":{"execution":{"iopub.status.busy":"2023-08-01T09:47:28.596623Z","iopub.execute_input":"2023-08-01T09:47:28.597033Z","iopub.status.idle":"2023-08-01T09:47:30.708056Z","shell.execute_reply.started":"2023-08-01T09:47:28.597003Z","shell.execute_reply":"2023-08-01T09:47:30.706932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = pd.read_csv(r\"/kaggle/input/digit-recognizer/sample_submission.csv\")\nsample_submission[\"Label\"] = DNN_pred\nsample_submission","metadata":{"execution":{"iopub.status.busy":"2023-08-01T09:47:48.993290Z","iopub.execute_input":"2023-08-01T09:47:48.993693Z","iopub.status.idle":"2023-08-01T09:47:49.015913Z","shell.execute_reply.started":"2023-08-01T09:47:48.993658Z","shell.execute_reply":"2023-08-01T09:47:49.014661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.to_csv(r'/kaggle/working/submission_DNN.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-08-01T09:51:01.567285Z","iopub.execute_input":"2023-08-01T09:51:01.568245Z","iopub.status.idle":"2023-08-01T09:51:01.659904Z","shell.execute_reply.started":"2023-08-01T09:51:01.568207Z","shell.execute_reply":"2023-08-01T09:51:01.658808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Get 0.977 accuracy now wanna try CNN","metadata":{}},{"cell_type":"code","source":"model_cnn = tf.keras.Sequential([\n    tf.keras.layers.Conv2D(32,(3,3),activation='relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Conv2D(64,(3,3),activation='relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(256,'relu'),\n    tf.keras.layers.Dense(128,'relu'),\n    tf.keras.layers.Dense(10, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2023-08-01T10:33:07.779606Z","iopub.execute_input":"2023-08-01T10:33:07.780023Z","iopub.status.idle":"2023-08-01T10:33:07.807939Z","shell.execute_reply.started":"2023-08-01T10:33:07.779993Z","shell.execute_reply":"2023-08-01T10:33:07.806746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_cnn.compile(\n    optimizer = 'adam',\n    loss = 'categorical_crossentropy',\n    metrics = ['accuracy']\n)","metadata":{"execution":{"iopub.status.busy":"2023-08-01T10:34:05.987952Z","iopub.execute_input":"2023-08-01T10:34:05.988913Z","iopub.status.idle":"2023-08-01T10:34:06.003050Z","shell.execute_reply.started":"2023-08-01T10:34:05.988866Z","shell.execute_reply":"2023-08-01T10:34:06.001842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_cnn = model_cnn.fit(x_train,y_train, epochs=10)","metadata":{"execution":{"iopub.status.busy":"2023-08-01T10:42:33.762517Z","iopub.execute_input":"2023-08-01T10:42:33.762944Z","iopub.status.idle":"2023-08-01T10:47:56.004304Z","shell.execute_reply.started":"2023-08-01T10:42:33.762912Z","shell.execute_reply":"2023-08-01T10:47:56.003144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history_cnn.history['accuracy']\nloss = history_cnn.history['loss']\nepochs = range(len(acc))\nplt.plot(epochs, acc, 'b', label='Training accuracy')\nplt.title('Training accuracy')\n\nplt.figure()\n\nplt.plot(epochs, loss, 'b', label='Training Loss')\nplt.title('Training loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-01T10:47:56.006129Z","iopub.execute_input":"2023-08-01T10:47:56.006493Z","iopub.status.idle":"2023-08-01T10:47:56.606014Z","shell.execute_reply.started":"2023-08-01T10:47:56.006450Z","shell.execute_reply":"2023-08-01T10:47:56.604860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_cnn.summary()","metadata":{"execution":{"iopub.status.busy":"2023-08-01T10:48:10.446270Z","iopub.execute_input":"2023-08-01T10:48:10.446702Z","iopub.status.idle":"2023-08-01T10:48:10.481041Z","shell.execute_reply.started":"2023-08-01T10:48:10.446670Z","shell.execute_reply":"2023-08-01T10:48:10.479853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_pred = np.argmax(model_cnn.predict(x_test),axis=1)\n\nprint(cnn_pred)","metadata":{"execution":{"iopub.status.busy":"2023-08-01T10:49:59.396474Z","iopub.execute_input":"2023-08-01T10:49:59.396886Z","iopub.status.idle":"2023-08-01T10:50:05.673368Z","shell.execute_reply.started":"2023-08-01T10:49:59.396855Z","shell.execute_reply":"2023-08-01T10:50:05.672128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = pd.read_csv(r\"/kaggle/input/digit-recognizer/sample_submission.csv\")\nsample_submission[\"Label\"] = cnn_pred\nsample_submission.to_csv(\"submission_CNN.csv\", index=False)\nsample_submission","metadata":{"execution":{"iopub.status.busy":"2023-08-01T10:50:05.675576Z","iopub.execute_input":"2023-08-01T10:50:05.678136Z","iopub.status.idle":"2023-08-01T10:50:05.767983Z","shell.execute_reply.started":"2023-08-01T10:50:05.678069Z","shell.execute_reply":"2023-08-01T10:50:05.766813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}