{"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":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30804,"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":"2024-12-15T20:11:15.307738Z","iopub.execute_input":"2024-12-15T20:11:15.308292Z","iopub.status.idle":"2024-12-15T20:11:16.555894Z","shell.execute_reply.started":"2024-12-15T20:11:15.308229Z","shell.execute_reply":"2024-12-15T20:11:16.554700Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**The DATA**","metadata":{}},{"cell_type":"markdown","source":"For this notebook we will use the Digit Recognizer competition dataset.\r\n\r\nLet's define the path to the dataset:","metadata":{}},{"cell_type":"code","source":"train_csv_path: str = \"../input/digit-recognizer/train.csv\"\ntest_csv_path: str = \"../input/digit-recognizer/test.csv\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-15T20:20:15.294934Z","iopub.execute_input":"2024-12-15T20:20:15.295691Z","iopub.status.idle":"2024-12-15T20:20:15.300352Z","shell.execute_reply.started":"2024-12-15T20:20:15.295650Z","shell.execute_reply":"2024-12-15T20:20:15.299035Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Import Packages**\n\r\nLets load all the needed packages for this notebook:","metadata":{}},{"cell_type":"code","source":"import cv2\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.model_selection import StratifiedKFold\nfrom sklearn.naive_bayes import BernoulliNB\nfrom matplotlib import pyplot as plt\nfrom typing import List\n%matplotlib inline\nsns.set_style(\"darkgrid\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-15T20:20:18.585738Z","iopub.execute_input":"2024-12-15T20:20:18.586163Z","iopub.status.idle":"2024-12-15T20:20:18.593323Z","shell.execute_reply.started":"2024-12-15T20:20:18.586094Z","shell.execute_reply":"2024-12-15T20:20:18.592031Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Load Data**\n\r\nLet’s load the data set using Pandas:","metadata":{}},{"cell_type":"code","source":"train_df: pd.DataFrame = pd.read_csv(train_csv_path)\ntest_df: pd.DataFrame = pd.read_csv(test_csv_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-15T20:20:24.266046Z","iopub.execute_input":"2024-12-15T20:20:24.266462Z","iopub.status.idle":"2024-12-15T20:20:28.571407Z","shell.execute_reply.started":"2024-12-15T20:20:24.266425Z","shell.execute_reply":"2024-12-15T20:20:28.569980Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Quick Look at the Data**\n\r\nLet’s take a look at the top five rows:","metadata":{}},{"cell_type":"code","source":"train_df.head(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-15T20:20:44.112566Z","iopub.execute_input":"2024-12-15T20:20:44.112973Z","iopub.status.idle":"2024-12-15T20:20:44.137660Z","shell.execute_reply.started":"2024-12-15T20:20:44.112935Z","shell.execute_reply":"2024-12-15T20:20:44.136642Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df.head(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-15T20:20:55.056096Z","iopub.execute_input":"2024-12-15T20:20:55.057330Z","iopub.status.idle":"2024-12-15T20:20:55.073054Z","shell.execute_reply.started":"2024-12-15T20:20:55.057277Z","shell.execute_reply":"2024-12-15T20:20:55.071988Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**2. Preprocess the Data**\n\nPrepare the data for training:\n\nExtract Features (X) and Labels (y) from the Training Dataset.\nNormalize Pixel Values to range [0, 1] (this helps in improving model performance).","metadata":{}},{"cell_type":"code","source":"# Separate features and labels from the training dataset\nX = train_df.iloc[:, 1:].values / 255.0  # Normalize pixel values\ny = train_df.iloc[:, 0].values           # Labels\n\n# Normalize test data\nX_test = test_df.values / 255.0\n\n# Check shapes\nprint(f\"Training Data Shape: {X.shape}\")\nprint(f\"Training Labels Shape: {y.shape}\")\nprint(f\"Test Data Shape: {X_test.shape}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-15T20:25:26.233412Z","iopub.execute_input":"2024-12-15T20:25:26.233824Z","iopub.status.idle":"2024-12-15T20:25:26.481984Z","shell.execute_reply.started":"2024-12-15T20:25:26.233789Z","shell.execute_reply":"2024-12-15T20:25:26.480923Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**3. Train-Test Split (Optional, for Validation)**\n\nSplit the training data into training and validation sets to evaluate the model.","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)\n\nprint(f\"Training Data Shape: {X_train.shape}\")\nprint(f\"Validation Data Shape: {X_val.shape}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-15T20:25:47.024242Z","iopub.execute_input":"2024-12-15T20:25:47.024674Z","iopub.status.idle":"2024-12-15T20:25:47.495491Z","shell.execute_reply.started":"2024-12-15T20:25:47.024641Z","shell.execute_reply":"2024-12-15T20:25:47.494329Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**4. Train the Naive Bayes Model**\n\nUse Bernoulli Naive Bayes, as pixel intensities in images (binary-like data) work well with this variant.","metadata":{}},{"cell_type":"code","source":"from sklearn.naive_bayes import BernoulliNB\nfrom sklearn.metrics import accuracy_score\n\n# Initialize the Bernoulli Naive Bayes model\nmodel = BernoulliNB()\n\n# Train the model\nmodel.fit(X_train, y_train)\n\n# Validate the model\ny_val_pred = model.predict(X_val)\nval_accuracy = accuracy_score(y_val, y_val_pred)\n\nprint(f\"Validation Accuracy: {val_accuracy:.4f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-15T20:26:03.976312Z","iopub.execute_input":"2024-12-15T20:26:03.976701Z","iopub.status.idle":"2024-12-15T20:26:04.429325Z","shell.execute_reply.started":"2024-12-15T20:26:03.976669Z","shell.execute_reply":"2024-12-15T20:26:04.428034Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**5. Make Predictions on Test Data**\n\nGenerate predictions on the test dataset using the trained model.","metadata":{}},{"cell_type":"code","source":"# Predict on test dataset\ntest_predictions = model.predict(X_test)\n\n# Check a few predictions\nprint(test_predictions[:10])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-15T20:26:17.126097Z","iopub.execute_input":"2024-12-15T20:26:17.126508Z","iopub.status.idle":"2024-12-15T20:26:17.482632Z","shell.execute_reply.started":"2024-12-15T20:26:17.126457Z","shell.execute_reply":"2024-12-15T20:26:17.478798Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**6. Prepare Submission File**\n\nKaggle requires a submission file in the format:\n\nImageId: Sequential ID starting from 1.\nLabel: Predicted digit.\nCreate and save the submission file:","metadata":{}},{"cell_type":"code","source":"# Prepare submission DataFrame\nsubmission = pd.DataFrame({'ImageId': np.arange(1, len(test_predictions) + 1), 'Label': test_predictions})\n\n# Save the submission file\nsubmission.to_csv('submission.csv', index=False)\n\nprint(\"Submission file created successfully!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-15T20:26:39.319566Z","iopub.execute_input":"2024-12-15T20:26:39.319955Z","iopub.status.idle":"2024-12-15T20:26:39.351307Z","shell.execute_reply.started":"2024-12-15T20:26:39.319921Z","shell.execute_reply":"2024-12-15T20:26:39.350207Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}