{"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":63087,"databundleVersionId":6874572,"sourceType":"competition"}],"dockerImageVersionId":30684,"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_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score","metadata":{"execution":{"iopub.status.busy":"2024-04-10T03:48:25.347160Z","iopub.execute_input":"2024-04-10T03:48:25.347585Z","iopub.status.idle":"2024-04-10T03:48:43.627848Z","shell.execute_reply.started":"2024-04-10T03:48:25.347547Z","shell.execute_reply":"2024-04-10T03:48:43.626651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv(\"/kaggle/input/copy-of-digit-recognizer-testing/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-04-10T03:48:57.267174Z","iopub.execute_input":"2024-04-10T03:48:57.267944Z","iopub.status.idle":"2024-04-10T03:49:01.433702Z","shell.execute_reply.started":"2024-04-10T03:48:57.267910Z","shell.execute_reply":"2024-04-10T03:49:01.432413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Separate features (pixels) and labels\nX = train_data.drop(\"label\", axis=1).values.reshape(-1, 28, 28, 1)\ny = train_data[\"label\"]","metadata":{"execution":{"iopub.status.busy":"2024-04-10T03:49:01.435646Z","iopub.execute_input":"2024-04-10T03:49:01.436004Z","iopub.status.idle":"2024-04-10T03:49:01.552279Z","shell.execute_reply.started":"2024-04-10T03:49:01.435972Z","shell.execute_reply":"2024-04-10T03:49:01.551257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split the data into training and validation sets\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-10T03:49:04.277204Z","iopub.execute_input":"2024-04-10T03:49:04.277675Z","iopub.status.idle":"2024-04-10T03:49:04.802369Z","shell.execute_reply.started":"2024-04-10T03:49:04.277639Z","shell.execute_reply":"2024-04-10T03:49:04.801067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the CNN model architecture\nmodel = tf.keras.Sequential([\n    tf.keras.layers.Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)),\n    tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),\n    tf.keras.layers.Conv2D(64, kernel_size=(3, 3), activation='relu'),\n    tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(128, activation='relu'),\n    tf.keras.layers.Dropout(0.5),\n    tf.keras.layers.Dense(10, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2024-04-10T03:49:15.238977Z","iopub.execute_input":"2024-04-10T03:49:15.239550Z","iopub.status.idle":"2024-04-10T03:49:15.365189Z","shell.execute_reply.started":"2024-04-10T03:49:15.239507Z","shell.execute_reply":"2024-04-10T03:49:15.363913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the model\nmodel.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2024-04-10T03:50:00.017942Z","iopub.execute_input":"2024-04-10T03:50:00.018693Z","iopub.status.idle":"2024-04-10T03:50:00.036097Z","shell.execute_reply.started":"2024-04-10T03:50:00.018655Z","shell.execute_reply":"2024-04-10T03:50:00.034877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the model\nmodel.fit(X_train, y_train, epochs=10, validation_data=(X_val, y_val))","metadata":{"execution":{"iopub.status.busy":"2024-04-10T03:50:12.517387Z","iopub.execute_input":"2024-04-10T03:50:12.517763Z","iopub.status.idle":"2024-04-10T03:53:31.466856Z","shell.execute_reply.started":"2024-04-10T03:50:12.517735Z","shell.execute_reply":"2024-04-10T03:53:31.466022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate the model\ny_pred_proba = model.predict(X_val)\ny_pred = np.argmax(y_pred_proba, axis=1)\naccuracy = accuracy_score(y_val, y_pred)\nprint(\"Validation Accuracy:\", accuracy)","metadata":{"execution":{"iopub.status.busy":"2024-04-10T03:53:44.803577Z","iopub.execute_input":"2024-04-10T03:53:44.804760Z","iopub.status.idle":"2024-04-10T03:53:46.706740Z","shell.execute_reply.started":"2024-04-10T03:53:44.804710Z","shell.execute_reply":"2024-04-10T03:53:46.705899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the test data\ntest_data = pd.read_csv(\"/kaggle/input/copy-of-digit-recognizer-testing/test.csv\")\nX_test = test_data.values.reshape(-1, 28, 28, 1)\n\n# Make predictions on the test data\ntest_predictions_proba = model.predict(X_test)\ntest_predictions = np.argmax(test_predictions_proba, axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-10T03:54:09.000228Z","iopub.execute_input":"2024-04-10T03:54:09.001549Z","iopub.status.idle":"2024-04-10T03:54:17.248069Z","shell.execute_reply.started":"2024-04-10T03:54:09.001500Z","shell.execute_reply":"2024-04-10T03:54:17.246741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Prepare submission file\nsubmission_df = pd.DataFrame({\n    \"ImageId\": range(1, len(test_predictions) + 1),\n    \"Label\": test_predictions\n})\n\n# Save submission file\nsubmission_df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-04-10T03:55:43.769785Z","iopub.execute_input":"2024-04-10T03:55:43.770195Z","iopub.status.idle":"2024-04-10T03:55:43.819035Z","shell.execute_reply.started":"2024-04-10T03:55:43.770164Z","shell.execute_reply":"2024-04-10T03:55:43.817553Z"},"trusted":true},"execution_count":null,"outputs":[]}]}