{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30646,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix, classification_report\nfrom sklearn import metrics\n\nimport tensorflow as tf\nfrom tensorflow.keras import layers, models","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-02-16T16:48:41.152536Z","iopub.execute_input":"2024-02-16T16:48:41.154111Z","iopub.status.idle":"2024-02-16T16:48:41.160744Z","shell.execute_reply.started":"2024-02-16T16:48:41.154069Z","shell.execute_reply":"2024-02-16T16:48:41.159699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-16T16:48:41.162363Z","iopub.execute_input":"2024-02-16T16:48:41.162870Z","iopub.status.idle":"2024-02-16T16:48:45.509509Z","shell.execute_reply.started":"2024-02-16T16:48:41.162841Z","shell.execute_reply":"2024-02-16T16:48:45.508389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train = train[\"label\"]\n\n# Drop 'label' column\nX_train = train.drop(labels = [\"label\"],axis = 1) \n\ncounts = Y_train.value_counts().sort_index()\n\ncolors = np.random.rand(10, 3)\n\nplt.bar(counts.index, counts.values, color = colors)\n\nplt.xlabel('Label')\nplt.ylabel('Count')\nplt.title('Occurrences of digits from 0 to 9 in the label column')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-16T16:48:45.510832Z","iopub.execute_input":"2024-02-16T16:48:45.511197Z","iopub.status.idle":"2024-02-16T16:48:45.806590Z","shell.execute_reply.started":"2024-02-16T16:48:45.511161Z","shell.execute_reply":"2024-02-16T16:48:45.805059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_index = np.random.randint(0, len(train))\nrandom_image = X_train.iloc[random_index].values.reshape(28, 28) \n\nplt.imshow(random_image, cmap='gray')\nplt.title(f\"Label: {Y_train.iloc[random_index]}\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-16T16:48:45.810186Z","iopub.execute_input":"2024-02-16T16:48:45.810674Z","iopub.status.idle":"2024-02-16T16:48:46.013467Z","shell.execute_reply.started":"2024-02-16T16:48:45.810635Z","shell.execute_reply":"2024-02-16T16:48:46.012444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Normalization\nX_train = X_train/255\ntest = test/255","metadata":{"execution":{"iopub.status.busy":"2024-02-16T16:48:46.014841Z","iopub.execute_input":"2024-02-16T16:48:46.016219Z","iopub.status.idle":"2024-02-16T16:48:46.148951Z","shell.execute_reply.started":"2024-02-16T16:48:46.016181Z","shell.execute_reply":"2024-02-16T16:48:46.147926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split the train and the validation set for the fitting\nX_train, X_val, y_train, y_val = train_test_split(X_train, Y_train, test_size=0.2, random_state=42)\n\nprint(\"Training set shape:\", X_train.shape)\nprint(\"Validation set shape:\", X_val.shape)","metadata":{"execution":{"iopub.status.busy":"2024-02-16T16:48:46.150189Z","iopub.execute_input":"2024-02-16T16:48:46.150990Z","iopub.status.idle":"2024-02-16T16:48:46.483639Z","shell.execute_reply.started":"2024-02-16T16:48:46.150956Z","shell.execute_reply":"2024-02-16T16:48:46.482453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Define the model\nmodel = models.Sequential([\n    layers.Flatten(input_shape=(28, 28)),  # Flatten the 28x28 input images\n    layers.Dense(1028, activation='relu'),   \n    layers.Dense(10, activation='softmax') \n])\n\n# Compile the model\nmodel.compile(optimizer='adam', \n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])\n\nmodel.fit(X_train.values.reshape(-1, 28, 28), y_train, epochs=10, validation_data=(X_val.values.reshape(-1, 28, 28), y_val))","metadata":{"execution":{"iopub.status.busy":"2024-02-16T16:48:46.485575Z","iopub.execute_input":"2024-02-16T16:48:46.486062Z","iopub.status.idle":"2024-02-16T16:50:40.631047Z","shell.execute_reply.started":"2024-02-16T16:48:46.486022Z","shell.execute_reply":"2024-02-16T16:50:40.629822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate the model on the validation set\nvalidation_loss, validation_accuracy = model.evaluate(X_val.values.reshape(-1, 28, 28), y_val)\n\n# Print the validation accuracy\nprint(f\"Validation Accuracy: {validation_accuracy * 100:.2f}%\")","metadata":{"execution":{"iopub.status.busy":"2024-02-16T16:50:40.632768Z","iopub.execute_input":"2024-02-16T16:50:40.633475Z","iopub.status.idle":"2024-02-16T16:50:42.083100Z","shell.execute_reply.started":"2024-02-16T16:50:40.633432Z","shell.execute_reply":"2024-02-16T16:50:42.081556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make predictions on the validation set\ny_pred = model.predict(X_val.values.reshape(-1, 28, 28))\n\n# Convert predicted probabilities to class labels\npredicted_labels = np.argmax(y_pred, axis=1)\n\n# Print confusion matrix and classification report\nconf_matrix = confusion_matrix(y_val, predicted_labels)\nclass_report = classification_report(y_val, predicted_labels)\n\ncm_display = metrics.ConfusionMatrixDisplay(confusion_matrix = conf_matrix)\n\n\ncm_display.plot()\nplt.show()\n\nprint(\"\\nClassification Report:\")\nprint(class_report)","metadata":{"execution":{"iopub.status.busy":"2024-02-16T16:50:42.085065Z","iopub.execute_input":"2024-02-16T16:50:42.085846Z","iopub.status.idle":"2024-02-16T16:50:43.658582Z","shell.execute_reply.started":"2024-02-16T16:50:42.085800Z","shell.execute_reply":"2024-02-16T16:50:43.657312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make predictions on the test set\npredictions = model.predict(test.values.reshape(-1, 28, 28))\n\n# Convert predicted probabilities to class labels\npredicted_labels = np.argmax(predictions, axis=1)\n\n# Create a DataFrame with ImageId and predicted Label\nsubmission_df = pd.DataFrame({'ImageId': range(1, len(test) + 1), 'Label': predicted_labels})\n\n# Save the predictions to a CSV file\nsubmission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-02-16T16:50:43.661530Z","iopub.execute_input":"2024-02-16T16:50:43.661888Z","iopub.status.idle":"2024-02-16T16:50:49.166764Z","shell.execute_reply.started":"2024-02-16T16:50:43.661858Z","shell.execute_reply":"2024-02-16T16:50:49.165837Z"},"trusted":true},"execution_count":null,"outputs":[]}]}