{"metadata":{"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.10.12"},"papermill":{"default_parameters":{},"duration":602.245818,"end_time":"2023-11-13T17:22:40.592477","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2023-11-13T17:12:38.346659","version":"2.4.0"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom sklearn import preprocessing \nfrom sklearn import metrics\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, array_to_img\nfrom tensorflow.keras import datasets, layers, models\nimport numpy as np\nimport pandas as pd\n","metadata":{"execution":{"iopub.execute_input":"2023-11-13T17:12:43.011749Z","iopub.status.busy":"2023-11-13T17:12:43.010650Z","iopub.status.idle":"2023-11-13T17:12:54.400313Z","shell.execute_reply":"2023-11-13T17:12:54.399359Z"},"papermill":{"duration":11.397932,"end_time":"2023-11-13T17:12:54.402998","exception":false,"start_time":"2023-11-13T17:12:43.005066","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_file = \"../input/digit-recognizer/train.csv\"\ntest_file = \"../input/digit-recognizer/test.csv\"\n\ntrain_df = pd.read_csv(train_file)\ntesting_df = pd.read_csv(test_file)\n\ntrain_df.head(10)","metadata":{"execution":{"iopub.execute_input":"2023-11-13T17:12:54.419487Z","iopub.status.busy":"2023-11-13T17:12:54.418235Z","iopub.status.idle":"2023-11-13T17:12:58.476867Z","shell.execute_reply":"2023-11-13T17:12:58.475131Z"},"papermill":{"duration":4.068667,"end_time":"2023-11-13T17:12:58.478965","exception":false,"start_time":"2023-11-13T17:12:54.410298","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def getdata(dataframe, split_ratio):\n    train_df, test_df = train_test_split(dataframe, train_size = split_ratio, shuffle = True, random_state = 10)\n    return test_df, train_df\n\ndef convert_to_matrix(dataframe):\n    images = dataframe.iloc[:, 1:]\n    labels = dataframe[\"label\"]\n    \n    images = np.reshape(images.values, (images.shape[0], 28, 28))\n    labels = np.array(labels).astype(np.float64)\n    images = np.array(images).astype(np.float64)\n    return images, labels\n\ndef convert_to_matrix_test(dataframe):\n    images = dataframe.iloc[:, :]\n   \n    \n    images = np.reshape(images.values, (images.shape[0], 28, 28))\n    images = np.array(images).astype(np.float64)\n    return images","metadata":{"execution":{"iopub.execute_input":"2023-11-13T17:12:58.490070Z","iopub.status.busy":"2023-11-13T17:12:58.489614Z","iopub.status.idle":"2023-11-13T17:12:58.497233Z","shell.execute_reply":"2023-11-13T17:12:58.496000Z"},"papermill":{"duration":0.015632,"end_time":"2023-11-13T17:12:58.499456","exception":false,"start_time":"2023-11-13T17:12:58.483824","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_size = 0.8\ntraining_df, test_df = getdata(train_df, training_size)\ntrain_data, train_labels = convert_to_matrix(training_df)\ntest_data, test_labels = convert_to_matrix(test_df)\n\ndef plot_categories(training_images, training_labels):\n  fig, axes = plt.subplots(1, 10, figsize=(16, 15))\n  axes = axes.flatten()\n  numbers = [x for x in range(10)]\n\n  for k in range(10):\n    img = training_images[k]\n    img = np.expand_dims(img, axis=-1)\n    img = array_to_img(img)\n    ax = axes[k]\n    ax.imshow(img, cmap=\"Greys_r\")\n    ax.set_title(f\"{numbers[int(training_labels[k])]}\")\n    ax.set_axis_off()\n\n  plt.tight_layout()\n  plt.show()\n\nplot_categories(train_data, train_labels)","metadata":{"execution":{"iopub.execute_input":"2023-11-13T17:12:58.510467Z","iopub.status.busy":"2023-11-13T17:12:58.510105Z","iopub.status.idle":"2023-11-13T17:12:59.432983Z","shell.execute_reply":"2023-11-13T17:12:59.431817Z"},"papermill":{"duration":0.931225,"end_time":"2023-11-13T17:12:59.435527","exception":false,"start_time":"2023-11-13T17:12:58.504302","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = np.expand_dims(train_data,axis=3)\ntest_data = np.expand_dims(test_data,axis=3)\n\n\ntest_x = convert_to_matrix_test(testing_df)\ntest_x = np.expand_dims(test_x, axis=3)\n ","metadata":{"execution":{"iopub.execute_input":"2023-11-13T17:12:59.449461Z","iopub.status.busy":"2023-11-13T17:12:59.449089Z","iopub.status.idle":"2023-11-13T17:12:59.514405Z","shell.execute_reply":"2023-11-13T17:12:59.512706Z"},"papermill":{"duration":0.07576,"end_time":"2023-11-13T17:12:59.517137","exception":false,"start_time":"2023-11-13T17:12:59.441377","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x = train_data\ntrain_y = train_labels\nval_x = test_data\nval_y = test_labels","metadata":{"execution":{"iopub.execute_input":"2023-11-13T17:12:59.528695Z","iopub.status.busy":"2023-11-13T17:12:59.528318Z","iopub.status.idle":"2023-11-13T17:12:59.534267Z","shell.execute_reply":"2023-11-13T17:12:59.533014Z"},"papermill":{"duration":0.014752,"end_time":"2023-11-13T17:12:59.537048","exception":false,"start_time":"2023-11-13T17:12:59.522296","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(np.shape(train_data))","metadata":{"execution":{"iopub.execute_input":"2023-11-13T17:12:59.549193Z","iopub.status.busy":"2023-11-13T17:12:59.548815Z","iopub.status.idle":"2023-11-13T17:12:59.555422Z","shell.execute_reply":"2023-11-13T17:12:59.553677Z"},"papermill":{"duration":0.015213,"end_time":"2023-11-13T17:12:59.557478","exception":false,"start_time":"2023-11-13T17:12:59.542265","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.Sequential()\nmodel.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28,1)))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(64, activation='relu'))\nmodel.add(layers.Dropout(0.3))\nmodel.add(layers.Dense(10))\n\nmodel.summary()","metadata":{"execution":{"iopub.execute_input":"2023-11-13T17:12:59.569813Z","iopub.status.busy":"2023-11-13T17:12:59.569359Z","iopub.status.idle":"2023-11-13T17:12:59.823060Z","shell.execute_reply":"2023-11-13T17:12:59.821756Z"},"papermill":{"duration":0.262448,"end_time":"2023-11-13T17:12:59.825296","exception":false,"start_time":"2023-11-13T17:12:59.562848","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate = 0.00005, beta_1 = 0.8),\n              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n              metrics=['accuracy'])\n\nhistory = model.fit(train_x, train_y, epochs=50, \n                    validation_data=(val_x, val_y))","metadata":{"execution":{"iopub.execute_input":"2023-11-13T17:12:59.840653Z","iopub.status.busy":"2023-11-13T17:12:59.840252Z","iopub.status.idle":"2023-11-13T17:22:23.088461Z","shell.execute_reply":"2023-11-13T17:22:23.086737Z"},"papermill":{"duration":563.259359,"end_time":"2023-11-13T17:22:23.091635","exception":false,"start_time":"2023-11-13T17:12:59.832276","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs = range(len(acc))\nplt.xlim(0, 50)\nplt.ylim(0.6, 1)\nplt.plot(epochs, acc, 'r', label='Training accuracy')\nplt.plot(epochs, val_acc, 'b', label='Validation accuracy')\nplt.title('Training and validation accuracy')\nplt.legend()\nplt.figure()\n\nplt.xlim(0, 50)\nplt.ylim(0, 1)\nplt.plot(epochs, loss, 'r', label='Training Loss')\nplt.plot(epochs, val_loss, 'b', label='Validation Loss')\nplt.title('Training and validation loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2023-11-13T17:22:23.543332Z","iopub.status.busy":"2023-11-13T17:22:23.542993Z","iopub.status.idle":"2023-11-13T17:22:23.962695Z","shell.execute_reply":"2023-11-13T17:22:23.961856Z"},"papermill":{"duration":0.647088,"end_time":"2023-11-13T17:22:23.964509","exception":false,"start_time":"2023-11-13T17:22:23.317421","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testY = model.predict(test_x, verbose=2)\n\nprint(testY)","metadata":{"execution":{"iopub.execute_input":"2023-11-13T17:22:24.429167Z","iopub.status.busy":"2023-11-13T17:22:24.428784Z","iopub.status.idle":"2023-11-13T17:22:29.732524Z","shell.execute_reply":"2023-11-13T17:22:29.731297Z"},"papermill":{"duration":5.545703,"end_time":"2023-11-13T17:22:29.734823","exception":false,"start_time":"2023-11-13T17:22:24.189120","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(np.shape(test_x))","metadata":{"execution":{"iopub.execute_input":"2023-11-13T17:22:30.191117Z","iopub.status.busy":"2023-11-13T17:22:30.190756Z","iopub.status.idle":"2023-11-13T17:22:30.197657Z","shell.execute_reply":"2023-11-13T17:22:30.195980Z"},"papermill":{"duration":0.235879,"end_time":"2023-11-13T17:22:30.199598","exception":false,"start_time":"2023-11-13T17:22:29.963719","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(testY[0])\n#print(test_x[1])\n\ndef visualize_input(img, ax):\n    ax.imshow(img, cmap='gray')\n    width, height = img.shape\n    thresh = img.max()/2.5\n    for x in range(width):\n        for y in range(height):\n            ax.annotate(str(round(img[x][y],2)), xy=(y,x),\n                        horizontalalignment='center',\n                        verticalalignment='center',\n                        color='white' if img[x][y]<thresh else 'black')\n\nfig = plt.figure(figsize = (12,12)) \nax = fig.add_subplot(111)\nvisualize_input(test_x[10].reshape(28,28), ax)\n","metadata":{"execution":{"iopub.execute_input":"2023-11-13T17:22:30.656327Z","iopub.status.busy":"2023-11-13T17:22:30.655945Z","iopub.status.idle":"2023-11-13T17:22:33.394675Z","shell.execute_reply":"2023-11-13T17:22:33.393549Z"},"papermill":{"duration":2.971004,"end_time":"2023-11-13T17:22:33.396518","exception":false,"start_time":"2023-11-13T17:22:30.425514","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_num = 4\n\ntestY_list = testY.tolist()\nprint(testY_list[test_num].index(max(testY_list[test_num])))\n\ndef visualize_input(img, ax):\n    ax.imshow(img, cmap='gray')\n    width, height = img.shape\n    thresh = img.max()/2.5\n    for x in range(width):\n        for y in range(height):\n            ax.annotate(str(round(img[x][y],2)), xy=(y,x),\n                        horizontalalignment='center',\n                        verticalalignment='center',\n                        color='white' if img[x][y]<thresh else 'black')\n\nfig = plt.figure(figsize = (12,12)) \nax = fig.add_subplot(111)\nvisualize_input(test_x[test_num].reshape(28,28), ax)\n","metadata":{"execution":{"iopub.execute_input":"2023-11-13T17:22:33.920032Z","iopub.status.busy":"2023-11-13T17:22:33.918598Z","iopub.status.idle":"2023-11-13T17:22:36.642423Z","shell.execute_reply":"2023-11-13T17:22:36.640995Z"},"papermill":{"duration":2.954835,"end_time":"2023-11-13T17:22:36.645061","exception":false,"start_time":"2023-11-13T17:22:33.690226","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgId = []\npredLabels = []\n\nfor i in range(len(testY)):\n    predLabels.append(testY_list[i].index(max(testY_list[i])))\n    imgId.append(i+1)\n\n\nsubmission_dict = {'ImageId' : imgId, 'Label': predLabels}\nsubmission_df = pd.DataFrame(submission_dict)\nsubmission_df.head()","metadata":{"execution":{"iopub.execute_input":"2023-11-13T17:22:37.109280Z","iopub.status.busy":"2023-11-13T17:22:37.108780Z","iopub.status.idle":"2023-11-13T17:22:37.153388Z","shell.execute_reply":"2023-11-13T17:22:37.151303Z"},"papermill":{"duration":0.279729,"end_time":"2023-11-13T17:22:37.156227","exception":false,"start_time":"2023-11-13T17:22:36.876498","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.execute_input":"2023-11-13T17:22:37.619906Z","iopub.status.busy":"2023-11-13T17:22:37.619142Z","iopub.status.idle":"2023-11-13T17:22:37.654074Z","shell.execute_reply":"2023-11-13T17:22:37.652935Z"},"papermill":{"duration":0.270001,"end_time":"2023-11-13T17:22:37.656147","exception":false,"start_time":"2023-11-13T17:22:37.386146","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}