{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30775,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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","execution":{"iopub.status.busy":"2024-09-26T19:38:31.978599Z","iopub.execute_input":"2024-09-26T19:38:31.979024Z","iopub.status.idle":"2024-09-26T19:38:31.987951Z","shell.execute_reply.started":"2024-09-26T19:38:31.978984Z","shell.execute_reply":"2024-09-26T19:38:31.986841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Import necessary libraries\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Dropout, Flatten, BatchNormalization, Input\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau\nfrom tensorflow.keras.datasets import mnist\nfrom tensorflow.keras.utils import to_categorical\n\n# # Load MNIST data\n# (X_train, y_train), (X_val, y_val) = mnist.load_data()","metadata":{"execution":{"iopub.status.busy":"2024-09-26T19:38:31.999605Z","iopub.execute_input":"2024-09-26T19:38:31.999937Z","iopub.status.idle":"2024-09-26T19:38:32.007709Z","shell.execute_reply.started":"2024-09-26T19:38:31.999901Z","shell.execute_reply":"2024-09-26T19:38:32.006647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Load Dataset\n\ntrain = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\n\n# Explore the data\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2024-09-26T19:38:32.009920Z","iopub.execute_input":"2024-09-26T19:38:32.010357Z","iopub.status.idle":"2024-09-26T19:38:36.624904Z","shell.execute_reply.started":"2024-09-26T19:38:32.010297Z","shell.execute_reply":"2024-09-26T19:38:36.623784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Preprocessing\n### 1. Separate Features and Labels\nSplit the training data into features (x) and labels (y).","metadata":{}},{"cell_type":"code","source":"X_train = train.drop('label', axis=1).values\ny_train = train['label'].values","metadata":{"execution":{"iopub.status.busy":"2024-09-26T19:38:36.626172Z","iopub.execute_input":"2024-09-26T19:38:36.626583Z","iopub.status.idle":"2024-09-26T19:38:36.707208Z","shell.execute_reply.started":"2024-09-26T19:38:36.626538Z","shell.execute_reply":"2024-09-26T19:38:36.706415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 3. Reshaping the data\nFor training neural networks like CNNs, reshape the 1D arrays into 2D images (28x28 pixels) with a single colour channel.","metadata":{}},{"cell_type":"code","source":"X_train = X_train.reshape(-1, 28, 28, 1)\ntest = test.values.reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2024-09-26T19:38:36.709181Z","iopub.execute_input":"2024-09-26T19:38:36.709553Z","iopub.status.idle":"2024-09-26T19:38:36.714437Z","shell.execute_reply.started":"2024-09-26T19:38:36.709515Z","shell.execute_reply":"2024-09-26T19:38:36.713442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2. Normalization\nNormalize pixel values to the range [0,1] by dividing by 255. Neural netwoks perform better with normalized data.","metadata":{}},{"cell_type":"code","source":"X_train = X_train.astype('float32') / 255.0\ntest = test.astype('float32') / 255.0","metadata":{"execution":{"iopub.status.busy":"2024-09-26T19:38:36.715647Z","iopub.execute_input":"2024-09-26T19:38:36.715930Z","iopub.status.idle":"2024-09-26T19:38:36.845064Z","shell.execute_reply.started":"2024-09-26T19:38:36.715899Z","shell.execute_reply":"2024-09-26T19:38:36.844224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Visualize a few examples from the dataset","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10, 10))\nfor i in range(9):\n    plt.subplot(3, 3, i+1)\n    plt.imshow(X_train[i].reshape(28, 28), cmap='gray')\n    plt.title(f\"Label: {y_train[i]}\")\n    plt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-26T19:38:36.846271Z","iopub.execute_input":"2024-09-26T19:38:36.846676Z","iopub.status.idle":"2024-09-26T19:38:37.477411Z","shell.execute_reply.started":"2024-09-26T19:38:36.846632Z","shell.execute_reply":"2024-09-26T19:38:37.476498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 4. One-Hot Encoding (Optional)\nIf you are using deep learning model with a categorical output layer, convert labels into one-hot encoded values.","metadata":{}},{"cell_type":"code","source":"y_train = tf.keras.utils.to_categorical(y_train, num_classes=10)\n\n# Split the training data into training and validation sets (for model evaluation)\nX_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.1, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-09-26T19:38:37.478544Z","iopub.execute_input":"2024-09-26T19:38:37.478822Z","iopub.status.idle":"2024-09-26T19:38:37.830197Z","shell.execute_reply.started":"2024-09-26T19:38:37.478791Z","shell.execute_reply":"2024-09-26T19:38:37.829418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Building the model","metadata":{}},{"cell_type":"code","source":"# Create the improved model using the Input layer\ndef create_improved_model():\n    model = Sequential()\n\n    # Use Input layer to specify input shape\n    model.add(Input(shape=(28, 28, 1)))\n\n    # Add Convolutional layers with BatchNormalization\n    model.add(Conv2D(32, kernel_size=(3, 3), activation='relu'))\n    model.add(BatchNormalization())\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    \n    model.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))\n    model.add(BatchNormalization())\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    \n    # Add Dense layers with Dropout\n    model.add(Flatten())\n    model.add(Dense(128, activation='relu'))\n    model.add(Dropout(0.3))  # Dropout to prevent overfitting\n    model.add(Dense(64, activation='relu'))\n    model.add(Dropout(0.3))\n    \n    model.add(Dense(10, activation='softmax'))  # Output layer for 10 classes\n    \n    return model\n\n# Compile the model\nmodel = create_improved_model()\nmodel.compile(optimizer=Adam(learning_rate=0.001), loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Data augmentation\ndatagen = ImageDataGenerator(\n    rotation_range=10,\n    zoom_range=0.1,\n    width_shift_range=0.1,\n    height_shift_range=0.1\n)\ndatagen.fit(X_train)\n\n# Add ReduceLROnPlateau to reduce the learning rate if validation loss plateaus\nreduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=3, verbose=1)\n\n# Train the model\nhistory = model.fit(datagen.flow(X_train, y_train, batch_size=128),\n                    epochs=20, \n                    validation_data=(X_val, y_val),\n                    callbacks=[reduce_lr],\n                    verbose=2)\n","metadata":{"execution":{"iopub.status.busy":"2024-09-26T19:38:37.831310Z","iopub.execute_input":"2024-09-26T19:38:37.831639Z","iopub.status.idle":"2024-09-26T19:42:50.833427Z","shell.execute_reply.started":"2024-09-26T19:38:37.831605Z","shell.execute_reply":"2024-09-26T19:42:50.832519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Using CNNs","metadata":{}},{"cell_type":"markdown","source":"### Model Evaluation and Predictions\n\nOnce the model is trained, you can evaluate it on the training/validation data and make predictions for the test set.","metadata":{}},{"cell_type":"code","source":"# Predict labels for test data\npredictions = model.predict(test)\n\n# Convert predictions to label indices\npredictions = np.argmax(predictions, axis=1)\n\n# Prepare the submission file\nsubmission = pd.DataFrame({'ImageId': np.arange(1, len(predictions) + 1), 'Label': predictions})\nsubmission.to_csv('submission3.csv', index=False)\n\nprint(\"Submission file created successfully!\")\n","metadata":{"execution":{"iopub.status.busy":"2024-09-26T19:42:50.836476Z","iopub.execute_input":"2024-09-26T19:42:50.837028Z","iopub.status.idle":"2024-09-26T19:42:53.008149Z","shell.execute_reply.started":"2024-09-26T19:42:50.836992Z","shell.execute_reply":"2024-09-26T19:42:53.007203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}}]}