{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","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":30635,"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-01-28T06:01:57.817797Z","iopub.execute_input":"2024-01-28T06:01:57.818159Z","iopub.status.idle":"2024-01-28T06:01:58.222495Z","shell.execute_reply.started":"2024-01-28T06:01:57.818125Z","shell.execute_reply":"2024-01-28T06:01:58.221405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-01-28T06:03:37.289122Z","iopub.execute_input":"2024-01-28T06:03:37.289521Z","iopub.status.idle":"2024-01-28T06:03:40.790301Z","shell.execute_reply.started":"2024-01-28T06:03:37.289484Z","shell.execute_reply":"2024-01-28T06:03:40.789098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-28T06:03:44.915084Z","iopub.execute_input":"2024-01-28T06:03:44.915788Z","iopub.status.idle":"2024-01-28T06:03:45.191402Z","shell.execute_reply.started":"2024-01-28T06:03:44.915744Z","shell.execute_reply":"2024-01-28T06:03:45.190359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-28T06:03:55.392636Z","iopub.execute_input":"2024-01-28T06:03:55.393174Z","iopub.status.idle":"2024-01-28T06:03:55.420023Z","shell.execute_reply.started":"2024-01-28T06:03:55.393121Z","shell.execute_reply":"2024-01-28T06:03:55.418882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_features_df = train_df.drop(\"label\", axis=1)\ntrain_targets = train_df.loc[:, \"label\"]","metadata":{"execution":{"iopub.status.busy":"2024-01-28T06:05:11.246452Z","iopub.execute_input":"2024-01-28T06:05:11.246962Z","iopub.status.idle":"2024-01-28T06:05:11.366981Z","shell.execute_reply.started":"2024-01-28T06:05:11.246919Z","shell.execute_reply":"2024-01-28T06:05:11.365652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import linear_model, pipeline, preprocessing, metrics, model_selection, feature_selection ","metadata":{"execution":{"iopub.status.busy":"2024-01-28T06:06:13.512193Z","iopub.execute_input":"2024-01-28T06:06:13.512680Z","iopub.status.idle":"2024-01-28T06:06:14.570376Z","shell.execute_reply.started":"2024-01-28T06:06:13.512639Z","shell.execute_reply":"2024-01-28T06:06:14.569163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ml_pipeline = pipeline.make_pipeline(\n    feature_selection.VarianceThreshold(threshold=0.0),\n    preprocessing.StandardScaler(),\n    linear_model.LogisticRegression(\n        max_iter=1000,\n    )\n)\nml_pipeline","metadata":{"execution":{"iopub.status.busy":"2024-01-28T06:09:37.498501Z","iopub.execute_input":"2024-01-28T06:09:37.498914Z","iopub.status.idle":"2024-01-28T06:09:37.513374Z","shell.execute_reply.started":"2024-01-28T06:09:37.498881Z","shell.execute_reply":"2024-01-28T06:09:37.512144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_ = ml_pipeline.fit(train_features_df, train_targets)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T06:09:38.768943Z","iopub.execute_input":"2024-01-28T06:09:38.769721Z","iopub.status.idle":"2024-01-28T06:11:52.473806Z","shell.execute_reply.started":"2024-01-28T06:09:38.769665Z","shell.execute_reply":"2024-01-28T06:11:52.472417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_predictions = ml_pipeline.predict(train_features_df)\nreport = metrics.classification_report(\n    train_targets,\n    train_predictions,\n)\nprint(report)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T06:14:17.137936Z","iopub.execute_input":"2024-01-28T06:14:17.138293Z","iopub.status.idle":"2024-01-28T06:14:17.669971Z","shell.execute_reply.started":"2024-01-28T06:14:17.138263Z","shell.execute_reply":"2024-01-28T06:14:17.668613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_features_df = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-01-28T06:15:41.188606Z","iopub.execute_input":"2024-01-28T06:15:41.188999Z","iopub.status.idle":"2024-01-28T06:15:42.739601Z","shell.execute_reply.started":"2024-01-28T06:15:41.188964Z","shell.execute_reply":"2024-01-28T06:15:42.738593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_features_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-28T06:15:48.188035Z","iopub.execute_input":"2024-01-28T06:15:48.188433Z","iopub.status.idle":"2024-01-28T06:15:48.206589Z","shell.execute_reply.started":"2024-01-28T06:15:48.188397Z","shell.execute_reply":"2024-01-28T06:15:48.205466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predictions = ml_pipeline.predict(test_features_df)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T06:19:02.733950Z","iopub.execute_input":"2024-01-28T06:19:02.735073Z","iopub.status.idle":"2024-01-28T06:19:03.054381Z","shell.execute_reply.started":"2024-01-28T06:19:02.735011Z","shell.execute_reply":"2024-01-28T06:19:03.052929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = (\n    pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\", index_col=\"ImageId\")\n      .assign(Label=test_predictions)\n)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T06:19:13.167978Z","iopub.execute_input":"2024-01-28T06:19:13.168403Z","iopub.status.idle":"2024-01-28T06:19:13.182774Z","shell.execute_reply.started":"2024-01-28T06:19:13.168367Z","shell.execute_reply":"2024-01-28T06:19:13.181464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-28T06:19:22.891212Z","iopub.execute_input":"2024-01-28T06:19:22.891736Z","iopub.status.idle":"2024-01-28T06:19:22.902923Z","shell.execute_reply.started":"2024-01-28T06:19:22.891691Z","shell.execute_reply":"2024-01-28T06:19:22.902066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_ = submission_df.to_csv(\"submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-01-28T06:19:53.648650Z","iopub.execute_input":"2024-01-28T06:19:53.649751Z","iopub.status.idle":"2024-01-28T06:19:53.698332Z","shell.execute_reply.started":"2024-01-28T06:19:53.649674Z","shell.execute_reply":"2024-01-28T06:19:53.697176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%bash\n\ncat submission.csv | head -n 5","metadata":{"execution":{"iopub.status.busy":"2024-01-28T06:20:15.428777Z","iopub.execute_input":"2024-01-28T06:20:15.429220Z","iopub.status.idle":"2024-01-28T06:20:15.455885Z","shell.execute_reply.started":"2024-01-28T06:20:15.429184Z","shell.execute_reply":"2024-01-28T06:20:15.455185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}