{"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":30761,"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","execution":{"iopub.status.busy":"2024-10-23T14:05:42.842082Z","iopub.execute_input":"2024-10-23T14:05:42.842845Z","iopub.status.idle":"2024-10-23T14:05:43.276505Z","shell.execute_reply.started":"2024-10-23T14:05:42.842801Z","shell.execute_reply":"2024-10-23T14:05:43.275297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1. Frame the Problem\n","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. Get the Data","metadata":{}},{"cell_type":"code","source":"%%bash\n\ncat /kaggle/input/digit-recognizer/train.csv | head -n 5","metadata":{"execution":{"iopub.status.busy":"2024-10-23T14:05:43.278768Z","iopub.execute_input":"2024-10-23T14:05:43.279409Z","iopub.status.idle":"2024-10-23T14:05:43.332879Z","shell.execute_reply.started":"2024-10-23T14:05:43.279356Z","shell.execute_reply":"2024-10-23T14:05:43.331815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\n    \"/kaggle/input/digit-recognizer/train.csv\",\n)\n\ntest_features_df = pd.read_csv(\n    \"/kaggle/input/digit-recognizer/test.csv\",\n)\n","metadata":{"execution":{"iopub.status.busy":"2024-10-23T14:05:43.334553Z","iopub.execute_input":"2024-10-23T14:05:43.335007Z","iopub.status.idle":"2024-10-23T14:05:49.028011Z","shell.execute_reply.started":"2024-10-23T14:05:43.334959Z","shell.execute_reply":"2024-10-23T14:05:49.026873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2024-10-23T14:05:49.031238Z","iopub.execute_input":"2024-10-23T14:05:49.031735Z","iopub.status.idle":"2024-10-23T14:05:49.101766Z","shell.execute_reply.started":"2024-10-23T14:05:49.031687Z","shell.execute_reply":"2024-10-23T14:05:49.100325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3. Explore the Data","metadata":{}},{"cell_type":"code","source":"train_df.describe()","metadata":{"execution":{"iopub.status.busy":"2024-10-23T14:13:29.651565Z","iopub.execute_input":"2024-10-23T14:13:29.651988Z","iopub.status.idle":"2024-10-23T14:13:32.201034Z","shell.execute_reply.started":"2024-10-23T14:13:29.651952Z","shell.execute_reply":"2024-10-23T14:13:32.199787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 4. Prepare the Data for ML ","metadata":{}},{"cell_type":"code","source":"train_features_df = train_df.drop(\"label\", axis=1)\ntrain_target = train_df.loc[:, \"label\"]\n","metadata":{"execution":{"iopub.status.busy":"2024-10-23T14:05:49.103563Z","iopub.execute_input":"2024-10-23T14:05:49.104030Z","iopub.status.idle":"2024-10-23T14:05:49.226031Z","shell.execute_reply.started":"2024-10-23T14:05:49.103979Z","shell.execute_reply":"2024-10-23T14:05:49.224745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import impute, feature_selection, preprocessing, pipeline\n\n\ndata_preprocessing_pipeline = pipeline.make_pipeline(\n    impute.SimpleImputer(),\n    feature_selection.VarianceThreshold(),\n    preprocessing.StandardScaler()\n).set_output(transform=\"pandas\")","metadata":{"execution":{"iopub.status.busy":"2024-10-23T14:55:36.407418Z","iopub.execute_input":"2024-10-23T14:55:36.407817Z","iopub.status.idle":"2024-10-23T14:55:36.413885Z","shell.execute_reply.started":"2024-10-23T14:55:36.407783Z","shell.execute_reply":"2024-10-23T14:55:36.412587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_preprocessing_pipeline","metadata":{"execution":{"iopub.status.busy":"2024-10-23T14:55:36.481575Z","iopub.execute_input":"2024-10-23T14:55:36.481992Z","iopub.status.idle":"2024-10-23T14:55:36.496360Z","shell.execute_reply.started":"2024-10-23T14:55:36.481955Z","shell.execute_reply":"2024-10-23T14:55:36.494873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 5. Explore Different Models","metadata":{}},{"cell_type":"code","source":"from sklearn import model_selection\n\n\nsubsampled_train_features_df, _, subsampled_train_target, _ = (\n    model_selection.train_test_split(\n        train_features_df,\n        train_target,\n        stratify=train_target,\n        test_size=0.9,\n        random_state=42\n    )\n)","metadata":{"execution":{"iopub.status.busy":"2024-10-23T14:28:32.050247Z","iopub.execute_input":"2024-10-23T14:28:32.050654Z","iopub.status.idle":"2024-10-23T14:28:32.298434Z","shell.execute_reply.started":"2024-10-23T14:28:32.050618Z","shell.execute_reply":"2024-10-23T14:28:32.297296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subsampled_train_features_df.info()","metadata":{"execution":{"iopub.status.busy":"2024-10-23T14:28:40.929191Z","iopub.execute_input":"2024-10-23T14:28:40.929676Z","iopub.status.idle":"2024-10-23T14:28:40.979019Z","shell.execute_reply.started":"2024-10-23T14:28:40.929623Z","shell.execute_reply":"2024-10-23T14:28:40.977761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import linear_model\n\n\nestimator = linear_model.SGDClassifier(\n    loss=\"log_loss\",\n    n_jobs=-1,\n    random_state=42,\n)\n\n# fit transform training features\n_subsampled_train_feature_df = data_preprocessing_pipeline.fit_transform(\n    subsampled_train_features_df\n)\n_ = estimator.fit(_subsampled_train_feature_df, subsampled_train_target)","metadata":{"execution":{"iopub.status.busy":"2024-10-23T14:56:58.886958Z","iopub.execute_input":"2024-10-23T14:56:58.887446Z","iopub.status.idle":"2024-10-23T14:57:14.909647Z","shell.execute_reply.started":"2024-10-23T14:56:58.887396Z","shell.execute_reply":"2024-10-23T14:57:14.908391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 6. Fine-tune the Models","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Identify the best model","metadata":{}},{"cell_type":"code","source":"# refit the pipeline to the entire training data\n_train_features_df = data_preprocessing_pipeline.fit_transform(train_features_df)\nbest_estimator = estimator.fit(_train_features_df, train_target)\n","metadata":{"execution":{"iopub.status.busy":"2024-10-23T15:01:32.358166Z","iopub.execute_input":"2024-10-23T15:01:32.359302Z","iopub.status.idle":"2024-10-23T15:06:04.112029Z","shell.execute_reply.started":"2024-10-23T15:01:32.359251Z","shell.execute_reply":"2024-10-23T15:06:04.110567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 7. Summarize your results","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 8. Submit your predictions to the competition","metadata":{}},{"cell_type":"code","source":"# only transform test features!\n_test_features_df = data_preprocessing_pipeline.transform(test_features_df)\npredictions = best_estimator.predict(_test_features_df)","metadata":{"execution":{"iopub.status.busy":"2024-10-23T15:06:04.114252Z","iopub.execute_input":"2024-10-23T15:06:04.114641Z","iopub.status.idle":"2024-10-23T15:06:04.613432Z","shell.execute_reply.started":"2024-10-23T15:06:04.114602Z","shell.execute_reply":"2024-10-23T15:06:04.611120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions[:5]","metadata":{"execution":{"iopub.status.busy":"2024-10-23T15:22:26.286902Z","iopub.execute_input":"2024-10-23T15:22:26.287378Z","iopub.status.idle":"2024-10-23T15:22:26.295433Z","shell.execute_reply.started":"2024-10-23T15:22:26.287336Z","shell.execute_reply":"2024-10-23T15:22:26.294088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%bash\n\ncat /kaggle/input/digit-recognizer/sample_submission.csv | head -n 5","metadata":{"execution":{"iopub.status.busy":"2024-10-23T15:22:26.604357Z","iopub.execute_input":"2024-10-23T15:22:26.604816Z","iopub.status.idle":"2024-10-23T15:22:26.635603Z","shell.execute_reply.started":"2024-10-23T15:22:26.604776Z","shell.execute_reply":"2024-10-23T15:22:26.634275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = (\n    pd.read_csv(\n        \"/kaggle/input/digit-recognizer/sample_submission.csv\",\n    ).assign(\n        Label=predictions\n    )\n)\n\nsubmission_df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-10-23T15:22:27.436367Z","iopub.execute_input":"2024-10-23T15:22:27.436818Z","iopub.status.idle":"2024-10-23T15:22:27.475871Z","shell.execute_reply.started":"2024-10-23T15:22:27.436778Z","shell.execute_reply":"2024-10-23T15:22:27.474734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%bash\n\ncat /kaggle/working/submission.csv | head -n 5","metadata":{"execution":{"iopub.status.busy":"2024-10-23T15:22:28.229358Z","iopub.execute_input":"2024-10-23T15:22:28.229799Z","iopub.status.idle":"2024-10-23T15:22:28.246476Z","shell.execute_reply.started":"2024-10-23T15:22:28.229761Z","shell.execute_reply":"2024-10-23T15:22:28.245185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}