{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":59743,"databundleVersionId":6448218,"sourceType":"competition"}],"dockerImageVersionId":30918,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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":{"iopub.status.busy":"2025-03-01T18:28:28.345377Z","iopub.execute_input":"2025-03-01T18:28:28.345762Z","iopub.status.idle":"2025-03-01T18:28:28.781010Z","shell.execute_reply.started":"2025-03-01T18:28:28.345735Z","shell.execute_reply":"2025-03-01T18:28:28.779664Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n# Load the train and test data\ntrain = pd.read_csv('/kaggle/input/digit-recognizer-without-Extra-Training-Data/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer-without-Extra-Training-Data/test.csv')\n\n# Display the first few rows of the train dataset\ntrain.head()\n\n# Display the first few rows of the test dataset\ntest.head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-01T18:28:33.949244Z","iopub.execute_input":"2025-03-01T18:28:33.949819Z","iopub.status.idle":"2025-03-01T18:28:37.427909Z","shell.execute_reply.started":"2025-03-01T18:28:33.949787Z","shell.execute_reply":"2025-03-01T18:28:37.426621Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Visualize some sample images from the training data\ndef visualize_images(images, labels):\n    fig, axes = plt.subplots(1, 5, figsize=(10, 5))\n    for i in range(5):\n        axes[i].imshow(images[i].reshape(28, 28), cmap='gray')\n        axes[i].set_title(f\"Label: {labels[i]}\")\n        axes[i].axis('off')\n    plt.show()\n\n# Extract image and label data\nX_train = train.drop('label', axis=1).values\ny_train = train['label'].values\n\n# Visualize the first few images and their labels\nvisualize_images(X_train, y_train)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-01T18:28:41.459484Z","iopub.execute_input":"2025-03-01T18:28:41.460035Z","iopub.status.idle":"2025-03-01T18:28:41.833589Z","shell.execute_reply.started":"2025-03-01T18:28:41.459962Z","shell.execute_reply":"2025-03-01T18:28:41.832537Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Convert X_train to DataFrame\nX_train_df = pd.DataFrame(X_train)\n\n# Display statistical summary for pixel values\nprint(X_train_df.describe())\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-01T18:28:45.261056Z","iopub.execute_input":"2025-03-01T18:28:45.261422Z","iopub.status.idle":"2025-03-01T18:28:47.367703Z","shell.execute_reply.started":"2025-03-01T18:28:45.261395Z","shell.execute_reply":"2025-03-01T18:28:47.366312Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Normalize pixel values to be between 0 and 1\nX_train = X_train / 255.0\n\n# Normalize the test data\nX_test = test.values / 255.0\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-01T18:28:52.179122Z","iopub.execute_input":"2025-03-01T18:28:52.179490Z","iopub.status.idle":"2025-03-01T18:28:52.395592Z","shell.execute_reply.started":"2025-03-01T18:28:52.179464Z","shell.execute_reply":"2025-03-01T18:28:52.394627Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n# Split the data into training and validation sets\nX_train_split, X_val_split, y_train_split, y_val_split = train_test_split(X_train, y_train, test_size=0.2, random_state=48)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-01T18:28:56.275372Z","iopub.execute_input":"2025-03-01T18:28:56.275787Z","iopub.status.idle":"2025-03-01T18:28:57.247616Z","shell.execute_reply.started":"2025-03-01T18:28:56.275753Z","shell.execute_reply":"2025-03-01T18:28:57.246383Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Check class distribution in the train dataset\nimport seaborn as sns\n\n# Get the class distribution\nclass_distribution = train['label'].value_counts()\n\n# Plot the class distribution\nplt.figure(figsize=(8, 5))\nsns.barplot(x=class_distribution.index, y=class_distribution.values)\nplt.title(\"Class Distribution in Training Data\")\nplt.xlabel(\"Digit\")\nplt.ylabel(\"Number of Instances\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-01T18:29:01.125733Z","iopub.execute_input":"2025-03-01T18:29:01.126372Z","iopub.status.idle":"2025-03-01T18:29:01.495035Z","shell.execute_reply.started":"2025-03-01T18:29:01.126334Z","shell.execute_reply":"2025-03-01T18:29:01.493422Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import layers, models\n\n# Build a  neural network\nmodel = models.Sequential([\n    layers.Dense(128, activation='relu', input_shape=(784,)),\n    layers.Dropout(0.2),\n    layers.Dense(64, activation='relu'),\n    layers.Dense(10, activation='softmax')  # 10 classes for digits 0-9\n])\n\n# Compile the model\nmodel.compile(optimizer='adam', \n              loss='sparse_categorical_crossentropy', \n              metrics=['accuracy'])\n\n# Print the model summary\nmodel.summary()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-01T18:29:05.149197Z","iopub.execute_input":"2025-03-01T18:29:05.149823Z","iopub.status.idle":"2025-03-01T18:29:09.001152Z","shell.execute_reply.started":"2025-03-01T18:29:05.149787Z","shell.execute_reply":"2025-03-01T18:29:08.999822Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Train the model\nhistory = model.fit(X_train_split, y_train_split, epochs=10, batch_size=32, validation_data=(X_val_split, y_val_split))\n\n# Plot the training history\nplt.plot(history.history['accuracy'], label='accuracy')\nplt.plot(history.history['val_accuracy'], label = 'val_accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend(loc='lower right')\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-01T18:29:13.191116Z","iopub.execute_input":"2025-03-01T18:29:13.191839Z","iopub.status.idle":"2025-03-01T18:29:51.972617Z","shell.execute_reply.started":"2025-03-01T18:29:13.191800Z","shell.execute_reply":"2025-03-01T18:29:51.971154Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Evaluate the model on the validation data\nval_loss, val_acc = model.evaluate(X_val_split, y_val_split)\nprint(f'Validation accuracy: {val_acc}')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-01T18:29:58.787378Z","iopub.execute_input":"2025-03-01T18:29:58.787775Z","iopub.status.idle":"2025-03-01T18:29:59.370263Z","shell.execute_reply.started":"2025-03-01T18:29:58.787747Z","shell.execute_reply":"2025-03-01T18:29:59.368911Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import seaborn as sns\nfrom sklearn.metrics import confusion_matrix\n# Make predictions on the validation data\ny_val_pred = model.predict(X_val_split)\ny_val_pred_classes = np.argmax(y_val_pred, axis=1)\n\n# Calculate confusion matrix\ncm = confusion_matrix(y_val_split, y_val_pred_classes)\n# Plot the confusion matrix using heatmap\nplt.figure(figsize=(10, 7))\nsns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=np.arange(10), yticklabels=np.arange(10), cbar=False)\nplt.title('Confusion Matrix Heatmap')\nplt.xlabel('Predicted')\nplt.ylabel('Actual')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-01T18:30:04.131804Z","iopub.execute_input":"2025-03-01T18:30:04.132236Z","iopub.status.idle":"2025-03-01T18:30:05.568990Z","shell.execute_reply.started":"2025-03-01T18:30:04.132202Z","shell.execute_reply":"2025-03-01T18:30:05.567875Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Make predictions on the test dataset\ny_pred = model.predict(X_test)\ny_pred_classes = np.argmax(y_pred, axis=1)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-01T18:30:11.181101Z","iopub.execute_input":"2025-03-01T18:30:11.181485Z","iopub.status.idle":"2025-03-01T18:30:13.206895Z","shell.execute_reply.started":"2025-03-01T18:30:11.181458Z","shell.execute_reply":"2025-03-01T18:30:13.205521Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Save the trained model in .h5 format\nmodel.save('digit_recognizer_model.h5')\n\n# Make predictions on the test dataset\ny_pred = model.predict(X_test)\ny_pred_classes = np.argmax(y_pred, axis=1)\n\n# Prepare the submission DataFrame\nsubmission = pd.DataFrame({\n    'ImageId': np.arange(1, len(y_pred_classes) + 1),\n    'Label': y_pred_classes\n})\n\n# Save to CSV file\nsubmission.to_csv('submission.csv', index=False)\n\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-01T18:30:19.118944Z","iopub.execute_input":"2025-03-01T18:30:19.119373Z","iopub.status.idle":"2025-03-01T18:30:21.117626Z","shell.execute_reply.started":"2025-03-01T18:30:19.119342Z","shell.execute_reply":"2025-03-01T18:30:21.116225Z"}},"outputs":[],"execution_count":null}]}