{"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":"import pandas as pd\nimport numpy as np \n\ntest_path = '../input/house-prices-advanced-regression-techniques/test.csv'\ntrain_path = '../input/house-prices-advanced-regression-techniques/train.csv'\n\ntrain = pd.read_csv(train_path)\ntest = pd.read_csv(test_path)\ntrain.head()","metadata":{"_uuid":"7b9b40da-f26a-40ab-ad7a-937849dd1088","_cell_guid":"fd8240d8-dec5-4c03-8335-7815706ea3c2","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-07-26T13:30:20.587354Z","iopub.execute_input":"2022-07-26T13:30:20.587822Z","iopub.status.idle":"2022-07-26T13:30:20.667996Z","shell.execute_reply.started":"2022-07-26T13:30:20.587785Z","shell.execute_reply":"2022-07-26T13:30:20.666638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.set_index(\"Id\", inplace=True)\ntest.set_index(\"Id\", inplace=True)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:30:20.669913Z","iopub.execute_input":"2022-07-26T13:30:20.670868Z","iopub.status.idle":"2022-07-26T13:30:20.703191Z","shell.execute_reply.started":"2022-07-26T13:30:20.670824Z","shell.execute_reply":"2022-07-26T13:30:20.701778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.shape, test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:30:20.753884Z","iopub.execute_input":"2022-07-26T13:30:20.754578Z","iopub.status.idle":"2022-07-26T13:30:20.760243Z","shell.execute_reply.started":"2022-07-26T13:30:20.754535Z","shell.execute_reply":"2022-07-26T13:30:20.759225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:30:20.823164Z","iopub.execute_input":"2022-07-26T13:30:20.823904Z","iopub.status.idle":"2022-07-26T13:30:20.831362Z","shell.execute_reply.started":"2022-07-26T13:30:20.823861Z","shell.execute_reply":"2022-07-26T13:30:20.829893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:30:20.880727Z","iopub.execute_input":"2022-07-26T13:30:20.881148Z","iopub.status.idle":"2022-07-26T13:30:20.913754Z","shell.execute_reply.started":"2022-07-26T13:30:20.881113Z","shell.execute_reply":"2022-07-26T13:30:20.912589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:30:20.969371Z","iopub.execute_input":"2022-07-26T13:30:20.969802Z","iopub.status.idle":"2022-07-26T13:30:20.998307Z","shell.execute_reply.started":"2022-07-26T13:30:20.969766Z","shell.execute_reply":"2022-07-26T13:30:20.995235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nulls = train.isnull().sum().sort_values(ascending=False)\nnulls.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:30:21.016551Z","iopub.execute_input":"2022-07-26T13:30:21.016931Z","iopub.status.idle":"2022-07-26T13:30:21.038426Z","shell.execute_reply.started":"2022-07-26T13:30:21.016899Z","shell.execute_reply":"2022-07-26T13:30:21.037377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#total nulls\ntrain.isnull().sum().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:30:21.070316Z","iopub.execute_input":"2022-07-26T13:30:21.070926Z","iopub.status.idle":"2022-07-26T13:30:21.091515Z","shell.execute_reply.started":"2022-07-26T13:30:21.070890Z","shell.execute_reply":"2022-07-26T13:30:21.089967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#target\ntrain.SalePrice.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:30:21.114265Z","iopub.execute_input":"2022-07-26T13:30:21.114677Z","iopub.status.idle":"2022-07-26T13:30:21.122770Z","shell.execute_reply.started":"2022-07-26T13:30:21.114642Z","shell.execute_reply":"2022-07-26T13:30:21.121495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#find huge nulls indexes\nhuge_nulls = nulls.head(6).index.tolist()\nhuge_nulls","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:30:21.204296Z","iopub.execute_input":"2022-07-26T13:30:21.204711Z","iopub.status.idle":"2022-07-26T13:30:21.212007Z","shell.execute_reply.started":"2022-07-26T13:30:21.204665Z","shell.execute_reply":"2022-07-26T13:30:21.210677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#drop huge nulls\ntrain.drop(huge_nulls, axis=1, inplace=True)\ntest.drop(huge_nulls, axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:30:21.256001Z","iopub.execute_input":"2022-07-26T13:30:21.256468Z","iopub.status.idle":"2022-07-26T13:30:21.269240Z","shell.execute_reply.started":"2022-07-26T13:30:21.256433Z","shell.execute_reply":"2022-07-26T13:30:21.268157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.shape,test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:30:21.304843Z","iopub.execute_input":"2022-07-26T13:30:21.305439Z","iopub.status.idle":"2022-07-26T13:30:21.310387Z","shell.execute_reply.started":"2022-07-26T13:30:21.305395Z","shell.execute_reply":"2022-07-26T13:30:21.309409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe().T","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:30:21.357886Z","iopub.execute_input":"2022-07-26T13:30:21.358350Z","iopub.status.idle":"2022-07-26T13:30:21.483030Z","shell.execute_reply.started":"2022-07-26T13:30:21.358312Z","shell.execute_reply":"2022-07-26T13:30:21.481796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# DATA PREPROCESSING #\ntarget = train.SalePrice\nfeatures = train.drop('SalePrice', axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:30:21.485131Z","iopub.execute_input":"2022-07-26T13:30:21.485761Z","iopub.status.idle":"2022-07-26T13:30:21.492989Z","shell.execute_reply.started":"2022-07-26T13:30:21.485727Z","shell.execute_reply":"2022-07-26T13:30:21.491720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:30:21.502687Z","iopub.execute_input":"2022-07-26T13:30:21.503386Z","iopub.status.idle":"2022-07-26T13:30:21.532433Z","shell.execute_reply.started":"2022-07-26T13:30:21.503344Z","shell.execute_reply":"2022-07-26T13:30:21.531165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_val, y_train, y_val = train_test_split(features, target, train_size=0.8, random_state=0)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:30:21.559318Z","iopub.execute_input":"2022-07-26T13:30:21.560140Z","iopub.status.idle":"2022-07-26T13:30:21.569815Z","shell.execute_reply.started":"2022-07-26T13:30:21.560087Z","shell.execute_reply":"2022-07-26T13:30:21.568899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"categoricals = [col for col in X_train.columns if X_train[col].nunique() <= 10 and X_train[col].dtype == 'object']\ncategoricals","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:30:21.608577Z","iopub.execute_input":"2022-07-26T13:30:21.610729Z","iopub.status.idle":"2022-07-26T13:30:21.636576Z","shell.execute_reply.started":"2022-07-26T13:30:21.610678Z","shell.execute_reply":"2022-07-26T13:30:21.635665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numericals = [cols for cols in X_train.columns if X_train[cols].dtype in ['int64', 'float64']]\nnumericals","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:30:21.711226Z","iopub.execute_input":"2022-07-26T13:30:21.711880Z","iopub.status.idle":"2022-07-26T13:30:21.722456Z","shell.execute_reply.started":"2022-07-26T13:30:21.711842Z","shell.execute_reply":"2022-07-26T13:30:21.720744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_cols = categoricals+numericals\nmy_cols[0:5]","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:30:21.765539Z","iopub.execute_input":"2022-07-26T13:30:21.765949Z","iopub.status.idle":"2022-07-26T13:30:21.776616Z","shell.execute_reply.started":"2022-07-26T13:30:21.765917Z","shell.execute_reply":"2022-07-26T13:30:21.774785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train[my_cols]\nX_valid = X_val[my_cols]","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:30:21.808934Z","iopub.execute_input":"2022-07-26T13:30:21.810112Z","iopub.status.idle":"2022-07-26T13:30:21.817865Z","shell.execute_reply.started":"2022-07-26T13:30:21.810055Z","shell.execute_reply":"2022-07-26T13:30:21.816544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# PIPE LINE #\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.preprocessing import OneHotEncoder","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:30:21.851983Z","iopub.execute_input":"2022-07-26T13:30:21.852407Z","iopub.status.idle":"2022-07-26T13:30:21.858286Z","shell.execute_reply.started":"2022-07-26T13:30:21.852375Z","shell.execute_reply":"2022-07-26T13:30:21.856986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numerical_pipeline = Pipeline(steps=[\n    ('imputer', SimpleImputer(strategy='median')),\n    ('scaler', StandardScaler())\n])","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:30:21.928468Z","iopub.execute_input":"2022-07-26T13:30:21.929764Z","iopub.status.idle":"2022-07-26T13:30:21.935858Z","shell.execute_reply.started":"2022-07-26T13:30:21.929721Z","shell.execute_reply":"2022-07-26T13:30:21.934240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"categorical_pipeline = Pipeline(steps=[\n    ('imputer', SimpleImputer(strategy='most_frequent')),\n    ('OHE', OneHotEncoder(handle_unknown='ignore'))\n])","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:30:21.991068Z","iopub.execute_input":"2022-07-26T13:30:21.991710Z","iopub.status.idle":"2022-07-26T13:30:21.996942Z","shell.execute_reply.started":"2022-07-26T13:30:21.991674Z","shell.execute_reply":"2022-07-26T13:30:21.995550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.compose import ColumnTransformer\npreprocessor = ColumnTransformer(transformers=[\n    ('num', numerical_pipeline, numericals),\n    ('cat', categorical_pipeline, categoricals)\n])","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:30:22.038694Z","iopub.execute_input":"2022-07-26T13:30:22.039700Z","iopub.status.idle":"2022-07-26T13:30:22.045128Z","shell.execute_reply.started":"2022-07-26T13:30:22.039653Z","shell.execute_reply":"2022-07-26T13:30:22.044115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# MODEL BUILDING # \nfrom sklearn.ensemble import RandomForestRegressor\nrf = RandomForestRegressor(n_estimators=150, random_state=0)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:30:22.109639Z","iopub.execute_input":"2022-07-26T13:30:22.110610Z","iopub.status.idle":"2022-07-26T13:30:22.116119Z","shell.execute_reply.started":"2022-07-26T13:30:22.110571Z","shell.execute_reply":"2022-07-26T13:30:22.114935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_pipeline = Pipeline(steps=[\n    ('preprocessor', preprocessor),\n    ('model', rf)\n])","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:30:22.264212Z","iopub.execute_input":"2022-07-26T13:30:22.264829Z","iopub.status.idle":"2022-07-26T13:30:22.269592Z","shell.execute_reply.started":"2022-07-26T13:30:22.264793Z","shell.execute_reply":"2022-07-26T13:30:22.268569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_pipeline.fit(X_train, y_train)\nmy_pipeline","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:30:22.304042Z","iopub.execute_input":"2022-07-26T13:30:22.304728Z","iopub.status.idle":"2022-07-26T13:30:25.787408Z","shell.execute_reply.started":"2022-07-26T13:30:22.304679Z","shell.execute_reply":"2022-07-26T13:30:25.786051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = my_pipeline.predict(X_valid)\npreds[0:5]","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:30:25.789695Z","iopub.execute_input":"2022-07-26T13:30:25.790043Z","iopub.status.idle":"2022-07-26T13:30:25.840421Z","shell.execute_reply.started":"2022-07-26T13:30:25.790012Z","shell.execute_reply":"2022-07-26T13:30:25.839036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import mean_absolute_error\nprint(f'MAE: {mean_absolute_error(y_val, preds)}')","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:30:25.841976Z","iopub.execute_input":"2022-07-26T13:30:25.842426Z","iopub.status.idle":"2022-07-26T13:30:25.849601Z","shell.execute_reply.started":"2022-07-26T13:30:25.842379Z","shell.execute_reply":"2022-07-26T13:30:25.848342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# CROSS VALIDATION #\nfrom sklearn.model_selection import cross_val_score\nscores = cross_val_score(my_pipeline, features, target, cv=7, scoring='neg_mean_absolute_error')\nscores = scores *-1","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:30:25.851792Z","iopub.execute_input":"2022-07-26T13:30:25.852865Z","iopub.status.idle":"2022-07-26T13:30:51.582573Z","shell.execute_reply.started":"2022-07-26T13:30:25.852825Z","shell.execute_reply":"2022-07-26T13:30:51.581308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores.mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:30:51.584293Z","iopub.execute_input":"2022-07-26T13:30:51.584660Z","iopub.status.idle":"2022-07-26T13:30:51.591491Z","shell.execute_reply.started":"2022-07-26T13:30:51.584626Z","shell.execute_reply":"2022-07-26T13:30:51.590403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import mean_absolute_percentage_error\nprint(f'MAPE: {mean_absolute_percentage_error(y_val, preds)*100:.2f}%')","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:30:51.592813Z","iopub.execute_input":"2022-07-26T13:30:51.593171Z","iopub.status.idle":"2022-07-26T13:30:51.605227Z","shell.execute_reply.started":"2022-07-26T13:30:51.593139Z","shell.execute_reply":"2022-07-26T13:30:51.604261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_preds = my_pipeline.predict(test)\noutput = pd.DataFrame({'Id': test.index,\n                       'SalePrice': test_preds})\noutput.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T13:30:51.606430Z","iopub.execute_input":"2022-07-26T13:30:51.606951Z","iopub.status.idle":"2022-07-26T13:30:51.720364Z","shell.execute_reply.started":"2022-07-26T13:30:51.606915Z","shell.execute_reply":"2022-07-26T13:30:51.719038Z"},"trusted":true},"execution_count":null,"outputs":[]}]}