{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Getting Started: Mean Imputation\n\nThis notebook provides a simple method to imputing missing values, and provides code for making a submission file.","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom pathlib import Path\nfrom tqdm import tqdm\n\nfrom sklearn.impute import SimpleImputer","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-14T09:57:46.352371Z","iopub.execute_input":"2022-07-14T09:57:46.352852Z","iopub.status.idle":"2022-07-14T09:57:46.359004Z","shell.execute_reply.started":"2022-07-14T09:57:46.352818Z","shell.execute_reply":"2022-07-14T09:57:46.358089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_path = Path('/kaggle/input/tabular-playground-series-jun-2022/')\ndata = pd.read_csv(input_path / 'data.csv', index_col='row_id')\nsubmission = pd.read_csv(input_path / 'sample_submission.csv', index_col='row-col')","metadata":{"execution":{"iopub.status.busy":"2022-07-14T10:06:17.303434Z","iopub.execute_input":"2022-07-14T10:06:17.303984Z","iopub.status.idle":"2022-07-14T10:06:32.880065Z","shell.execute_reply.started":"2022-07-14T10:06:17.303947Z","shell.execute_reply":"2022-07-14T10:06:32.878979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T10:16:54.065944Z","iopub.execute_input":"2022-07-14T10:16:54.066397Z","iopub.status.idle":"2022-07-14T10:16:54.107470Z","shell.execute_reply.started":"2022-07-14T10:16:54.066364Z","shell.execute_reply":"2022-07-14T10:16:54.106283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Use scikit-learn SimpleImputer for a simple benchmark\n\nThere are many ways to impute missing values in data. One of the easiest is to simply replace the missing values of a feature with the mean of the feature values. `scikit-learn` has a method for this: `SimpleImputer`","metadata":{}},{"cell_type":"code","source":"imp = SimpleImputer(\n        missing_values=np.nan,\n        strategy='mean')\n\ndata[:] = imp.fit_transform(data)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T10:17:57.085215Z","iopub.execute_input":"2022-07-14T10:17:57.085648Z","iopub.status.idle":"2022-07-14T10:17:59.296650Z","shell.execute_reply.started":"2022-07-14T10:17:57.085616Z","shell.execute_reply":"2022-07-14T10:17:59.295249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-07-14T10:28:53.135017Z","iopub.execute_input":"2022-07-14T10:28:53.135521Z","iopub.status.idle":"2022-07-14T10:28:53.213815Z","shell.execute_reply.started":"2022-07-14T10:28:53.135485Z","shell.execute_reply":"2022-07-14T10:28:53.212472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Use `row-col` from the sample submission to find the imputed values","metadata":{}},{"cell_type":"code","source":"for i in tqdm(submission.index):\n    print(i)\n    row = int(i.split('-')[0])\n    col = i.split('-')[1]\n    submission.loc[i, 'value'] = data.loc[row, col]\n\nsubmission.to_csv('mean_benchmark.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-14T10:31:02.949038Z","iopub.execute_input":"2022-07-14T10:31:02.949895Z","iopub.status.idle":"2022-07-14T10:33:02.756108Z","shell.execute_reply.started":"2022-07-14T10:31:02.949847Z","shell.execute_reply":"2022-07-14T10:33:02.754789Z"},"trusted":true},"execution_count":null,"outputs":[]}]}