{"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\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport seaborn as sns\nfrom sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score\nfrom sklearn.preprocessing import OneHotEncoder, RobustScaler, StandardScaler\nfrom sklearn.metrics import mean_absolute_error, mean_squared_error, mean_absolute_percentage_error\nfrom sklearn.impute import KNNImputer, SimpleImputer\nfrom sklearn.linear_model import LinearRegression, Ridge, Lasso\nfrom sklearn.ensemble import RandomForestRegressor, AdaBoostClassifier\nfrom xgboost import XGBRegressor\n#conda install lightgbm\nfrom lightgbm import LGBMRegressor\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"id":"qToOtTHZ7ysG","execution":{"iopub.status.busy":"2022-08-06T16:25:17.395774Z","iopub.execute_input":"2022-08-06T16:25:17.397023Z","iopub.status.idle":"2022-08-06T16:25:17.405552Z","shell.execute_reply.started":"2022-08-06T16:25:17.396975Z","shell.execute_reply":"2022-08-06T16:25:17.404503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\ndef get_csv(filename):\n    path = \"../input/house-prices-advanced-regression-techniques\"\n    path = os.path.join(path, filename)\n    return pd.read_csv(path)","metadata":{"id":"dcWh1v678Ic8","execution":{"iopub.status.busy":"2022-08-06T16:25:17.406990Z","iopub.execute_input":"2022-08-06T16:25:17.408137Z","iopub.status.idle":"2022-08-06T16:25:17.419284Z","shell.execute_reply.started":"2022-08-06T16:25:17.408084Z","shell.execute_reply":"2022-08-06T16:25:17.417989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading Data and Initial Analysis","metadata":{"id":"x8HoWppt-oCk"}},{"cell_type":"code","source":"data = get_csv(\"train.csv\")\ntest = get_csv(\"test.csv\")","metadata":{"id":"VCKukaUt-sDo","execution":{"iopub.status.busy":"2022-08-06T16:25:17.420682Z","iopub.execute_input":"2022-08-06T16:25:17.421204Z","iopub.status.idle":"2022-08-06T16:25:17.466759Z","shell.execute_reply.started":"2022-08-06T16:25:17.421171Z","shell.execute_reply":"2022-08-06T16:25:17.465879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data","metadata":{"id":"aZ-51fiX-8k3","outputId":"d98fa996-da95-4d26-e035-29f0ad471887","execution":{"iopub.status.busy":"2022-08-06T16:25:17.654739Z","iopub.execute_input":"2022-08-06T16:25:17.656134Z","iopub.status.idle":"2022-08-06T16:25:17.682845Z","shell.execute_reply.started":"2022-08-06T16:25:17.656086Z","shell.execute_reply":"2022-08-06T16:25:17.681731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"id":"qBA4o4g6_H8l","outputId":"73bce17b-81e1-44cf-f0c0-d94b885f5acd","execution":{"iopub.status.busy":"2022-08-06T16:25:17.895427Z","iopub.execute_input":"2022-08-06T16:25:17.896214Z","iopub.status.idle":"2022-08-06T16:25:17.919584Z","shell.execute_reply.started":"2022-08-06T16:25:17.896174Z","shell.execute_reply":"2022-08-06T16:25:17.919004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.describe().T","metadata":{"id":"d3wqTbPc_PB8","outputId":"436f07b0-8216-491f-dc61-0feceed68239","execution":{"iopub.status.busy":"2022-08-06T16:25:17.920752Z","iopub.execute_input":"2022-08-06T16:25:17.921081Z","iopub.status.idle":"2022-08-06T16:25:18.016248Z","shell.execute_reply.started":"2022-08-06T16:25:17.921060Z","shell.execute_reply":"2022-08-06T16:25:18.015073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:18.017582Z","iopub.execute_input":"2022-08-06T16:25:18.017801Z","iopub.status.idle":"2022-08-06T16:25:18.023844Z","shell.execute_reply.started":"2022-08-06T16:25:18.017780Z","shell.execute_reply":"2022-08-06T16:25:18.022859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['Id'].nunique()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:18.025766Z","iopub.execute_input":"2022-08-06T16:25:18.026103Z","iopub.status.idle":"2022-08-06T16:25:18.038884Z","shell.execute_reply.started":"2022-08-06T16:25:18.026079Z","shell.execute_reply":"2022-08-06T16:25:18.037778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"As all IDs are unique and are of no much use for us, we can drop it","metadata":{}},{"cell_type":"code","source":"data_mul = data.copy()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:18.040215Z","iopub.execute_input":"2022-08-06T16:25:18.040652Z","iopub.status.idle":"2022-08-06T16:25:18.049135Z","shell.execute_reply.started":"2022-08-06T16:25:18.040628Z","shell.execute_reply":"2022-08-06T16:25:18.048028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = pd.DataFrame()\npredictions['Id'] = test['Id']","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:18.050483Z","iopub.execute_input":"2022-08-06T16:25:18.050830Z","iopub.status.idle":"2022-08-06T16:25:18.060431Z","shell.execute_reply.started":"2022-08-06T16:25:18.050796Z","shell.execute_reply":"2022-08-06T16:25:18.059001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = data.drop('Id', axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:18.061573Z","iopub.execute_input":"2022-08-06T16:25:18.061815Z","iopub.status.idle":"2022-08-06T16:25:18.077018Z","shell.execute_reply.started":"2022-08-06T16:25:18.061793Z","shell.execute_reply":"2022-08-06T16:25:18.075497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:18.078359Z","iopub.execute_input":"2022-08-06T16:25:18.078625Z","iopub.status.idle":"2022-08-06T16:25:18.090498Z","shell.execute_reply.started":"2022-08-06T16:25:18.078602Z","shell.execute_reply":"2022-08-06T16:25:18.089198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test.drop('Id', axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:18.092402Z","iopub.execute_input":"2022-08-06T16:25:18.092612Z","iopub.status.idle":"2022-08-06T16:25:18.102515Z","shell.execute_reply.started":"2022-08-06T16:25:18.092591Z","shell.execute_reply":"2022-08-06T16:25:18.101439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:18.104822Z","iopub.execute_input":"2022-08-06T16:25:18.105547Z","iopub.status.idle":"2022-08-06T16:25:18.114939Z","shell.execute_reply.started":"2022-08-06T16:25:18.105522Z","shell.execute_reply":"2022-08-06T16:25:18.114254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data[data['YearBuilt']<1900]['YearBuilt']","metadata":{"id":"czpPoTTV_g5t","outputId":"027287a7-24e7-43d9-f87f-10bcbe16bc06","execution":{"iopub.status.busy":"2022-08-06T16:25:18.116538Z","iopub.execute_input":"2022-08-06T16:25:18.117687Z","iopub.status.idle":"2022-08-06T16:25:18.129368Z","shell.execute_reply.started":"2022-08-06T16:25:18.117648Z","shell.execute_reply":"2022-08-06T16:25:18.128757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data[(data['YearBuilt']== 1985) | (data['YearBuilt']== 1885)][['YearBuilt','SalePrice','YrSold']]","metadata":{"id":"8pfuPozh_9f8","outputId":"da8345f9-f6b5-46ce-9013-be1371c8f645","execution":{"iopub.status.busy":"2022-08-06T16:25:18.150562Z","iopub.execute_input":"2022-08-06T16:25:18.151582Z","iopub.status.idle":"2022-08-06T16:25:18.161779Z","shell.execute_reply.started":"2022-08-06T16:25:18.151541Z","shell.execute_reply":"2022-08-06T16:25:18.161174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data[(data['YearBuilt']== 1872) | (data['YearBuilt']== 1972)][['YearBuilt','SalePrice','YrSold']]","metadata":{"id":"CYKmsjiqBA4M","outputId":"e98ad614-ec7a-4c86-9359-f9fab6a36f41","execution":{"iopub.status.busy":"2022-08-06T16:25:18.163241Z","iopub.execute_input":"2022-08-06T16:25:18.163851Z","iopub.status.idle":"2022-08-06T16:25:18.175722Z","shell.execute_reply.started":"2022-08-06T16:25:18.163824Z","shell.execute_reply":"2022-08-06T16:25:18.175131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['YearBuilt'].describe()","metadata":{"id":"J6THW-vWBrOq","outputId":"bb1a1744-f4bc-4c0d-a2c2-3775fc362275","execution":{"iopub.status.busy":"2022-08-06T16:25:18.176774Z","iopub.execute_input":"2022-08-06T16:25:18.177573Z","iopub.status.idle":"2022-08-06T16:25:18.191083Z","shell.execute_reply.started":"2022-08-06T16:25:18.177545Z","shell.execute_reply":"2022-08-06T16:25:18.190308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There were few records of houses being built in 1800s. In test too there are records of houses built in 1800s. So we cannot diregard them as incorrect. We'll carry on without any change\n\n\n","metadata":{"id":"aRcoWsl8CI2y"}},{"cell_type":"code","source":"data[data['LotArea'] > 100000]","metadata":{"id":"SdidSz25CdcP","outputId":"f012028e-6ad4-4ddf-faa0-83767f63d201","execution":{"iopub.status.busy":"2022-08-06T16:25:18.192172Z","iopub.execute_input":"2022-08-06T16:25:18.193033Z","iopub.status.idle":"2022-08-06T16:25:18.216288Z","shell.execute_reply.started":"2022-08-06T16:25:18.193007Z","shell.execute_reply":"2022-08-06T16:25:18.215616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data[data['SalePrice'] >= 300000][['LotArea','SalePrice']]","metadata":{"id":"WMJGpoamDhZS","outputId":"d90b4816-c335-448c-e51c-5972808b54bd","execution":{"iopub.status.busy":"2022-08-06T16:25:18.218474Z","iopub.execute_input":"2022-08-06T16:25:18.218847Z","iopub.status.idle":"2022-08-06T16:25:18.228463Z","shell.execute_reply.started":"2022-08-06T16:25:18.218823Z","shell.execute_reply":"2022-08-06T16:25:18.227763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There are few records with high LotArea and high SalePrices. There can be many reasons for them.","metadata":{"id":"QwqX_zMBEt4M"}},{"cell_type":"code","source":"data[['1stFlrSF','2ndFlrSF','GrLivArea']]","metadata":{"id":"1gHHhaibEAQN","execution":{"iopub.status.busy":"2022-08-06T16:25:18.229455Z","iopub.execute_input":"2022-08-06T16:25:18.230059Z","iopub.status.idle":"2022-08-06T16:25:18.239407Z","shell.execute_reply.started":"2022-08-06T16:25:18.230034Z","shell.execute_reply":"2022-08-06T16:25:18.238659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data[['BsmtFinSF1','BsmtFinSF2','BsmtUnfSF','TotalBsmtSF']]","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:18.240667Z","iopub.execute_input":"2022-08-06T16:25:18.241296Z","iopub.status.idle":"2022-08-06T16:25:18.252408Z","shell.execute_reply.started":"2022-08-06T16:25:18.241262Z","shell.execute_reply":"2022-08-06T16:25:18.251662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"As we can see above GrLivArea equals 1stFlrSF + 2ndFlrSF","metadata":{}},{"cell_type":"markdown","source":"Similar is the case with TotalBsmtSF","metadata":{}},{"cell_type":"markdown","source":"We can drop the rest columns as they can be represented by one consolidated column and including all might create multicollinearity","metadata":{}},{"cell_type":"markdown","source":"Multicollinearity is a statistical concept where several independent variables in a model are correlated","metadata":{}},{"cell_type":"code","source":"data = data.drop(['1stFlrSF','2ndFlrSF','BsmtFinSF1','BsmtFinSF2','BsmtUnfSF'], axis= 1)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:18.253210Z","iopub.execute_input":"2022-08-06T16:25:18.253895Z","iopub.status.idle":"2022-08-06T16:25:18.267131Z","shell.execute_reply.started":"2022-08-06T16:25:18.253865Z","shell.execute_reply":"2022-08-06T16:25:18.265940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test.drop(['1stFlrSF','2ndFlrSF','BsmtFinSF1','BsmtFinSF2','BsmtUnfSF'], axis= 1)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:18.268339Z","iopub.execute_input":"2022-08-06T16:25:18.268672Z","iopub.status.idle":"2022-08-06T16:25:18.278030Z","shell.execute_reply.started":"2022-08-06T16:25:18.268650Z","shell.execute_reply":"2022-08-06T16:25:18.277388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## *Null Values*","metadata":{"id":"sDuL2gYuFY0J"}},{"cell_type":"code","source":"def missing_values(df):\n  '''\n  To print the count and percentage of missing values. Values with zero counts have been removed\n  '''\n  sum = df.isna().sum().sort_values(ascending= False)[df.isna().sum().sort_values(ascending= False) != 0]\n  percent = (sum/len(df))*100\n  return pd.concat([sum,percent],axis=1, keys=['Sum','Percent'])","metadata":{"id":"yxaNixO6FpNA","execution":{"iopub.status.busy":"2022-08-06T16:25:18.279001Z","iopub.execute_input":"2022-08-06T16:25:18.279356Z","iopub.status.idle":"2022-08-06T16:25:18.290135Z","shell.execute_reply.started":"2022-08-06T16:25:18.279334Z","shell.execute_reply":"2022-08-06T16:25:18.289219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing_values(data)","metadata":{"id":"Tqkq4ESxHyjn","outputId":"c11e7e9a-b2fb-4f45-8c90-97ba2cc0cb52","execution":{"iopub.status.busy":"2022-08-06T16:25:18.291267Z","iopub.execute_input":"2022-08-06T16:25:18.291480Z","iopub.status.idle":"2022-08-06T16:25:18.313805Z","shell.execute_reply.started":"2022-08-06T16:25:18.291459Z","shell.execute_reply":"2022-08-06T16:25:18.312896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Test Data missing values')\nmissing_values(test)","metadata":{"id":"HzRAbdEGKdXF","outputId":"1eb55b34-cacb-4738-f6bb-85690d18effb","execution":{"iopub.status.busy":"2022-08-06T16:25:18.315049Z","iopub.execute_input":"2022-08-06T16:25:18.315597Z","iopub.status.idle":"2022-08-06T16:25:18.335485Z","shell.execute_reply.started":"2022-08-06T16:25:18.315561Z","shell.execute_reply":"2022-08-06T16:25:18.334431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['MasVnrType'].value_counts()","metadata":{"id":"GvsuzNt07wjx","outputId":"5689f32e-d46b-43f7-a17f-2491d8b66d11","execution":{"iopub.status.busy":"2022-08-06T16:25:18.336530Z","iopub.execute_input":"2022-08-06T16:25:18.336741Z","iopub.status.idle":"2022-08-06T16:25:18.345286Z","shell.execute_reply.started":"2022-08-06T16:25:18.336720Z","shell.execute_reply":"2022-08-06T16:25:18.344405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['MasVnrType'].unique()","metadata":{"id":"DLs80U7abRqw","outputId":"70db516c-5417-413a-b2b5-eebd06b028fa","execution":{"iopub.status.busy":"2022-08-06T16:25:18.350904Z","iopub.execute_input":"2022-08-06T16:25:18.351358Z","iopub.status.idle":"2022-08-06T16:25:18.359772Z","shell.execute_reply.started":"2022-08-06T16:25:18.351335Z","shell.execute_reply":"2022-08-06T16:25:18.358772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There are some records where the category is None given as NA, those are not the null records.\n\n      \n      ","metadata":{"id":"Mp6WGBt671hP"}},{"cell_type":"code","source":"na_value_cols = ['Alley','MasVnrType','BsmtQual','BsmtCond','BsmtExposure','BsmtFinType1','BsmtFinType2','FireplaceQu',\n                 'GarageType','GarageFinish','GarageQual','GarageCond','PoolQC','Fence','MiscFeature']","metadata":{"id":"jf4yoBDx8Aax","execution":{"iopub.status.busy":"2022-08-06T16:25:18.361125Z","iopub.execute_input":"2022-08-06T16:25:18.361542Z","iopub.status.idle":"2022-08-06T16:25:18.373011Z","shell.execute_reply.started":"2022-08-06T16:25:18.361519Z","shell.execute_reply":"2022-08-06T16:25:18.372014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def none_values(df):\n  '''\n  To impute None values wherever there is categorical with NA values\n  '''\n  na_value_col = ['Alley','Fence','MiscFeature']\n  garage_col = ['GarageType','GarageFinish','GarageQual','GarageCond']\n  bsmt_col = ['BsmtQual','BsmtCond','BsmtExposure','BsmtFinType1','BsmtFinType2']\n  \n  for col in na_value_col:\n    df[col] = df[col].fillna('None')\n  \n  for i in range (0,len(df)):\n    \n    if (df.loc[i,'GarageArea']==0):\n      for col in garage_col:\n        df.loc[i,col] = 'None'\n    \n    if (df.loc[i,'Fireplaces']==0):\n      df.loc[i,'FireplaceQu'] = 'None'\n    \n    if (df.loc[i,'TotalBsmtSF']==0):\n      for col in bsmt_col:\n        df.loc[i,col] = 'None'\n    \n    if (df.loc[i,'MasVnrArea']==0):\n      df.loc[i,'MasVnrType'] = 'None'\n    \n    if (df.loc[i,'PoolArea']==0):\n      df.loc[i,'PoolQC'] = 'None'","metadata":{"id":"hYytemfTASHd","execution":{"iopub.status.busy":"2022-08-06T16:25:18.374361Z","iopub.execute_input":"2022-08-06T16:25:18.374880Z","iopub.status.idle":"2022-08-06T16:25:18.385649Z","shell.execute_reply.started":"2022-08-06T16:25:18.374852Z","shell.execute_reply":"2022-08-06T16:25:18.384389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"none_values(data)\nnone_values(test)","metadata":{"id":"XnizwblyBZzQ","execution":{"iopub.status.busy":"2022-08-06T16:25:18.386931Z","iopub.execute_input":"2022-08-06T16:25:18.387376Z","iopub.status.idle":"2022-08-06T16:25:20.376553Z","shell.execute_reply.started":"2022-08-06T16:25:18.387339Z","shell.execute_reply":"2022-08-06T16:25:20.375828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing_values(data)","metadata":{"id":"WnKpWf-jBc0v","outputId":"de126d72-e45b-4482-9f18-24947f1860cc","execution":{"iopub.status.busy":"2022-08-06T16:25:20.377456Z","iopub.execute_input":"2022-08-06T16:25:20.377851Z","iopub.status.idle":"2022-08-06T16:25:20.396043Z","shell.execute_reply.started":"2022-08-06T16:25:20.377828Z","shell.execute_reply":"2022-08-06T16:25:20.395288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing_values(test)","metadata":{"id":"-CzO7bDqDI8H","outputId":"4c5b872d-c526-4645-9ba3-7fc72c747cff","execution":{"iopub.status.busy":"2022-08-06T16:25:20.397048Z","iopub.execute_input":"2022-08-06T16:25:20.397930Z","iopub.status.idle":"2022-08-06T16:25:20.416444Z","shell.execute_reply.started":"2022-08-06T16:25:20.397880Z","shell.execute_reply":"2022-08-06T16:25:20.415673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_col = ['MSSubClass','MSZoning','Street','Alley','LotShape','LandContour','Utilities','LotConfig','LandSlope','Neighborhood',\n           'Condition1','Condition2','BldgType','HouseStyle','OverallQual','OverallCond','RoofStyle','RoofMatl',\n           'Exterior1st','Exterior2nd','MasVnrType','ExterQual','ExterCond','Foundation','BsmtQual','BsmtCond','BsmtExposure','BsmtFinType1',\n           'BsmtFinType2','Heating','HeatingQC','CentralAir','Electrical','BsmtFullBath','BsmtHalfBath','FullBath',\n           'HalfBath','BedroomAbvGr','KitchenAbvGr','KitchenQual','TotRmsAbvGrd','Functional','Fireplaces','FireplaceQu',\n           'GarageType','GarageFinish','GarageQual','GarageCond','PavedDrive','PoolQC','Fence','MiscFeature','MoSold',\n           'SaleCondition','SaleType']","metadata":{"id":"HZX4im8EHNEX","execution":{"iopub.status.busy":"2022-08-06T16:25:20.417696Z","iopub.execute_input":"2022-08-06T16:25:20.418111Z","iopub.status.idle":"2022-08-06T16:25:20.422554Z","shell.execute_reply.started":"2022-08-06T16:25:20.418086Z","shell.execute_reply":"2022-08-06T16:25:20.422008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_col = ['LotFrontage','LotArea','YearBuilt','YearRemodAdd','MasVnrArea','TotalBsmtSF','LowQualFinSF',\n           'GrLivArea','GarageYrBlt','GarageCars','GarageArea','WoodDeckSF',\n           'OpenPorchSF','EnclosedPorch','3SsnPorch','ScreenPorch','PoolArea','MiscVal','YrSold']","metadata":{"id":"LSp8pnwWINiF","execution":{"iopub.status.busy":"2022-08-06T16:25:20.423480Z","iopub.execute_input":"2022-08-06T16:25:20.424010Z","iopub.status.idle":"2022-08-06T16:25:20.436773Z","shell.execute_reply.started":"2022-08-06T16:25:20.423941Z","shell.execute_reply":"2022-08-06T16:25:20.435863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"map_col = ['LotShape','Utilities','LandSlope','OverallQual','OverallCond','ExterQual',\n           'ExterCond','BsmtQual','BsmtCond','BsmtExposure','BsmtFinType1','BsmtFinType2','HeatingQC','BsmtFullBath',\n           'BsmtHalfBath','FullBath','HalfBath','BedroomAbvGr','KitchenAbvGr','KitchenQual','TotRmsAbvGrd',\n           'Functional','Fireplaces','FireplaceQu','GarageFinish','GarageQual','GarageCond',\n           'PoolQC']","metadata":{"id":"Yqu69B8j0V6Q","execution":{"iopub.status.busy":"2022-08-06T16:25:20.437944Z","iopub.execute_input":"2022-08-06T16:25:20.438318Z","iopub.status.idle":"2022-08-06T16:25:20.448078Z","shell.execute_reply.started":"2022-08-06T16:25:20.438294Z","shell.execute_reply":"2022-08-06T16:25:20.447321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ohe_col = list(set(cat_col) - set(map_col))","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:20.449044Z","iopub.execute_input":"2022-08-06T16:25:20.449271Z","iopub.status.idle":"2022-08-06T16:25:20.464119Z","shell.execute_reply.started":"2022-08-06T16:25:20.449247Z","shell.execute_reply":"2022-08-06T16:25:20.463123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ohe_col","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:20.465410Z","iopub.execute_input":"2022-08-06T16:25:20.465943Z","iopub.status.idle":"2022-08-06T16:25:20.475971Z","shell.execute_reply.started":"2022-08-06T16:25:20.465917Z","shell.execute_reply":"2022-08-06T16:25:20.475138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We can see for houses without basement or garage some of the numerical values are being displayed as NA values. These should be zero as garage or basement is not available","metadata":{"id":"xz6VmFuKABlJ"}},{"cell_type":"code","source":"data[(data['GarageArea']==0) | (data['TotalBsmtSF']==0)]['GarageYrBlt']","metadata":{"id":"9duMqwyCASI-","outputId":"b16326bd-8f1c-4cba-e26e-3efcf8e9c8eb","execution":{"iopub.status.busy":"2022-08-06T16:25:20.476903Z","iopub.execute_input":"2022-08-06T16:25:20.477582Z","iopub.status.idle":"2022-08-06T16:25:20.489968Z","shell.execute_reply.started":"2022-08-06T16:25:20.477559Z","shell.execute_reply":"2022-08-06T16:25:20.488908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#to impute 0 instead of null values as it should be NONE.\ndef zero_values(df):\n    bsmt_col = ['BsmtFullBath','BsmtHalfBath']\n    garage_col = ['GarageYrBlt','GarageCars']\n    \n    for i in range(0,len(df)):\n        if(df.loc[i,'GarageArea']==0):\n            for col in garage_col:\n                df.loc[i,col] = 0\n        \n        if (df.loc[i,'TotalBsmtSF']==0):\n            for col in bsmt_col:\n                df.loc[i,col] = 0\n        \n        if (df[df.index == i][['MasVnrType','MasVnrArea']].isnull()['MasVnrType'][i] == True) & (df[df.index == i][['MasVnrType','MasVnrArea']].isnull()['MasVnrArea'][i] == True):\n            df.loc[i,'MasVnrType'] = 'None'\n            df.loc[i,'MasVnrArea'] = 0\n            ","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:20.491027Z","iopub.execute_input":"2022-08-06T16:25:20.491305Z","iopub.status.idle":"2022-08-06T16:25:20.498804Z","shell.execute_reply.started":"2022-08-06T16:25:20.491276Z","shell.execute_reply":"2022-08-06T16:25:20.498255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zero_values(data)","metadata":{"id":"cXsnEF9zDJIM","execution":{"iopub.status.busy":"2022-08-06T16:25:20.499882Z","iopub.execute_input":"2022-08-06T16:25:20.500142Z","iopub.status.idle":"2022-08-06T16:25:22.860838Z","shell.execute_reply.started":"2022-08-06T16:25:20.500121Z","shell.execute_reply":"2022-08-06T16:25:22.860199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zero_values(test)","metadata":{"id":"Zn2PMuM1DX4n","execution":{"iopub.status.busy":"2022-08-06T16:25:22.861971Z","iopub.execute_input":"2022-08-06T16:25:22.862197Z","iopub.status.idle":"2022-08-06T16:25:25.261889Z","shell.execute_reply.started":"2022-08-06T16:25:22.862176Z","shell.execute_reply":"2022-08-06T16:25:25.261268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing_values(data)","metadata":{"id":"alYnHjqQDMvf","outputId":"268716de-fa63-4852-aa04-2b6b03fc3784","execution":{"iopub.status.busy":"2022-08-06T16:25:25.262752Z","iopub.execute_input":"2022-08-06T16:25:25.263234Z","iopub.status.idle":"2022-08-06T16:25:25.280187Z","shell.execute_reply.started":"2022-08-06T16:25:25.263209Z","shell.execute_reply":"2022-08-06T16:25:25.279227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing_values(test)","metadata":{"id":"z2wcfXIzDRSb","outputId":"4b9b5f00-ba37-4a30-9d9b-aab41877a690","execution":{"iopub.status.busy":"2022-08-06T16:25:25.281421Z","iopub.execute_input":"2022-08-06T16:25:25.281638Z","iopub.status.idle":"2022-08-06T16:25:25.301435Z","shell.execute_reply.started":"2022-08-06T16:25:25.281617Z","shell.execute_reply":"2022-08-06T16:25:25.300457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualisations","metadata":{}},{"cell_type":"code","source":"data1 = data[data.notna()].copy()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:25.302612Z","iopub.execute_input":"2022-08-06T16:25:25.302943Z","iopub.status.idle":"2022-08-06T16:25:25.317149Z","shell.execute_reply.started":"2022-08-06T16:25:25.302912Z","shell.execute_reply":"2022-08-06T16:25:25.316230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cor_mat = data1.corr()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:25.318367Z","iopub.execute_input":"2022-08-06T16:25:25.318700Z","iopub.status.idle":"2022-08-06T16:25:25.326757Z","shell.execute_reply.started":"2022-08-06T16:25:25.318653Z","shell.execute_reply":"2022-08-06T16:25:25.326007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,10))\nsns.heatmap(cor_mat)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:25.328237Z","iopub.execute_input":"2022-08-06T16:25:25.329211Z","iopub.status.idle":"2022-08-06T16:25:25.951920Z","shell.execute_reply.started":"2022-08-06T16:25:25.329165Z","shell.execute_reply":"2022-08-06T16:25:25.951318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n\n\nLets look into the relationships which have shown higher correlation with Sale Price\n\n\n","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10,7))\nsns.scatterplot(x='EnclosedPorch', y='SalePrice', data=data1)\nplt.title('Enclosed Porch vs Sale Price')\nplt.xlabel('Enclosed Area')\nplt.ylabel('Sale Price')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:25.953172Z","iopub.execute_input":"2022-08-06T16:25:25.953566Z","iopub.status.idle":"2022-08-06T16:25:26.129732Z","shell.execute_reply.started":"2022-08-06T16:25:25.953542Z","shell.execute_reply":"2022-08-06T16:25:26.128791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,7))\nsns.boxplot(x='KitchenAbvGr',y='SalePrice', data=data1)\nplt.title('Number of Kitchen vs Sale Price')\nplt.xlabel('Number of Kitchen above ground')\nplt.ylabel('Sale Price')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:26.130963Z","iopub.execute_input":"2022-08-06T16:25:26.131190Z","iopub.status.idle":"2022-08-06T16:25:26.306802Z","shell.execute_reply.started":"2022-08-06T16:25:26.131167Z","shell.execute_reply":"2022-08-06T16:25:26.305556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n\n\n\nIn the Enclosed Porch plot we can see that there are many 0 values and for the Kitchen above ground plot the values mostly have value 1 and 2.\nAs values have been concentrated in these features the correlation values were high for them","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10,7))\nsns.displot(x='SalePrice', data=data1, aspect=2)\nplt.title(\"Distribution of Sale Price\")","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:26.307839Z","iopub.execute_input":"2022-08-06T16:25:26.308088Z","iopub.status.idle":"2022-08-06T16:25:26.713181Z","shell.execute_reply.started":"2022-08-06T16:25:26.308066Z","shell.execute_reply":"2022-08-06T16:25:26.712296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,7))\nsns.scatterplot(x='LotArea', y='SalePrice', data=data1)\nplt.title('Lot Area vs Sale Price')\nplt.xlabel('Lot Area')\nplt.ylabel('Sale Price')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:26.715051Z","iopub.execute_input":"2022-08-06T16:25:26.715347Z","iopub.status.idle":"2022-08-06T16:25:26.880255Z","shell.execute_reply.started":"2022-08-06T16:25:26.715318Z","shell.execute_reply":"2022-08-06T16:25:26.879145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,7))\nsns.scatterplot(x='LotFrontage', y='SalePrice', data=data1)\nplt.title('Lot Frontage vs Sale Price')\nplt.xlabel('Lot Frontage')\nplt.ylabel('Sale Price')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:26.881413Z","iopub.execute_input":"2022-08-06T16:25:26.881637Z","iopub.status.idle":"2022-08-06T16:25:27.059405Z","shell.execute_reply.started":"2022-08-06T16:25:26.881612Z","shell.execute_reply":"2022-08-06T16:25:27.058472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"As we can see Lot Frontage has some clear relation with the Sale Price, also its correlation was significant.\nBut there are quite a large number of missing values for Lot Frontage. \nWe cannot just drop the column or impute values with mean or median","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(12,7))\ndata1.groupby('YrSold')['SalePrice'].median().plot()\nplt.title('Year Sold vs Sale Price')\nplt.xlabel('Year Sold')\nplt.ylabel('Sale Price')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:27.060271Z","iopub.execute_input":"2022-08-06T16:25:27.060487Z","iopub.status.idle":"2022-08-06T16:25:27.239435Z","shell.execute_reply.started":"2022-08-06T16:25:27.060465Z","shell.execute_reply":"2022-08-06T16:25:27.238422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,7))\ndata1.groupby('YearBuilt')['SalePrice'].median().plot()\nplt.title('Year Built vs Sale Price')\nplt.xlabel('Year Built')\nplt.ylabel('Sale Price')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:27.240298Z","iopub.execute_input":"2022-08-06T16:25:27.240521Z","iopub.status.idle":"2022-08-06T16:25:27.403190Z","shell.execute_reply.started":"2022-08-06T16:25:27.240500Z","shell.execute_reply":"2022-08-06T16:25:27.402572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,7))\ndata1.groupby('YearRemodAdd')['SalePrice'].median().plot()\nplt.title('Year Remodeled vs Sale Price')\nplt.xlabel('Year ReBuilt')\nplt.ylabel('Sale Price')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:27.411776Z","iopub.execute_input":"2022-08-06T16:25:27.412624Z","iopub.status.idle":"2022-08-06T16:25:27.558141Z","shell.execute_reply.started":"2022-08-06T16:25:27.412599Z","shell.execute_reply":"2022-08-06T16:25:27.557005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,7))\nsns.scatterplot(x='TotalBsmtSF', y='SalePrice', data=data1)\nplt.title('Basement Area vs Sale Price')\nplt.xlabel('Basement Area')\nplt.ylabel('Sale Price')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:27.559428Z","iopub.execute_input":"2022-08-06T16:25:27.559802Z","iopub.status.idle":"2022-08-06T16:25:27.732285Z","shell.execute_reply.started":"2022-08-06T16:25:27.559769Z","shell.execute_reply":"2022-08-06T16:25:27.731247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,7))\nsns.scatterplot(x='GarageArea', y='SalePrice', data=data1)\nplt.title('Garage Area vs Sale Price')\nplt.xlabel('Garage Area')\nplt.ylabel('Sale Price')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:27.733827Z","iopub.execute_input":"2022-08-06T16:25:27.734163Z","iopub.status.idle":"2022-08-06T16:25:27.905782Z","shell.execute_reply.started":"2022-08-06T16:25:27.734136Z","shell.execute_reply":"2022-08-06T16:25:27.904765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,7))\nsns.scatterplot(x='GrLivArea', y='SalePrice', data=data1)\nplt.title('Living Area vs Sale Price')\nplt.xlabel('Living Area')\nplt.ylabel('Sale Price')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:27.907163Z","iopub.execute_input":"2022-08-06T16:25:27.907512Z","iopub.status.idle":"2022-08-06T16:25:28.076980Z","shell.execute_reply.started":"2022-08-06T16:25:27.907480Z","shell.execute_reply":"2022-08-06T16:25:28.073724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,7))\nsns.boxplot(x='HouseStyle', y='SalePrice', data=data1)\nplt.title('House style vs Sales Price')\nplt.xlabel('House Style')\nplt.ylabel('Sale Price')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:28.078395Z","iopub.execute_input":"2022-08-06T16:25:28.078746Z","iopub.status.idle":"2022-08-06T16:25:28.315183Z","shell.execute_reply.started":"2022-08-06T16:25:28.078715Z","shell.execute_reply":"2022-08-06T16:25:28.313982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,7))\nsns.boxplot(x='OverallQual', y='SalePrice', data=data1)\nplt.title('Overall House Quality vs Sales Price')\nplt.xlabel('House Quality Rating')\nplt.ylabel('Sale Price')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:28.316559Z","iopub.execute_input":"2022-08-06T16:25:28.316795Z","iopub.status.idle":"2022-08-06T16:25:28.553267Z","shell.execute_reply.started":"2022-08-06T16:25:28.316772Z","shell.execute_reply":"2022-08-06T16:25:28.552510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,7))\nsns.boxplot(x='BedroomAbvGr', y='SalePrice', data=data1)\nplt.title('Bedrooms above basement vs Sales Price')\nplt.xlabel('Number of Bedrooms')\nplt.ylabel('Sale Price')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:28.554265Z","iopub.execute_input":"2022-08-06T16:25:28.554511Z","iopub.status.idle":"2022-08-06T16:25:28.770381Z","shell.execute_reply.started":"2022-08-06T16:25:28.554489Z","shell.execute_reply":"2022-08-06T16:25:28.769742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,7))\nsns.boxplot(x='TotRmsAbvGrd', y='SalePrice', data=data1)\nplt.title('Total Rooms above basement vs Sales Price')\nplt.xlabel('Number of Rooms')\nplt.ylabel('Sale Price')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:28.771348Z","iopub.execute_input":"2022-08-06T16:25:28.772119Z","iopub.status.idle":"2022-08-06T16:25:29.029466Z","shell.execute_reply.started":"2022-08-06T16:25:28.772093Z","shell.execute_reply":"2022-08-06T16:25:29.028578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,7))\nsns.boxplot(x='FullBath', y='SalePrice', data=data1)\nplt.title('Full Bath Rooms above basement vs Sales Price')\nplt.xlabel('Number of BathRooms')\nplt.ylabel('Sale Price')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:29.030460Z","iopub.execute_input":"2022-08-06T16:25:29.030768Z","iopub.status.idle":"2022-08-06T16:25:29.198413Z","shell.execute_reply.started":"2022-08-06T16:25:29.030738Z","shell.execute_reply":"2022-08-06T16:25:29.196690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,7))\nsns.boxplot(x='HalfBath', y='SalePrice', data=data1)\nplt.title('Half Bath Rooms above basement vs Sales Price')\nplt.xlabel('Number of Half BathRooms')\nplt.ylabel('Sale Price')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:29.199722Z","iopub.execute_input":"2022-08-06T16:25:29.200065Z","iopub.status.idle":"2022-08-06T16:25:29.358192Z","shell.execute_reply.started":"2022-08-06T16:25:29.200034Z","shell.execute_reply":"2022-08-06T16:25:29.357178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,7))\nsns.boxplot(x='GarageCars', y='SalePrice', data=data1)\nplt.title('Garage Cars vs Sales Price')\nplt.xlabel('Number of Cars')\nplt.ylabel('Sale Price')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:29.359390Z","iopub.execute_input":"2022-08-06T16:25:29.359598Z","iopub.status.idle":"2022-08-06T16:25:29.546072Z","shell.execute_reply.started":"2022-08-06T16:25:29.359576Z","shell.execute_reply":"2022-08-06T16:25:29.545215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,7))\nsns.scatterplot(x='LotArea', y='SalePrice',hue= 'BedroomAbvGr',palette='deep', data=data1)\nplt.title('Lot Area vs Sale Price')\nplt.xlabel('Lot Area')\nplt.ylabel('Sale Price')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:29.547803Z","iopub.execute_input":"2022-08-06T16:25:29.548097Z","iopub.status.idle":"2022-08-06T16:25:30.011083Z","shell.execute_reply.started":"2022-08-06T16:25:29.548074Z","shell.execute_reply":"2022-08-06T16:25:30.009525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nsns.catplot(x='OverallQual', y='SalePrice',hue='TotRmsAbvGrd', data=data1, aspect=2)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:30.012552Z","iopub.execute_input":"2022-08-06T16:25:30.012805Z","iopub.status.idle":"2022-08-06T16:25:31.816232Z","shell.execute_reply.started":"2022-08-06T16:25:30.012783Z","shell.execute_reply":"2022-08-06T16:25:31.815165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.catplot(x='OverallQual', y='SalePrice',hue='GarageCars', data=data1, aspect=2)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:31.817671Z","iopub.execute_input":"2022-08-06T16:25:31.818419Z","iopub.status.idle":"2022-08-06T16:25:32.753379Z","shell.execute_reply.started":"2022-08-06T16:25:31.818368Z","shell.execute_reply":"2022-08-06T16:25:32.752193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.catplot(x='OverallQual', y='SalePrice',hue='HouseStyle', data=data1, aspect=2)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:32.754515Z","iopub.execute_input":"2022-08-06T16:25:32.754772Z","iopub.status.idle":"2022-08-06T16:25:34.065451Z","shell.execute_reply.started":"2022-08-06T16:25:32.754749Z","shell.execute_reply":"2022-08-06T16:25:34.064477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train Test Split","metadata":{}},{"cell_type":"code","source":"X = data.drop('SalePrice', axis=1)\ny = data['SalePrice']","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.066309Z","iopub.execute_input":"2022-08-06T16:25:34.066519Z","iopub.status.idle":"2022-08-06T16:25:34.073199Z","shell.execute_reply.started":"2022-08-06T16:25:34.066497Z","shell.execute_reply":"2022-08-06T16:25:34.071882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.3, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.074173Z","iopub.execute_input":"2022-08-06T16:25:34.074443Z","iopub.status.idle":"2022-08-06T16:25:34.087124Z","shell.execute_reply.started":"2022-08-06T16:25:34.074421Z","shell.execute_reply":"2022-08-06T16:25:34.086001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We already have the test data file so we make this split as train and validation as we'll be training and tuning our models based on the validation data errors","metadata":{}},{"cell_type":"markdown","source":"# Imputations","metadata":{}},{"cell_type":"code","source":"imputer_cat = SimpleImputer(strategy= 'most_frequent')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.088370Z","iopub.execute_input":"2022-08-06T16:25:34.089354Z","iopub.status.idle":"2022-08-06T16:25:34.094754Z","shell.execute_reply.started":"2022-08-06T16:25:34.089322Z","shell.execute_reply":"2022-08-06T16:25:34.094172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imputer_cat.fit(X_train[cat_col])","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.096092Z","iopub.execute_input":"2022-08-06T16:25:34.096932Z","iopub.status.idle":"2022-08-06T16:25:34.119552Z","shell.execute_reply.started":"2022-08-06T16:25:34.096900Z","shell.execute_reply":"2022-08-06T16:25:34.118867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train[cat_col] = imputer_cat.transform(X_train[cat_col])","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.120812Z","iopub.execute_input":"2022-08-06T16:25:34.121696Z","iopub.status.idle":"2022-08-06T16:25:34.135663Z","shell.execute_reply.started":"2022-08-06T16:25:34.121661Z","shell.execute_reply":"2022-08-06T16:25:34.135070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_val[cat_col] = imputer_cat.transform(X_val[cat_col])","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.136724Z","iopub.execute_input":"2022-08-06T16:25:34.138174Z","iopub.status.idle":"2022-08-06T16:25:34.150693Z","shell.execute_reply.started":"2022-08-06T16:25:34.138139Z","shell.execute_reply":"2022-08-06T16:25:34.149330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[cat_col] = imputer_cat.transform(test[cat_col])","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.152116Z","iopub.execute_input":"2022-08-06T16:25:34.154444Z","iopub.status.idle":"2022-08-06T16:25:34.173815Z","shell.execute_reply.started":"2022-08-06T16:25:34.154384Z","shell.execute_reply":"2022-08-06T16:25:34.172480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing_values(X_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.175064Z","iopub.execute_input":"2022-08-06T16:25:34.175298Z","iopub.status.idle":"2022-08-06T16:25:34.193858Z","shell.execute_reply.started":"2022-08-06T16:25:34.175276Z","shell.execute_reply":"2022-08-06T16:25:34.192614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"For LotFrontage more than 18 percent of the data is missing. As this feature is quite correlated to target we cannot simply drop it.","metadata":{}},{"cell_type":"markdown","source":"Also we cannot just use SimpleImputer with median. As all missing data will be filled with median values","metadata":{}},{"cell_type":"markdown","source":"So here we will try the KNN Imputer.","metadata":{}},{"cell_type":"markdown","source":"With KNN Imputer for each sample’s missing values are imputed using the mean value from n_neighbors nearest neighbors found in the training set. Two samples are close if the features that neither is missing are close.","metadata":{}},{"cell_type":"markdown","source":"KNN Imputer only takes the numerical data, so we'll train it on training numerical data","metadata":{}},{"cell_type":"code","source":"imputer_num = KNNImputer(n_neighbors=2, weights= 'distance')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.195022Z","iopub.execute_input":"2022-08-06T16:25:34.195324Z","iopub.status.idle":"2022-08-06T16:25:34.199511Z","shell.execute_reply.started":"2022-08-06T16:25:34.195300Z","shell.execute_reply":"2022-08-06T16:25:34.198618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imputer_num.fit(X_train[num_col])","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.200611Z","iopub.execute_input":"2022-08-06T16:25:34.200982Z","iopub.status.idle":"2022-08-06T16:25:34.215595Z","shell.execute_reply.started":"2022-08-06T16:25:34.200942Z","shell.execute_reply":"2022-08-06T16:25:34.214683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train[num_col] = imputer_num.transform(X_train[num_col])","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.216926Z","iopub.execute_input":"2022-08-06T16:25:34.217638Z","iopub.status.idle":"2022-08-06T16:25:34.245120Z","shell.execute_reply.started":"2022-08-06T16:25:34.217613Z","shell.execute_reply":"2022-08-06T16:25:34.244375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_val[num_col] = imputer_num.transform(X_val[num_col])","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.246490Z","iopub.execute_input":"2022-08-06T16:25:34.246982Z","iopub.status.idle":"2022-08-06T16:25:34.265500Z","shell.execute_reply.started":"2022-08-06T16:25:34.246933Z","shell.execute_reply":"2022-08-06T16:25:34.264761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[num_col] = imputer_num.transform(test[num_col])","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.266835Z","iopub.execute_input":"2022-08-06T16:25:34.267328Z","iopub.status.idle":"2022-08-06T16:25:34.295852Z","shell.execute_reply.started":"2022-08-06T16:25:34.267298Z","shell.execute_reply":"2022-08-06T16:25:34.295120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing_values(X_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.297189Z","iopub.execute_input":"2022-08-06T16:25:34.297645Z","iopub.status.idle":"2022-08-06T16:25:34.322415Z","shell.execute_reply.started":"2022-08-06T16:25:34.297616Z","shell.execute_reply":"2022-08-06T16:25:34.321694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing_values(X_val)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.323775Z","iopub.execute_input":"2022-08-06T16:25:34.324295Z","iopub.status.idle":"2022-08-06T16:25:34.344600Z","shell.execute_reply.started":"2022-08-06T16:25:34.324262Z","shell.execute_reply":"2022-08-06T16:25:34.343877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing_values(test)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.346107Z","iopub.execute_input":"2022-08-06T16:25:34.346570Z","iopub.status.idle":"2022-08-06T16:25:34.370339Z","shell.execute_reply.started":"2022-08-06T16:25:34.346539Z","shell.execute_reply":"2022-08-06T16:25:34.369598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dummification","metadata":{}},{"cell_type":"markdown","source":"## One Hot Encoding","metadata":{}},{"cell_type":"code","source":"ohe = OneHotEncoder(handle_unknown='ignore', drop='first')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.371658Z","iopub.execute_input":"2022-08-06T16:25:34.372153Z","iopub.status.idle":"2022-08-06T16:25:34.375965Z","shell.execute_reply.started":"2022-08-06T16:25:34.372122Z","shell.execute_reply":"2022-08-06T16:25:34.375261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ohe.fit(X_train[ohe_col])","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.377393Z","iopub.execute_input":"2022-08-06T16:25:34.377704Z","iopub.status.idle":"2022-08-06T16:25:34.397701Z","shell.execute_reply.started":"2022-08-06T16:25:34.377676Z","shell.execute_reply":"2022-08-06T16:25:34.396769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"onehot_col = list(ohe.get_feature_names(ohe_col))\nprint(onehot_col)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.399923Z","iopub.execute_input":"2022-08-06T16:25:34.400341Z","iopub.status.idle":"2022-08-06T16:25:34.404726Z","shell.execute_reply.started":"2022-08-06T16:25:34.400317Z","shell.execute_reply":"2022-08-06T16:25:34.403833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_ohe = ohe.transform(X_train[ohe_col])\nX_val_ohe = ohe.transform(X_val[ohe_col])","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.406048Z","iopub.execute_input":"2022-08-06T16:25:34.406490Z","iopub.status.idle":"2022-08-06T16:25:34.439822Z","shell.execute_reply.started":"2022-08-06T16:25:34.406466Z","shell.execute_reply":"2022-08-06T16:25:34.439119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ohe = ohe.transform(test[ohe_col])","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.440709Z","iopub.execute_input":"2022-08-06T16:25:34.441243Z","iopub.status.idle":"2022-08-06T16:25:34.464429Z","shell.execute_reply.started":"2022-08-06T16:25:34.441219Z","shell.execute_reply":"2022-08-06T16:25:34.462443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_ohe = pd.DataFrame(X_train_ohe.todense(), columns= onehot_col)\nX_val_ohe = pd.DataFrame(X_val_ohe.todense(), columns= onehot_col)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.466013Z","iopub.execute_input":"2022-08-06T16:25:34.466471Z","iopub.status.idle":"2022-08-06T16:25:34.474687Z","shell.execute_reply.started":"2022-08-06T16:25:34.466429Z","shell.execute_reply":"2022-08-06T16:25:34.473317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ohe = pd.DataFrame(test_ohe.todense(), columns= onehot_col)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.475688Z","iopub.execute_input":"2022-08-06T16:25:34.478887Z","iopub.status.idle":"2022-08-06T16:25:34.485334Z","shell.execute_reply.started":"2022-08-06T16:25:34.478855Z","shell.execute_reply":"2022-08-06T16:25:34.484015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There are few features which gives the ratings of some kind","metadata":{}},{"cell_type":"markdown","source":"For such rating feature we'll use Ordinal Encoder","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Ordinal Encoding","metadata":{}},{"cell_type":"markdown","source":"Ordinal data is a kind of categorical data with a set order or scale to it.","metadata":{}},{"cell_type":"markdown","source":"The below columns represent some sort of ratings or grades. Thus they have some order among them.","metadata":{}},{"cell_type":"markdown","source":"Hence we need to encode them as per their order","metadata":{}},{"cell_type":"code","source":"for col in map_col:\n    print(col, X_train[col].unique())","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.486678Z","iopub.execute_input":"2022-08-06T16:25:34.486915Z","iopub.status.idle":"2022-08-06T16:25:34.503822Z","shell.execute_reply.started":"2022-08-06T16:25:34.486894Z","shell.execute_reply":"2022-08-06T16:25:34.502409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We'll map the above categories as per ordinal mapping","metadata":{}},{"cell_type":"code","source":"qual = {'None': 0, 'Po': 1, 'Fa': 2, 'TA': 3, 'Gd': 4, 'Ex': 5}\nlot = {'Reg':4, 'IR1': 3, 'IR2': 2, 'IR3': 1}\nutilities = {'AllPub':4, 'NoSeWa': 3, 'NoSeWa': 2, 'ELO': 1}\nlandslope = {'Gtl': 3, 'Mod': 2, 'Sev': 1}\nbsmtexposure = {'Gd': 4, 'Av': 3,'Mn': 2, 'No': 1,'None': 0}\nbsmtfintype = {'GLQ': 6, 'ALQ': 5, 'BLQ': 4, 'Rec': 3, 'LwQ': 2, 'Unf': 1, 'None': 0}\nfunctional = {'Typ': 8, 'Min1': 7, 'Min2': 6, 'Mod': 5, 'Maj1': 4, 'Maj2': 3, 'Sev': 2, 'Sal': 1}\ngaragefinish = {'Fin': 3,'RFn': 2,'Unf': 1, 'None': 0}","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.505339Z","iopub.execute_input":"2022-08-06T16:25:34.506321Z","iopub.status.idle":"2022-08-06T16:25:34.514612Z","shell.execute_reply.started":"2022-08-06T16:25:34.506285Z","shell.execute_reply":"2022-08-06T16:25:34.513203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Ordinal(df):\n    qual_col = ['ExterQual','ExterCond','BsmtQual','BsmtCond','HeatingQC','KitchenQual','FireplaceQu',\n               'GarageQual','GarageCond','PoolQC']\n    for col in qual_col:\n        df[col] = df[col].map(qual)\n    \n    df['LotShape'] = df['LotShape'].map(lot)\n    df['Utilities'] = df['Utilities'].map(utilities)\n    df['LandSlope'] = df['LandSlope'].map(landslope)\n    df['BsmtExposure'] = df['BsmtExposure'].map(bsmtexposure)\n    df['BsmtFinType1'] = df['BsmtFinType1'].map(bsmtfintype)\n    df['BsmtFinType2'] = df['BsmtFinType2'].map(bsmtfintype)\n    df['Functional'] = df['Functional'].map(functional)\n    df['GarageFinish'] = df['GarageFinish'].map(garagefinish)\n    \n    return df","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.516070Z","iopub.execute_input":"2022-08-06T16:25:34.517153Z","iopub.status.idle":"2022-08-06T16:25:34.526898Z","shell.execute_reply.started":"2022-08-06T16:25:34.517118Z","shell.execute_reply":"2022-08-06T16:25:34.526123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_map = Ordinal(X_train[map_col])\nX_val_map = Ordinal(X_val[map_col])","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.529701Z","iopub.execute_input":"2022-08-06T16:25:34.530579Z","iopub.status.idle":"2022-08-06T16:25:34.564489Z","shell.execute_reply.started":"2022-08-06T16:25:34.530544Z","shell.execute_reply":"2022-08-06T16:25:34.563765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_map = Ordinal(test[map_col])","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.565794Z","iopub.execute_input":"2022-08-06T16:25:34.566555Z","iopub.status.idle":"2022-08-06T16:25:34.586261Z","shell.execute_reply.started":"2022-08-06T16:25:34.566522Z","shell.execute_reply":"2022-08-06T16:25:34.585327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_map = X_train_map.reset_index(drop=True)\nX_val_map = X_val_map.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.587849Z","iopub.execute_input":"2022-08-06T16:25:34.588110Z","iopub.status.idle":"2022-08-06T16:25:34.593598Z","shell.execute_reply.started":"2022-08-06T16:25:34.588087Z","shell.execute_reply":"2022-08-06T16:25:34.592773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_map = test_map.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.594594Z","iopub.execute_input":"2022-08-06T16:25:34.596821Z","iopub.status.idle":"2022-08-06T16:25:34.609039Z","shell.execute_reply.started":"2022-08-06T16:25:34.596773Z","shell.execute_reply":"2022-08-06T16:25:34.608326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in X_train_map.columns:\n    print(col, X_train_map[col].unique())","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.610055Z","iopub.execute_input":"2022-08-06T16:25:34.610404Z","iopub.status.idle":"2022-08-06T16:25:34.627123Z","shell.execute_reply.started":"2022-08-06T16:25:34.610382Z","shell.execute_reply":"2022-08-06T16:25:34.626469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The corresponding mapping has been done","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Scaling","metadata":{}},{"cell_type":"markdown","source":"As we had already seen before that there few reords with high values.","metadata":{}},{"cell_type":"markdown","source":"These are the houses with high areas. But these values are differnt that the usual values(outliers).","metadata":{}},{"cell_type":"markdown","source":"But they are the actual values of large house so we cannot just drop them treating as outliers. We need to check.","metadata":{}},{"cell_type":"markdown","source":"With such large values Scaling might be affected. So we'll use the Robust Scaler","metadata":{}},{"cell_type":"markdown","source":"Robust Scaler removes the median and scales the data according to the quantile range ","metadata":{}},{"cell_type":"code","source":"scaling = RobustScaler()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.628401Z","iopub.execute_input":"2022-08-06T16:25:34.629054Z","iopub.status.idle":"2022-08-06T16:25:34.632733Z","shell.execute_reply.started":"2022-08-06T16:25:34.629023Z","shell.execute_reply":"2022-08-06T16:25:34.632186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaling.fit(X_train[num_col])","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.633614Z","iopub.execute_input":"2022-08-06T16:25:34.634375Z","iopub.status.idle":"2022-08-06T16:25:34.651328Z","shell.execute_reply.started":"2022-08-06T16:25:34.634348Z","shell.execute_reply":"2022-08-06T16:25:34.649996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_num = pd.DataFrame(scaling.transform(X_train[num_col]), columns= num_col)\nX_val_num = pd.DataFrame(scaling.transform(X_val[num_col]), columns= num_col)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.652753Z","iopub.execute_input":"2022-08-06T16:25:34.653109Z","iopub.status.idle":"2022-08-06T16:25:34.662754Z","shell.execute_reply.started":"2022-08-06T16:25:34.653077Z","shell.execute_reply":"2022-08-06T16:25:34.661644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_num = pd.DataFrame(scaling.transform(test[num_col]), columns= num_col)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.663939Z","iopub.execute_input":"2022-08-06T16:25:34.664476Z","iopub.status.idle":"2022-08-06T16:25:34.673060Z","shell.execute_reply.started":"2022-08-06T16:25:34.664441Z","shell.execute_reply":"2022-08-06T16:25:34.672065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = pd.concat([X_train_map, X_train_num, X_train_ohe], axis= 1)\nX_val = pd.concat([X_val_map, X_val_num, X_val_ohe], axis= 1)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.674873Z","iopub.execute_input":"2022-08-06T16:25:34.675262Z","iopub.status.idle":"2022-08-06T16:25:34.688379Z","shell.execute_reply.started":"2022-08-06T16:25:34.675237Z","shell.execute_reply":"2022-08-06T16:25:34.687208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.concat([test_map, test_num, test_ohe], axis= 1)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.689354Z","iopub.execute_input":"2022-08-06T16:25:34.689991Z","iopub.status.idle":"2022-08-06T16:25:34.698117Z","shell.execute_reply.started":"2022-08-06T16:25:34.689933Z","shell.execute_reply":"2022-08-06T16:25:34.697366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.699363Z","iopub.execute_input":"2022-08-06T16:25:34.699799Z","iopub.status.idle":"2022-08-06T16:25:34.725459Z","shell.execute_reply.started":"2022-08-06T16:25:34.699775Z","shell.execute_reply":"2022-08-06T16:25:34.724471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.select_dtypes('object').columns","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.726482Z","iopub.execute_input":"2022-08-06T16:25:34.726738Z","iopub.status.idle":"2022-08-06T16:25:34.735792Z","shell.execute_reply.started":"2022-08-06T16:25:34.726715Z","shell.execute_reply":"2022-08-06T16:25:34.734675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"object_col = ['OverallQual', 'OverallCond', 'BsmtFullBath', 'BsmtHalfBath','FullBath', 'HalfBath', 'BedroomAbvGr',\n              'KitchenAbvGr', 'TotRmsAbvGrd','Fireplaces']","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.736761Z","iopub.execute_input":"2022-08-06T16:25:34.737013Z","iopub.status.idle":"2022-08-06T16:25:34.748834Z","shell.execute_reply.started":"2022-08-06T16:25:34.736991Z","shell.execute_reply":"2022-08-06T16:25:34.748225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in object_col:\n    print(col, X_train[col].unique())","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.750369Z","iopub.execute_input":"2022-08-06T16:25:34.750807Z","iopub.status.idle":"2022-08-06T16:25:34.766541Z","shell.execute_reply.started":"2022-08-06T16:25:34.750783Z","shell.execute_reply":"2022-08-06T16:25:34.764736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train[map_col] = X_train[map_col].astype('int64')\nX_val[map_col] = X_val[map_col].astype('int64')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.768424Z","iopub.execute_input":"2022-08-06T16:25:34.769176Z","iopub.status.idle":"2022-08-06T16:25:34.791903Z","shell.execute_reply.started":"2022-08-06T16:25:34.769144Z","shell.execute_reply":"2022-08-06T16:25:34.791026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[map_col] = test[map_col].astype('int64')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.793560Z","iopub.execute_input":"2022-08-06T16:25:34.793863Z","iopub.status.idle":"2022-08-06T16:25:34.807600Z","shell.execute_reply.started":"2022-08-06T16:25:34.793840Z","shell.execute_reply":"2022-08-06T16:25:34.805767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Building","metadata":{}},{"cell_type":"code","source":"def eval(y_train, y_train_pred, y_val, y_val_pred):\n    \n    print(f\"The Mean Absolute Error on train dataset: {mean_absolute_error(y_pred= y_train_pred, y_true= y_train):.3f} \\n\")\n    print(f\"The Mean Absolute Error on validation dataset: {mean_absolute_error(y_pred= y_val_pred, y_true= y_val):.3f} \\n\\n\")\n\n    print(f\"The Mean Squared Error on train dataset: {mean_squared_error(y_pred= y_train_pred, y_true= y_train):.3f} \\n\")\n    print(f\"The Mean Squared Error on validation dataset: {mean_squared_error(y_pred= y_val_pred, y_true= y_val):.3f} \\n\\n\")\n\n    print(f\"The Root Mean Squared Error on train dataset: {np.sqrt(mean_squared_error(y_pred= y_train_pred, y_true= y_train)):.3f} \\n\")\n    print(f\"The Root Mean Squared Error on validation dataset: {np.sqrt(mean_squared_error(y_pred= y_val_pred, y_true= y_val)):.3f} \\n\\n\")\n\n    print(f\"The Mean Absolute Percentage Error on train dataset: {mean_absolute_percentage_error(y_pred= y_train_pred, y_true= y_train)*100:.3f} \\n\")\n    print(f\"The Mean Absolute Percentage Error on validation dataset: {mean_absolute_percentage_error(y_pred= y_val_pred, y_true= y_val)*100:.3f} \\n\")","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.810285Z","iopub.execute_input":"2022-08-06T16:25:34.810648Z","iopub.status.idle":"2022-08-06T16:25:34.816515Z","shell.execute_reply.started":"2022-08-06T16:25:34.810613Z","shell.execute_reply":"2022-08-06T16:25:34.815848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = pd.DataFrame(columns=['Model', 'Train_MAE', 'Val_MAE', 'Train_MSE','Val_MSE', 'Train_RMSE', 'Val_RMSE',\n                               'Train_MAPE', 'Val_MAPE'])\n\ndef get_metrics(y_train, y_train_pred, y_val, y_val_pred, model_description, dataframe):\n\n    train_mae = round(mean_absolute_error(y_true= y_train, y_pred= y_train_pred),3)\n    train_mse = round(mean_squared_error(y_true= y_train, y_pred= y_train_pred),3)\n    train_rmse = round(np.sqrt(mean_squared_error(y_true= y_train, y_pred= y_train_pred)),3)\n    train_mape = round(mean_absolute_percentage_error(y_true= y_train, y_pred= y_train_pred)*100,3)\n    \n    val_mae = round(mean_absolute_error(y_true= y_val, y_pred= y_val_pred),3)\n    val_mse = round(mean_squared_error(y_true= y_val, y_pred= y_val_pred),3)\n    val_rmse = round(np.sqrt(mean_squared_error(y_true= y_val, y_pred= y_val_pred)),3)\n    val_mape = round(mean_absolute_percentage_error(y_true= y_val, y_pred= y_val_pred)*100,3)\n\n    dataframe = dataframe.append(pd.Series([model_description, train_mae, val_mae, train_mse, val_mse,\n                                            train_rmse, val_rmse, train_mape, val_mape],index=scores.columns ), \n                                 ignore_index=True)\n\n    return(dataframe)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.817550Z","iopub.execute_input":"2022-08-06T16:25:34.817922Z","iopub.status.idle":"2022-08-06T16:25:34.834320Z","shell.execute_reply.started":"2022-08-06T16:25:34.817899Z","shell.execute_reply":"2022-08-06T16:25:34.833492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Linear Regression ","metadata":{}},{"cell_type":"code","source":"linear_reg = LinearRegression()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.835356Z","iopub.execute_input":"2022-08-06T16:25:34.835815Z","iopub.status.idle":"2022-08-06T16:25:34.849629Z","shell.execute_reply.started":"2022-08-06T16:25:34.835793Z","shell.execute_reply":"2022-08-06T16:25:34.848986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"linear_reg.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.850645Z","iopub.execute_input":"2022-08-06T16:25:34.851081Z","iopub.status.idle":"2022-08-06T16:25:34.893335Z","shell.execute_reply.started":"2022-08-06T16:25:34.851053Z","shell.execute_reply":"2022-08-06T16:25:34.892594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scoring = 'neg_mean_squared_error'","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.894408Z","iopub.execute_input":"2022-08-06T16:25:34.894812Z","iopub.status.idle":"2022-08-06T16:25:34.898539Z","shell.execute_reply.started":"2022-08-06T16:25:34.894785Z","shell.execute_reply":"2022-08-06T16:25:34.897564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.sqrt(-cross_val_score(linear_reg, X_train, y_train, cv=5, scoring= 'neg_mean_squared_error'))","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:34.901587Z","iopub.execute_input":"2022-08-06T16:25:34.902047Z","iopub.status.idle":"2022-08-06T16:25:35.164254Z","shell.execute_reply.started":"2022-08-06T16:25:34.902018Z","shell.execute_reply":"2022-08-06T16:25:35.163324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"9.13939514e+13","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:35.165519Z","iopub.execute_input":"2022-08-06T16:25:35.166845Z","iopub.status.idle":"2022-08-06T16:25:35.173858Z","shell.execute_reply.started":"2022-08-06T16:25:35.166814Z","shell.execute_reply":"2022-08-06T16:25:35.173206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_pred = linear_reg.predict(X_train)\ny_val_pred = linear_reg.predict(X_val)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:35.175013Z","iopub.execute_input":"2022-08-06T16:25:35.176446Z","iopub.status.idle":"2022-08-06T16:25:35.198794Z","shell.execute_reply.started":"2022-08-06T16:25:35.176415Z","shell.execute_reply":"2022-08-06T16:25:35.198058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eval(y_train, y_train_pred, y_val, y_val_pred)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-08-06T16:25:35.200177Z","iopub.execute_input":"2022-08-06T16:25:35.201443Z","iopub.status.idle":"2022-08-06T16:25:35.212040Z","shell.execute_reply.started":"2022-08-06T16:25:35.201411Z","shell.execute_reply":"2022-08-06T16:25:35.211330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = get_metrics(y_train, y_train_pred, y_val, y_val_pred, 'Linear_Regression', scores)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:35.213653Z","iopub.execute_input":"2022-08-06T16:25:35.214255Z","iopub.status.idle":"2022-08-06T16:25:35.230924Z","shell.execute_reply.started":"2022-08-06T16:25:35.214225Z","shell.execute_reply":"2022-08-06T16:25:35.229903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:35.232214Z","iopub.execute_input":"2022-08-06T16:25:35.232485Z","iopub.status.idle":"2022-08-06T16:25:35.256582Z","shell.execute_reply.started":"2022-08-06T16:25:35.232458Z","shell.execute_reply":"2022-08-06T16:25:35.255591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We can see that the model is not able to generalize quite a bit on validation set.","metadata":{}},{"cell_type":"markdown","source":"We have used all the features, lets see by applying regularization on them","metadata":{}},{"cell_type":"markdown","source":"So we'll now check with Lasso and Ridge which uses regularization","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## LASSO ","metadata":{}},{"cell_type":"code","source":"lasso = Lasso()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:35.257703Z","iopub.execute_input":"2022-08-06T16:25:35.258009Z","iopub.status.idle":"2022-08-06T16:25:35.266440Z","shell.execute_reply.started":"2022-08-06T16:25:35.257981Z","shell.execute_reply":"2022-08-06T16:25:35.265484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"param_grid = {'alpha': np.linspace(1e-5, 500, 40)}","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:35.268294Z","iopub.execute_input":"2022-08-06T16:25:35.269338Z","iopub.status.idle":"2022-08-06T16:25:35.279133Z","shell.execute_reply.started":"2022-08-06T16:25:35.269302Z","shell.execute_reply":"2022-08-06T16:25:35.278303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lasso_grid = GridSearchCV(lasso, param_grid = param_grid, scoring= scoring, cv=3, n_jobs= -1)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:35.280418Z","iopub.execute_input":"2022-08-06T16:25:35.280868Z","iopub.status.idle":"2022-08-06T16:25:35.288852Z","shell.execute_reply.started":"2022-08-06T16:25:35.280839Z","shell.execute_reply":"2022-08-06T16:25:35.288160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lasso_grid.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:35.290132Z","iopub.execute_input":"2022-08-06T16:25:35.290644Z","iopub.status.idle":"2022-08-06T16:25:37.727038Z","shell.execute_reply.started":"2022-08-06T16:25:35.290615Z","shell.execute_reply":"2022-08-06T16:25:37.726253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lasso_grid.best_params_","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:37.730431Z","iopub.execute_input":"2022-08-06T16:25:37.732230Z","iopub.status.idle":"2022-08-06T16:25:37.740134Z","shell.execute_reply.started":"2022-08-06T16:25:37.732190Z","shell.execute_reply":"2022-08-06T16:25:37.739422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_pred = lasso_grid.predict(X_train)\ny_val_pred = lasso_grid.predict(X_val)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:37.743698Z","iopub.execute_input":"2022-08-06T16:25:37.744357Z","iopub.status.idle":"2022-08-06T16:25:37.770912Z","shell.execute_reply.started":"2022-08-06T16:25:37.744325Z","shell.execute_reply":"2022-08-06T16:25:37.770137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eval(y_train, y_train_pred, y_val, y_val_pred)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:37.772377Z","iopub.execute_input":"2022-08-06T16:25:37.772915Z","iopub.status.idle":"2022-08-06T16:25:37.784142Z","shell.execute_reply.started":"2022-08-06T16:25:37.772885Z","shell.execute_reply":"2022-08-06T16:25:37.783282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = get_metrics(y_train, y_train_pred, y_val, y_val_pred, 'Lasso', scores)\nscores","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:37.786233Z","iopub.execute_input":"2022-08-06T16:25:37.787130Z","iopub.status.idle":"2022-08-06T16:25:37.819902Z","shell.execute_reply.started":"2022-08-06T16:25:37.787099Z","shell.execute_reply":"2022-08-06T16:25:37.819099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Ridge ","metadata":{}},{"cell_type":"code","source":"ridge = Ridge()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:37.821312Z","iopub.execute_input":"2022-08-06T16:25:37.822224Z","iopub.status.idle":"2022-08-06T16:25:37.826328Z","shell.execute_reply.started":"2022-08-06T16:25:37.822171Z","shell.execute_reply":"2022-08-06T16:25:37.825469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"param_grid = {'alpha': np.linspace(1e-5, 500, 40)}","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:37.828016Z","iopub.execute_input":"2022-08-06T16:25:37.829314Z","iopub.status.idle":"2022-08-06T16:25:37.839646Z","shell.execute_reply.started":"2022-08-06T16:25:37.829283Z","shell.execute_reply":"2022-08-06T16:25:37.838084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ridge_grid = GridSearchCV(ridge, param_grid, scoring = scoring, n_jobs= -1, cv= 3)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:37.843350Z","iopub.execute_input":"2022-08-06T16:25:37.845425Z","iopub.status.idle":"2022-08-06T16:25:37.851318Z","shell.execute_reply.started":"2022-08-06T16:25:37.845387Z","shell.execute_reply":"2022-08-06T16:25:37.850440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ridge_grid.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:37.854656Z","iopub.execute_input":"2022-08-06T16:25:37.856862Z","iopub.status.idle":"2022-08-06T16:25:38.559187Z","shell.execute_reply.started":"2022-08-06T16:25:37.856825Z","shell.execute_reply":"2022-08-06T16:25:38.558497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_pred = ridge_grid.predict(X_train)\ny_val_pred = ridge_grid.predict(X_val)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:38.601796Z","iopub.execute_input":"2022-08-06T16:25:38.604782Z","iopub.status.idle":"2022-08-06T16:25:38.627739Z","shell.execute_reply.started":"2022-08-06T16:25:38.604746Z","shell.execute_reply":"2022-08-06T16:25:38.626999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ridge_grid.best_params_","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:38.631547Z","iopub.execute_input":"2022-08-06T16:25:38.633778Z","iopub.status.idle":"2022-08-06T16:25:38.644233Z","shell.execute_reply.started":"2022-08-06T16:25:38.633747Z","shell.execute_reply":"2022-08-06T16:25:38.643525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eval(y_train, y_train_pred, y_val, y_val_pred)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:38.647859Z","iopub.execute_input":"2022-08-06T16:25:38.650250Z","iopub.status.idle":"2022-08-06T16:25:38.663811Z","shell.execute_reply.started":"2022-08-06T16:25:38.650219Z","shell.execute_reply":"2022-08-06T16:25:38.663003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = get_metrics(y_train, y_train_pred, y_val, y_val_pred, 'Ridge', scores)\nscores","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:38.667913Z","iopub.execute_input":"2022-08-06T16:25:38.670437Z","iopub.status.idle":"2022-08-06T16:25:38.694143Z","shell.execute_reply.started":"2022-08-06T16:25:38.670398Z","shell.execute_reply":"2022-08-06T16:25:38.693370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Random Forest ","metadata":{}},{"cell_type":"code","source":"rf = RandomForestRegressor(random_state= 42)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:38.697627Z","iopub.execute_input":"2022-08-06T16:25:38.699424Z","iopub.status.idle":"2022-08-06T16:25:38.704795Z","shell.execute_reply.started":"2022-08-06T16:25:38.699392Z","shell.execute_reply":"2022-08-06T16:25:38.703899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"param_grid = {'n_estimators': [100,150,200,250,300],\n             'min_samples_split': [2,7],\n              'max_depth': [0,6],\n              'min_samples_leaf': [1,6],\n              'ccp_alpha' : [0.001,0.01,0.1]}","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:38.708403Z","iopub.execute_input":"2022-08-06T16:25:38.710300Z","iopub.status.idle":"2022-08-06T16:25:38.716834Z","shell.execute_reply.started":"2022-08-06T16:25:38.710269Z","shell.execute_reply":"2022-08-06T16:25:38.715982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf_grid = GridSearchCV(rf, param_grid, scoring= scoring, cv= 3, n_jobs= -1)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:38.720432Z","iopub.execute_input":"2022-08-06T16:25:38.722698Z","iopub.status.idle":"2022-08-06T16:25:38.728669Z","shell.execute_reply.started":"2022-08-06T16:25:38.722665Z","shell.execute_reply":"2022-08-06T16:25:38.727796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf_grid.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:25:38.734584Z","iopub.execute_input":"2022-08-06T16:25:38.736315Z","iopub.status.idle":"2022-08-06T16:26:53.611063Z","shell.execute_reply.started":"2022-08-06T16:25:38.736238Z","shell.execute_reply":"2022-08-06T16:26:53.610125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf_grid.best_params_","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:26:53.612235Z","iopub.execute_input":"2022-08-06T16:26:53.612588Z","iopub.status.idle":"2022-08-06T16:26:53.618994Z","shell.execute_reply.started":"2022-08-06T16:26:53.612553Z","shell.execute_reply":"2022-08-06T16:26:53.618003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_pred = rf_grid.predict(X_train)\ny_val_pred = rf_grid.predict(X_val)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:26:53.620277Z","iopub.execute_input":"2022-08-06T16:26:53.620594Z","iopub.status.idle":"2022-08-06T16:26:53.698933Z","shell.execute_reply.started":"2022-08-06T16:26:53.620562Z","shell.execute_reply":"2022-08-06T16:26:53.697996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eval(y_train, y_train_pred, y_val, y_val_pred)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:26:53.700087Z","iopub.execute_input":"2022-08-06T16:26:53.700307Z","iopub.status.idle":"2022-08-06T16:26:53.708082Z","shell.execute_reply.started":"2022-08-06T16:26:53.700284Z","shell.execute_reply":"2022-08-06T16:26:53.707300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = get_metrics(y_train, y_train_pred, y_val, y_val_pred, 'Random_Forest', scores)\nscores","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:26:53.709135Z","iopub.execute_input":"2022-08-06T16:26:53.709368Z","iopub.status.idle":"2022-08-06T16:26:53.731329Z","shell.execute_reply.started":"2022-08-06T16:26:53.709346Z","shell.execute_reply":"2022-08-06T16:26:53.730253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"importances = rf_grid.best_estimator_.feature_importances_\nindices = np.argsort(importances)[::-1]\nind_att_names = X_train.columns\nimp_df = pd.DataFrame([ind_att_names[indices], np.sort(importances)[::-1]])","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:26:53.732444Z","iopub.execute_input":"2022-08-06T16:26:53.732678Z","iopub.status.idle":"2022-08-06T16:26:53.771509Z","shell.execute_reply.started":"2022-08-06T16:26:53.732656Z","shell.execute_reply":"2022-08-06T16:26:53.769698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd. set_option(\"display.max_columns\", None)\nimp_df","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:26:53.773187Z","iopub.execute_input":"2022-08-06T16:26:53.773443Z","iopub.status.idle":"2022-08-06T16:26:53.884740Z","shell.execute_reply.started":"2022-08-06T16:26:53.773421Z","shell.execute_reply":"2022-08-06T16:26:53.883765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols = ['OverallQual','GrLivArea','TotalBsmtSF','GarageCars','BsmtQual','LotArea',\n        'GarageArea','YearBuilt','GarageFinish','FullBath','WoodDeckSF','TotRmsAbvGrd','YearRemodAdd',\n       'KitchenQual','OpenPorchSF','FireplaceQu','ExterQual','LotFrontage','BsmtFullBath','GarageYrBlt',\n       'BsmtFinType1','BsmtExposure','MasVnrArea','CentralAir_Y','OverallCond','BedroomAbvGr','Fireplaces',\n       'LotShape']","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:26:53.886247Z","iopub.execute_input":"2022-08-06T16:26:53.886484Z","iopub.status.idle":"2022-08-06T16:26:53.899016Z","shell.execute_reply.started":"2022-08-06T16:26:53.886460Z","shell.execute_reply":"2022-08-06T16:26:53.897937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_rf = X_train[cols]\nX_val_rf = X_val[cols]","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:26:53.900372Z","iopub.execute_input":"2022-08-06T16:26:53.900597Z","iopub.status.idle":"2022-08-06T16:26:53.917007Z","shell.execute_reply.started":"2022-08-06T16:26:53.900575Z","shell.execute_reply":"2022-08-06T16:26:53.915513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train_rf.shape)\nprint(X_val_rf.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:26:53.918549Z","iopub.execute_input":"2022-08-06T16:26:53.918919Z","iopub.status.idle":"2022-08-06T16:26:53.929647Z","shell.execute_reply.started":"2022-08-06T16:26:53.918882Z","shell.execute_reply":"2022-08-06T16:26:53.928379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf = RandomForestRegressor(random_state= 42)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:26:53.931380Z","iopub.execute_input":"2022-08-06T16:26:53.931943Z","iopub.status.idle":"2022-08-06T16:26:53.940471Z","shell.execute_reply.started":"2022-08-06T16:26:53.931909Z","shell.execute_reply":"2022-08-06T16:26:53.939276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"param_grid = {'n_estimators': [200,210,220,230,240,250],\n             'min_samples_split': [2,5],\n              'max_depth': [0,6],\n              'min_samples_leaf': [1,3,5],\n              'ccp_alpha' : [0.0001,0.001,0.01,0.1]}","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:26:53.941592Z","iopub.execute_input":"2022-08-06T16:26:53.941841Z","iopub.status.idle":"2022-08-06T16:26:53.952939Z","shell.execute_reply.started":"2022-08-06T16:26:53.941819Z","shell.execute_reply":"2022-08-06T16:26:53.951996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf_grid = GridSearchCV(rf, param_grid, scoring= scoring, cv= 3, n_jobs= -1)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:26:53.954292Z","iopub.execute_input":"2022-08-06T16:26:53.954514Z","iopub.status.idle":"2022-08-06T16:26:53.965585Z","shell.execute_reply.started":"2022-08-06T16:26:53.954491Z","shell.execute_reply":"2022-08-06T16:26:53.963695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf_grid.fit(X_train_rf, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:26:53.967264Z","iopub.execute_input":"2022-08-06T16:26:53.967672Z","iopub.status.idle":"2022-08-06T16:28:54.849863Z","shell.execute_reply.started":"2022-08-06T16:26:53.967639Z","shell.execute_reply":"2022-08-06T16:28:54.849301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf_grid.best_params_","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:28:54.850692Z","iopub.execute_input":"2022-08-06T16:28:54.851846Z","iopub.status.idle":"2022-08-06T16:28:54.857218Z","shell.execute_reply.started":"2022-08-06T16:28:54.851822Z","shell.execute_reply":"2022-08-06T16:28:54.856323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_pred = rf_grid.predict(X_train_rf)\ny_val_pred = rf_grid.predict(X_val_rf)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:28:54.858387Z","iopub.execute_input":"2022-08-06T16:28:54.858644Z","iopub.status.idle":"2022-08-06T16:28:54.930148Z","shell.execute_reply.started":"2022-08-06T16:28:54.858616Z","shell.execute_reply":"2022-08-06T16:28:54.929238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eval(y_train, y_train_pred, y_val, y_val_pred)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:28:54.931024Z","iopub.execute_input":"2022-08-06T16:28:54.931236Z","iopub.status.idle":"2022-08-06T16:28:54.938059Z","shell.execute_reply.started":"2022-08-06T16:28:54.931209Z","shell.execute_reply":"2022-08-06T16:28:54.937458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = get_metrics(y_train, y_train_pred, y_val, y_val_pred, 'Random_Forest_features', scores)\nscores","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:28:54.939045Z","iopub.execute_input":"2022-08-06T16:28:54.939710Z","iopub.status.idle":"2022-08-06T16:28:54.964634Z","shell.execute_reply.started":"2022-08-06T16:28:54.939686Z","shell.execute_reply":"2022-08-06T16:28:54.963768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## XGBoost ","metadata":{}},{"cell_type":"code","source":"xgb = XGBRegressor()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:28:54.966252Z","iopub.execute_input":"2022-08-06T16:28:54.966497Z","iopub.status.idle":"2022-08-06T16:28:54.970191Z","shell.execute_reply.started":"2022-08-06T16:28:54.966475Z","shell.execute_reply":"2022-08-06T16:28:54.969448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"param_grid = {\n    \"max_depth\": [3, 4, 5, 7],\n    \"learning_rate\": [0.1, 0.01],\n    \"gamma\": [0, 0.25, 1],\n    \"reg_lambda\": [0, 1, 10],\n    \"scale_pos_weight\": [1, 3, 5],\n    \"subsample\": [0.8],\n    \"colsample_bytree\": [0.5],\n}","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:28:54.971226Z","iopub.execute_input":"2022-08-06T16:28:54.971444Z","iopub.status.idle":"2022-08-06T16:28:54.982877Z","shell.execute_reply.started":"2022-08-06T16:28:54.971422Z","shell.execute_reply":"2022-08-06T16:28:54.981440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xgb_grid = GridSearchCV(xgb, param_grid, scoring= scoring, cv= 3, n_jobs= -1)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:28:54.984469Z","iopub.execute_input":"2022-08-06T16:28:54.984691Z","iopub.status.idle":"2022-08-06T16:28:54.993330Z","shell.execute_reply.started":"2022-08-06T16:28:54.984670Z","shell.execute_reply":"2022-08-06T16:28:54.992728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xgb_grid.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:28:54.994591Z","iopub.execute_input":"2022-08-06T16:28:54.994922Z","iopub.status.idle":"2022-08-06T16:31:06.099553Z","shell.execute_reply.started":"2022-08-06T16:28:54.994891Z","shell.execute_reply":"2022-08-06T16:31:06.098485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xgb_grid.best_params_","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:31:06.102742Z","iopub.execute_input":"2022-08-06T16:31:06.103069Z","iopub.status.idle":"2022-08-06T16:31:06.111599Z","shell.execute_reply.started":"2022-08-06T16:31:06.103044Z","shell.execute_reply":"2022-08-06T16:31:06.110390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_pred = xgb_grid.predict(X_train)\ny_val_pred = xgb_grid.predict(X_val)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:31:06.112794Z","iopub.execute_input":"2022-08-06T16:31:06.113263Z","iopub.status.idle":"2022-08-06T16:31:06.143804Z","shell.execute_reply.started":"2022-08-06T16:31:06.113239Z","shell.execute_reply":"2022-08-06T16:31:06.142629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eval(y_train, y_train_pred, y_val, y_val_pred)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:31:06.146973Z","iopub.execute_input":"2022-08-06T16:31:06.147229Z","iopub.status.idle":"2022-08-06T16:31:06.156875Z","shell.execute_reply.started":"2022-08-06T16:31:06.147205Z","shell.execute_reply":"2022-08-06T16:31:06.155858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = get_metrics(y_train, y_train_pred, y_val, y_val_pred, 'XGBoost', scores)\nscores","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:31:06.158669Z","iopub.execute_input":"2022-08-06T16:31:06.159656Z","iopub.status.idle":"2022-08-06T16:31:06.180844Z","shell.execute_reply.started":"2022-08-06T16:31:06.159631Z","shell.execute_reply":"2022-08-06T16:31:06.179999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Selecting the columns which we picked above from feature selection","metadata":{}},{"cell_type":"code","source":"X_train_xgb = X_train[cols]\nX_val_xgb = X_val[cols]","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:31:06.182287Z","iopub.execute_input":"2022-08-06T16:31:06.182778Z","iopub.status.idle":"2022-08-06T16:31:06.187779Z","shell.execute_reply.started":"2022-08-06T16:31:06.182746Z","shell.execute_reply":"2022-08-06T16:31:06.187217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xgb = XGBRegressor()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:31:06.188796Z","iopub.execute_input":"2022-08-06T16:31:06.189429Z","iopub.status.idle":"2022-08-06T16:31:06.205399Z","shell.execute_reply.started":"2022-08-06T16:31:06.189405Z","shell.execute_reply":"2022-08-06T16:31:06.203256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xgb_grid = GridSearchCV(xgb, param_grid, scoring= scoring, cv= 3, n_jobs= -1)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:31:06.206993Z","iopub.execute_input":"2022-08-06T16:31:06.207523Z","iopub.status.idle":"2022-08-06T16:31:06.218942Z","shell.execute_reply.started":"2022-08-06T16:31:06.207483Z","shell.execute_reply":"2022-08-06T16:31:06.218005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xgb_grid.fit(X_train_xgb, y_train)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-08-06T16:31:06.220848Z","iopub.execute_input":"2022-08-06T16:31:06.221227Z","iopub.status.idle":"2022-08-06T16:32:04.693944Z","shell.execute_reply.started":"2022-08-06T16:31:06.221201Z","shell.execute_reply":"2022-08-06T16:32:04.692889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xgb_grid.best_params_","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:32:04.695557Z","iopub.execute_input":"2022-08-06T16:32:04.695973Z","iopub.status.idle":"2022-08-06T16:32:04.702299Z","shell.execute_reply.started":"2022-08-06T16:32:04.695917Z","shell.execute_reply":"2022-08-06T16:32:04.701640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_pred_xgb = xgb_grid.predict(X_train_xgb)\ny_val_pred_xgb = xgb_grid.predict(X_val_xgb)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:32:04.703545Z","iopub.execute_input":"2022-08-06T16:32:04.703799Z","iopub.status.idle":"2022-08-06T16:32:04.724771Z","shell.execute_reply.started":"2022-08-06T16:32:04.703776Z","shell.execute_reply":"2022-08-06T16:32:04.724021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eval(y_train, y_train_pred_xgb, y_val, y_val_pred_xgb)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:32:04.726025Z","iopub.execute_input":"2022-08-06T16:32:04.726692Z","iopub.status.idle":"2022-08-06T16:32:04.733934Z","shell.execute_reply.started":"2022-08-06T16:32:04.726663Z","shell.execute_reply":"2022-08-06T16:32:04.733009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = get_metrics(y_train, y_train_pred_xgb, y_val, y_val_pred_xgb, 'XGBoost_features', scores)\nscores","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:32:04.735501Z","iopub.execute_input":"2022-08-06T16:32:04.736006Z","iopub.status.idle":"2022-08-06T16:32:04.758777Z","shell.execute_reply.started":"2022-08-06T16:32:04.735977Z","shell.execute_reply":"2022-08-06T16:32:04.757786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## LightGBM","metadata":{}},{"cell_type":"code","source":"lgbm = LGBMRegressor()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:32:04.760111Z","iopub.execute_input":"2022-08-06T16:32:04.760543Z","iopub.status.idle":"2022-08-06T16:32:04.764401Z","shell.execute_reply.started":"2022-08-06T16:32:04.760517Z","shell.execute_reply":"2022-08-06T16:32:04.763298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"param_grid = {\n    \"max_depth\": [3, 4, 5, 6, 7], #depth of tree\n    'num_leaves': [8, 16, 32, 64], #num of decision leaves in a single tree\n    \"learning_rate\": [0.1, 0.01],\n    \"reg_lambda\": [0, 1, 10], #L2 regularization, reg_alpha L1 regularization\n}","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:32:04.765294Z","iopub.execute_input":"2022-08-06T16:32:04.766176Z","iopub.status.idle":"2022-08-06T16:32:04.776684Z","shell.execute_reply.started":"2022-08-06T16:32:04.766151Z","shell.execute_reply":"2022-08-06T16:32:04.775686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lgbm_grid = GridSearchCV(lgbm, param_grid, scoring= scoring, cv = 3, n_jobs= -1)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:32:04.777877Z","iopub.execute_input":"2022-08-06T16:32:04.778162Z","iopub.status.idle":"2022-08-06T16:32:04.790001Z","shell.execute_reply.started":"2022-08-06T16:32:04.778138Z","shell.execute_reply":"2022-08-06T16:32:04.789223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lgbm_grid.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:32:04.791297Z","iopub.execute_input":"2022-08-06T16:32:04.792718Z","iopub.status.idle":"2022-08-06T16:32:15.593983Z","shell.execute_reply.started":"2022-08-06T16:32:04.792680Z","shell.execute_reply":"2022-08-06T16:32:15.593155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lgbm_grid.best_params_","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:32:15.597075Z","iopub.execute_input":"2022-08-06T16:32:15.599428Z","iopub.status.idle":"2022-08-06T16:32:15.607788Z","shell.execute_reply.started":"2022-08-06T16:32:15.599392Z","shell.execute_reply":"2022-08-06T16:32:15.606411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_pred = lgbm_grid.predict(X_train)\ny_val_pred = lgbm_grid.predict(X_val)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:32:15.609141Z","iopub.execute_input":"2022-08-06T16:32:15.609523Z","iopub.status.idle":"2022-08-06T16:32:15.633414Z","shell.execute_reply.started":"2022-08-06T16:32:15.609494Z","shell.execute_reply":"2022-08-06T16:32:15.632597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eval(y_train, y_train_pred, y_val, y_val_pred)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:32:15.634732Z","iopub.execute_input":"2022-08-06T16:32:15.635135Z","iopub.status.idle":"2022-08-06T16:32:15.641293Z","shell.execute_reply.started":"2022-08-06T16:32:15.635110Z","shell.execute_reply":"2022-08-06T16:32:15.640582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = get_metrics(y_train, y_train_pred, y_val, y_val_pred, 'LightGBM', scores)\nscores","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:32:15.642385Z","iopub.execute_input":"2022-08-06T16:32:15.642793Z","iopub.status.idle":"2022-08-06T16:32:15.667454Z","shell.execute_reply.started":"2022-08-06T16:32:15.642764Z","shell.execute_reply":"2022-08-06T16:32:15.666783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_lgbm = X_train[cols]\nX_val_lgbm = X_val[cols]","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:32:15.668486Z","iopub.execute_input":"2022-08-06T16:32:15.668834Z","iopub.status.idle":"2022-08-06T16:32:15.674165Z","shell.execute_reply.started":"2022-08-06T16:32:15.668812Z","shell.execute_reply":"2022-08-06T16:32:15.673303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lgbm_grid = GridSearchCV(lgbm, param_grid, scoring= scoring, cv = 3, n_jobs= -1)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:32:15.675383Z","iopub.execute_input":"2022-08-06T16:32:15.675632Z","iopub.status.idle":"2022-08-06T16:32:15.687277Z","shell.execute_reply.started":"2022-08-06T16:32:15.675610Z","shell.execute_reply":"2022-08-06T16:32:15.686346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lgbm_grid.fit(X_train_lgbm, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:32:15.688326Z","iopub.execute_input":"2022-08-06T16:32:15.688713Z","iopub.status.idle":"2022-08-06T16:32:22.549430Z","shell.execute_reply.started":"2022-08-06T16:32:15.688689Z","shell.execute_reply":"2022-08-06T16:32:22.548697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lgbm_grid.best_params_","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:32:22.552200Z","iopub.execute_input":"2022-08-06T16:32:22.554043Z","iopub.status.idle":"2022-08-06T16:32:22.561920Z","shell.execute_reply.started":"2022-08-06T16:32:22.554014Z","shell.execute_reply":"2022-08-06T16:32:22.560346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_pred = lgbm_grid.predict(X_train_lgbm)\ny_val_pred = lgbm_grid.predict(X_val_lgbm)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:32:22.563039Z","iopub.execute_input":"2022-08-06T16:32:22.563770Z","iopub.status.idle":"2022-08-06T16:32:22.582770Z","shell.execute_reply.started":"2022-08-06T16:32:22.563735Z","shell.execute_reply":"2022-08-06T16:32:22.582083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eval(y_train, y_train_pred, y_val, y_val_pred)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:32:22.584248Z","iopub.execute_input":"2022-08-06T16:32:22.584796Z","iopub.status.idle":"2022-08-06T16:32:22.592348Z","shell.execute_reply.started":"2022-08-06T16:32:22.584765Z","shell.execute_reply":"2022-08-06T16:32:22.591599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = get_metrics(y_train, y_train_pred, y_val, y_val_pred, 'LightGBM_features', scores)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:32:22.595277Z","iopub.execute_input":"2022-08-06T16:32:22.595579Z","iopub.status.idle":"2022-08-06T16:32:22.605111Z","shell.execute_reply.started":"2022-08-06T16:32:22.595556Z","shell.execute_reply":"2022-08-06T16:32:22.603867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:32:22.607865Z","iopub.execute_input":"2022-08-06T16:32:22.608341Z","iopub.status.idle":"2022-08-06T16:32:22.625251Z","shell.execute_reply.started":"2022-08-06T16:32:22.608314Z","shell.execute_reply":"2022-08-06T16:32:22.624580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"From the above scores we can see that XGBoost with feature selection generalizes better than the rest","metadata":{}},{"cell_type":"markdown","source":"So we will use the same to predict values on the test data set","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test Predictions","metadata":{}},{"cell_type":"code","source":"xgb = XGBRegressor()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:32:22.626504Z","iopub.execute_input":"2022-08-06T16:32:22.627044Z","iopub.status.idle":"2022-08-06T16:32:22.631183Z","shell.execute_reply.started":"2022-08-06T16:32:22.627003Z","shell.execute_reply":"2022-08-06T16:32:22.630350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"param_grid = {\n    \"max_depth\": [3, 4, 5, 7],\n    \"learning_rate\": [0.1, 0.01],\n    \"gamma\": [0, 0.25, 1],\n    \"reg_lambda\": [0, 1, 10],\n    \"scale_pos_weight\": [1, 3, 5],\n    \"subsample\": [0.8],\n    \"colsample_bytree\": [0.5],\n}","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:32:22.634043Z","iopub.execute_input":"2022-08-06T16:32:22.634442Z","iopub.status.idle":"2022-08-06T16:32:22.643998Z","shell.execute_reply.started":"2022-08-06T16:32:22.634415Z","shell.execute_reply":"2022-08-06T16:32:22.643016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xgb_grid = GridSearchCV(xgb, param_grid, scoring= scoring, cv= 3, n_jobs= -1)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:32:22.645793Z","iopub.execute_input":"2022-08-06T16:32:22.646070Z","iopub.status.idle":"2022-08-06T16:32:22.654667Z","shell.execute_reply.started":"2022-08-06T16:32:22.646047Z","shell.execute_reply":"2022-08-06T16:32:22.653972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xgb_grid.fit(X_train_xgb, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:32:22.655833Z","iopub.execute_input":"2022-08-06T16:32:22.656355Z","iopub.status.idle":"2022-08-06T16:33:21.226131Z","shell.execute_reply.started":"2022-08-06T16:32:22.656325Z","shell.execute_reply":"2022-08-06T16:33:21.225018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xgb_grid.best_params_","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:33:21.230369Z","iopub.execute_input":"2022-08-06T16:33:21.231235Z","iopub.status.idle":"2022-08-06T16:33:21.239584Z","shell.execute_reply.started":"2022-08-06T16:33:21.231198Z","shell.execute_reply":"2022-08-06T16:33:21.238314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_xgb = test[cols]","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:33:21.241185Z","iopub.execute_input":"2022-08-06T16:33:21.242653Z","iopub.status.idle":"2022-08-06T16:33:21.252101Z","shell.execute_reply.started":"2022-08-06T16:33:21.242619Z","shell.execute_reply":"2022-08-06T16:33:21.250939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_xgb.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:33:21.253466Z","iopub.execute_input":"2022-08-06T16:33:21.253809Z","iopub.status.idle":"2022-08-06T16:33:21.264661Z","shell.execute_reply.started":"2022-08-06T16:33:21.253784Z","shell.execute_reply":"2022-08-06T16:33:21.263745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred = xgb_grid.predict(test_xgb)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:33:21.266109Z","iopub.execute_input":"2022-08-06T16:33:21.266518Z","iopub.status.idle":"2022-08-06T16:33:21.286334Z","shell.execute_reply.started":"2022-08-06T16:33:21.266494Z","shell.execute_reply":"2022-08-06T16:33:21.285121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:33:21.287656Z","iopub.execute_input":"2022-08-06T16:33:21.288433Z","iopub.status.idle":"2022-08-06T16:33:21.298702Z","shell.execute_reply.started":"2022-08-06T16:33:21.288403Z","shell.execute_reply":"2022-08-06T16:33:21.298096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions['Predictions'] = test_pred","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:33:21.299804Z","iopub.execute_input":"2022-08-06T16:33:21.300227Z","iopub.status.idle":"2022-08-06T16:33:21.309939Z","shell.execute_reply.started":"2022-08-06T16:33:21.300197Z","shell.execute_reply":"2022-08-06T16:33:21.309314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:33:21.311556Z","iopub.execute_input":"2022-08-06T16:33:21.312164Z","iopub.status.idle":"2022-08-06T16:33:21.332400Z","shell.execute_reply.started":"2022-08-06T16:33:21.312131Z","shell.execute_reply":"2022-08-06T16:33:21.331562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions.to_csv('House_price_prdictions_advance_regression.csv', index= False)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:33:21.333844Z","iopub.execute_input":"2022-08-06T16:33:21.334271Z","iopub.status.idle":"2022-08-06T16:33:21.349527Z","shell.execute_reply.started":"2022-08-06T16:33:21.334226Z","shell.execute_reply":"2022-08-06T16:33:21.348572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}