{"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":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"#  Load and Preprocess the Data\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\n\n# Load the data\ntrain_data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\n\n# Separate features and labels\nX_train = train_data.drop(columns='label').values\ny_train = train_data['label'].values\n\n# Normalize the pixel values\nX_train = X_train / 255.0\nX_test = test_data.values / 255.0\n\n# Reshape the data to 28x28 images\nX_train = X_train.reshape(-1, 28, 28, 1)\nX_test = X_test.reshape(-1, 28, 28, 1)\n\n# One-hot encode the labels\ny_train = to_categorical(y_train)\n\n# Split the training data into train and validation sets\nX_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.1, random_state=42)\n","metadata":{"execution":{"iopub.status.busy":"2024-07-13T11:32:38.794355Z","iopub.execute_input":"2024-07-13T11:32:38.794749Z","iopub.status.idle":"2024-07-13T11:32:46.263991Z","shell.execute_reply.started":"2024-07-13T11:32:38.794719Z","shell.execute_reply":"2024-07-13T11:32:46.262889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Build the CNN Model\n","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\n\n# Build the model\nmodel = Sequential([\n    Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)),\n    MaxPooling2D(pool_size=(2, 2)),\n    Conv2D(64, kernel_size=(3, 3), activation='relu'),\n    MaxPooling2D(pool_size=(2, 2)),\n    Flatten(),\n    Dense(128, activation='relu'),\n    Dropout(0.5),\n    Dense(10, activation='softmax')\n])\n\n# Compile the model\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Print the model summary\nmodel.summary()\n","metadata":{"execution":{"iopub.status.busy":"2024-07-13T11:33:42.781389Z","iopub.execute_input":"2024-07-13T11:33:42.781933Z","iopub.status.idle":"2024-07-13T11:33:42.945206Z","shell.execute_reply.started":"2024-07-13T11:33:42.781893Z","shell.execute_reply":"2024-07-13T11:33:42.944028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train The Model","metadata":{}},{"cell_type":"code","source":"# Train the model\nhistory = model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=10, batch_size=200)","metadata":{"execution":{"iopub.status.busy":"2024-07-13T11:34:08.505394Z","iopub.execute_input":"2024-07-13T11:34:08.506439Z","iopub.status.idle":"2024-07-13T11:36:51.301992Z","shell.execute_reply.started":"2024-07-13T11:34:08.506399Z","shell.execute_reply":"2024-07-13T11:36:51.300651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluate the Model and Save Predictions\n","metadata":{}},{"cell_type":"code","source":"# Predict on the test data\ny_test_pred = model.predict(X_test)\ny_test_pred_labels = np.argmax(y_test_pred, axis=1)\n\n# Create a submission DataFrame\nsubmission = pd.DataFrame({'ImageId': np.arange(1, len(y_test_pred_labels) + 1), 'Label': y_test_pred_labels})\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-07-13T11:37:00.414748Z","iopub.execute_input":"2024-07-13T11:37:00.415204Z","iopub.status.idle":"2024-07-13T11:37:06.333083Z","shell.execute_reply.started":"2024-07-13T11:37:00.415168Z","shell.execute_reply":"2024-07-13T11:37:06.331871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2024-07-13T11:37:21.454397Z","iopub.execute_input":"2024-07-13T11:37:21.454814Z","iopub.status.idle":"2024-07-13T11:37:21.473431Z","shell.execute_reply.started":"2024-07-13T11:37:21.454780Z","shell.execute_reply":"2024-07-13T11:37:21.472363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the submission to a CSV file\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-07-13T11:38:00.948281Z","iopub.execute_input":"2024-07-13T11:38:00.948704Z","iopub.status.idle":"2024-07-13T11:38:00.996033Z","shell.execute_reply.started":"2024-07-13T11:38:00.948672Z","shell.execute_reply":"2024-07-13T11:38:00.994874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}