{"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":"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":"2022-07-21T12:34:14.412498Z","iopub.execute_input":"2022-07-21T12:34:14.414007Z","iopub.status.idle":"2022-07-21T12:34:14.422052Z","shell.execute_reply.started":"2022-07-21T12:34:14.413954Z","shell.execute_reply":"2022-07-21T12:34:14.420959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os, sys, gc\nfrom pathlib import Path\nfrom tqdm.auto import tqdm\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\n\n%matplotlib inline\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2022-07-21T12:34:14.858292Z","iopub.execute_input":"2022-07-21T12:34:14.859692Z","iopub.status.idle":"2022-07-21T12:34:14.868261Z","shell.execute_reply.started":"2022-07-21T12:34:14.859633Z","shell.execute_reply":"2022-07-21T12:34:14.867184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Data_dir = Path(\"../input/home-data-for-ml-course/\")\nos.listdir(Data_dir)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T12:34:14.954282Z","iopub.execute_input":"2022-07-21T12:34:14.955082Z","iopub.status.idle":"2022-07-21T12:34:14.964817Z","shell.execute_reply.started":"2022-07-21T12:34:14.955031Z","shell.execute_reply":"2022-07-21T12:34:14.963489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(Data_dir/'train.csv', index_col=0)\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T12:34:15.034963Z","iopub.execute_input":"2022-07-21T12:34:15.035372Z","iopub.status.idle":"2022-07-21T12:34:15.086599Z","shell.execute_reply.started":"2022-07-21T12:34:15.035341Z","shell.execute_reply":"2022-07-21T12:34:15.085422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T12:34:15.113300Z","iopub.execute_input":"2022-07-21T12:34:15.113724Z","iopub.status.idle":"2022-07-21T12:34:15.132593Z","shell.execute_reply.started":"2022-07-21T12:34:15.113693Z","shell.execute_reply":"2022-07-21T12:34:15.131425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.dropna(inplace=True, axis=1)\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T12:34:15.192042Z","iopub.execute_input":"2022-07-21T12:34:15.195434Z","iopub.status.idle":"2022-07-21T12:34:15.236634Z","shell.execute_reply.started":"2022-07-21T12:34:15.195382Z","shell.execute_reply":"2022-07-21T12:34:15.235393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.describe(include='object')","metadata":{"execution":{"iopub.status.busy":"2022-07-21T12:34:15.268432Z","iopub.execute_input":"2022-07-21T12:34:15.268841Z","iopub.status.idle":"2022-07-21T12:34:15.351000Z","shell.execute_reply.started":"2022-07-21T12:34:15.268811Z","shell.execute_reply":"2022-07-21T12:34:15.349785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numeric_columns = train_df.iloc[:, :-1].select_dtypes(np.number).columns.tolist()\ncategoric_columns = train_df.iloc[:, :-1].select_dtypes(include=['object', 'category']).columns.tolist()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T12:34:15.352647Z","iopub.execute_input":"2022-07-21T12:34:15.353020Z","iopub.status.idle":"2022-07-21T12:34:15.366030Z","shell.execute_reply.started":"2022-07-21T12:34:15.352991Z","shell.execute_reply":"2022-07-21T12:34:15.364911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import StratifiedKFold\nnum_splits = 5\nX = train_df.index.values\ny = train_df['SalePrice'].values\ntrain_df['fold'] = -1\n\nsplitter = StratifiedKFold(num_splits, random_state=0, shuffle=True)\nfor fold, (tr_idx, val_idx) in enumerate(splitter.split(X, y)):\n    train_df.iloc[val_idx, -1] = fold","metadata":{"execution":{"iopub.status.busy":"2022-07-21T12:34:15.412838Z","iopub.execute_input":"2022-07-21T12:34:15.413634Z","iopub.status.idle":"2022-07-21T12:34:15.438001Z","shell.execute_reply.started":"2022-07-21T12:34:15.413592Z","shell.execute_reply":"2022-07-21T12:34:15.436526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['fold'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T12:34:15.474505Z","iopub.execute_input":"2022-07-21T12:34:15.475577Z","iopub.status.idle":"2022-07-21T12:34:15.485339Z","shell.execute_reply.started":"2022-07-21T12:34:15.475452Z","shell.execute_reply":"2022-07-21T12:34:15.483765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tqdm.pandas(desc='Creating validation and train stages')\ntrain_df['stage'] = train_df['fold'].progress_apply(lambda x: 'valid' if x == 1 else 'train')","metadata":{"execution":{"iopub.status.busy":"2022-07-21T12:34:15.529289Z","iopub.execute_input":"2022-07-21T12:34:15.529980Z","iopub.status.idle":"2022-07-21T12:34:15.579653Z","shell.execute_reply.started":"2022-07-21T12:34:15.529937Z","shell.execute_reply":"2022-07-21T12:34:15.578300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nencoder = LabelEncoder()\nfor col in categoric_columns:\n    train_df[col] = encoder.fit_transform(train_df[col])","metadata":{"execution":{"iopub.status.busy":"2022-07-21T12:34:15.593308Z","iopub.execute_input":"2022-07-21T12:34:15.594080Z","iopub.status.idle":"2022-07-21T12:34:15.626730Z","shell.execute_reply.started":"2022-07-21T12:34:15.594040Z","shell.execute_reply":"2022-07-21T12:34:15.624498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_df = train_df[train_df['stage'] == 'valid']\nnew_train_df = train_df[train_df['stage'] == 'train']","metadata":{"execution":{"iopub.status.busy":"2022-07-21T12:34:15.629381Z","iopub.execute_input":"2022-07-21T12:34:15.629797Z","iopub.status.idle":"2022-07-21T12:34:15.640271Z","shell.execute_reply.started":"2022-07-21T12:34:15.629763Z","shell.execute_reply":"2022-07-21T12:34:15.638898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combined_columns = categoric_columns + numeric_columns\ncombined_columns.append('SalePrice')","metadata":{"execution":{"iopub.status.busy":"2022-07-21T12:34:15.671186Z","iopub.execute_input":"2022-07-21T12:34:15.671566Z","iopub.status.idle":"2022-07-21T12:34:15.677085Z","shell.execute_reply.started":"2022-07-21T12:34:15.671537Z","shell.execute_reply":"2022-07-21T12:34:15.675559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_train_df = new_train_df[combined_columns]\nvalid_df = valid_df[combined_columns]\n\nfeatures = new_train_df.columns.tolist()[:-1]\ntarget = 'SalePrice'","metadata":{"execution":{"iopub.status.busy":"2022-07-21T12:34:15.701416Z","iopub.execute_input":"2022-07-21T12:34:15.701849Z","iopub.status.idle":"2022-07-21T12:34:15.709938Z","shell.execute_reply.started":"2022-07-21T12:34:15.701815Z","shell.execute_reply":"2022-07-21T12:34:15.708914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import HistGradientBoostingRegressor\nmodel = HistGradientBoostingRegressor()\nmodel.fit(new_train_df[features], new_train_df[target])\npred_train = model.predict(new_train_df[features])\npred_valid = model.predict(valid_df[features])","metadata":{"execution":{"iopub.status.busy":"2022-07-21T12:34:15.731625Z","iopub.execute_input":"2022-07-21T12:34:15.732074Z","iopub.status.idle":"2022-07-21T12:34:16.431819Z","shell.execute_reply.started":"2022-07-21T12:34:15.732029Z","shell.execute_reply":"2022-07-21T12:34:16.430846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import mean_absolute_error, mean_squared_error\nprint(f\"Train MAE: {mean_absolute_error(new_train_df[target], pred_train):.4f}\")\nprint(f\"Train MSE: {mean_squared_error(new_train_df[target], pred_train):.4f}\")\nprint(\"-----------------------------------------\")\nprint(f\"Valid MAE: {mean_absolute_error(valid_df[target], pred_valid):.4f}\")\nprint(f\"Valid MSE: {mean_squared_error(valid_df[target], pred_valid):.4f}\")","metadata":{"execution":{"iopub.status.busy":"2022-07-21T12:34:16.433867Z","iopub.execute_input":"2022-07-21T12:34:16.434911Z","iopub.status.idle":"2022-07-21T12:34:16.444708Z","shell.execute_reply.started":"2022-07-21T12:34:16.434869Z","shell.execute_reply":"2022-07-21T12:34:16.443753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(Data_dir/'test.csv', index_col=0)\ntest_df['SalePrice'] = -1","metadata":{"execution":{"iopub.status.busy":"2022-07-21T12:35:50.764060Z","iopub.execute_input":"2022-07-21T12:35:50.764539Z","iopub.status.idle":"2022-07-21T12:35:50.796974Z","shell.execute_reply.started":"2022-07-21T12:35:50.764504Z","shell.execute_reply":"2022-07-21T12:35:50.795521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nencoder = LabelEncoder()\nfor col in categoric_columns:\n    test_df[col] = encoder.fit_transform(test_df[col])","metadata":{"execution":{"iopub.status.busy":"2022-07-21T12:36:32.207215Z","iopub.execute_input":"2022-07-21T12:36:32.207613Z","iopub.status.idle":"2022-07-21T12:36:32.246933Z","shell.execute_reply.started":"2022-07-21T12:36:32.207583Z","shell.execute_reply":"2022-07-21T12:36:32.245536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_preds = model.predict(test_df[features])\ntest_df['SalePrice'] = test_preds","metadata":{"execution":{"iopub.status.busy":"2022-07-21T12:37:33.899406Z","iopub.execute_input":"2022-07-21T12:37:33.900264Z","iopub.status.idle":"2022-07-21T12:37:33.930367Z","shell.execute_reply.started":"2022-07-21T12:37:33.900211Z","shell.execute_reply":"2022-07-21T12:37:33.929405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = pd.read_csv(Data_dir/'sample_submission.csv')\nsample['SalePrice'] = test_preds","metadata":{"execution":{"iopub.status.busy":"2022-07-21T12:38:43.780769Z","iopub.execute_input":"2022-07-21T12:38:43.781419Z","iopub.status.idle":"2022-07-21T12:38:43.793843Z","shell.execute_reply.started":"2022-07-21T12:38:43.781385Z","shell.execute_reply":"2022-07-21T12:38:43.792255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample.to_csv('submission.csv', index=False)\npd.read_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-21T12:39:20.212458Z","iopub.execute_input":"2022-07-21T12:39:20.213092Z","iopub.status.idle":"2022-07-21T12:39:20.246663Z","shell.execute_reply.started":"2022-07-21T12:39:20.213044Z","shell.execute_reply":"2022-07-21T12:39:20.245471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}