{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# **House Price Prediction**","metadata":{}},{"cell_type":"markdown","source":"### Importing Required Libraries","metadata":{}},{"cell_type":"code","source":"# Import the required libraries\nimport numpy as np\nimport pandas as pd \n\nfrom scipy.stats import norm, skew\nfrom scipy.special import boxcox1p\n\n# visualization\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom matplotlib.pyplot import xticks\n%matplotlib inline\n\n# To display all the columns and rows\npd.set_option('display.max_columns', None)\npd.set_option('display.max_rows', None)\n\n# This library will be required to split the data set into train and test sets respectively.\nfrom sklearn.model_selection import train_test_split\n\n# This will be required to scale the data.\nfrom sklearn.preprocessing import StandardScaler,LabelEncoder\nfrom sklearn.pipeline import make_pipeline\n\n# model building packages\nfrom sklearn.linear_model import LinearRegression,Ridge,Lasso\nfrom sklearn.model_selection import GridSearchCV,KFold,cross_val_score\nfrom sklearn.metrics import mean_squared_error \nfrom sklearn.svm import SVR\nfrom sklearn.ensemble import RandomForestRegressor\nfrom xgboost import XGBRegressor\nfrom lightgbm import LGBMRegressor\n\n# Hide unnecessary warnings\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:43:52.266543Z","iopub.execute_input":"2022-07-05T06:43:52.266897Z","iopub.status.idle":"2022-07-05T06:43:52.279715Z","shell.execute_reply.started":"2022-07-05T06:43:52.266866Z","shell.execute_reply":"2022-07-05T06:43:52.278455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Loading Data**","metadata":{}},{"cell_type":"code","source":"df_train = pd.read_csv('../input/house-prices-advanced-regression-techniques/train.csv')\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:43:52.306001Z","iopub.execute_input":"2022-07-05T06:43:52.306332Z","iopub.status.idle":"2022-07-05T06:43:52.409347Z","shell.execute_reply.started":"2022-07-05T06:43:52.306302Z","shell.execute_reply":"2022-07-05T06:43:52.408005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checking Shape of the data\ndf_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:43:52.411348Z","iopub.execute_input":"2022-07-05T06:43:52.412058Z","iopub.status.idle":"2022-07-05T06:43:52.419083Z","shell.execute_reply.started":"2022-07-05T06:43:52.412006Z","shell.execute_reply":"2022-07-05T06:43:52.417707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Summary Statistics \ndf_train.describe().T","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:43:52.421776Z","iopub.execute_input":"2022-07-05T06:43:52.422362Z","iopub.status.idle":"2022-07-05T06:43:52.682908Z","shell.execute_reply.started":"2022-07-05T06:43:52.422319Z","shell.execute_reply":"2022-07-05T06:43:52.681679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checking missing values in training dataset\ndf_train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:43:52.685558Z","iopub.execute_input":"2022-07-05T06:43:52.685985Z","iopub.status.idle":"2022-07-05T06:43:52.713694Z","shell.execute_reply.started":"2022-07-05T06:43:52.685938Z","shell.execute_reply":"2022-07-05T06:43:52.711365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**As we can see there are missing values in train data that we need to fill/drop.**","metadata":{}},{"cell_type":"markdown","source":"## **Data Cleaning & Exploratory Data Analysis(EDA)**","metadata":{}},{"cell_type":"code","source":"# create copy of train dataset\ntrain_df = df_train.copy()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:43:52.715463Z","iopub.execute_input":"2022-07-05T06:43:52.715927Z","iopub.status.idle":"2022-07-05T06:43:52.725169Z","shell.execute_reply.started":"2022-07-05T06:43:52.715880Z","shell.execute_reply":"2022-07-05T06:43:52.722570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotting correlations on heatmap using seaborn\ntrain_corr = df_train.corr()\nplt.figure(figsize=(24,24))\nsns.heatmap(train_corr, annot=True,cmap='plasma')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:43:52.726984Z","iopub.execute_input":"2022-07-05T06:43:52.727429Z","iopub.status.idle":"2022-07-05T06:44:00.424555Z","shell.execute_reply.started":"2022-07-05T06:43:52.727386Z","shell.execute_reply":"2022-07-05T06:44:00.423479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Result-**\n* We can see from the above heatmap that the House Sale Price is majorly dependent on variables like OverallQual, YearBuilt GrLivArea,TotalBsmtSF, 1stFlrSF, GarageCars, GarageArea, PoolArea etc.\n* Some of the attributes are correlated like GrLivArea is correlated with TotRmsAbvGrand, 2ndFlrSF and many others.\n* We will check categorical variables also in a while and treat multicollinearity later in our model.","metadata":{}},{"cell_type":"code","source":"# Since SalePrice is our target variable, let's look at features which show more than 50% correlation with SalePrice. \ntop_50_corr = train_corr.index[abs(train_corr['SalePrice']>0.5)]\nplt.figure(figsize=(12, 8))\ntop_50_corr = df_train[top_50_corr].corr()\nsns.heatmap(top_50_corr,cmap=\"YlGnBu\", annot=True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:00.425937Z","iopub.execute_input":"2022-07-05T06:44:00.426506Z","iopub.status.idle":"2022-07-05T06:44:01.405953Z","shell.execute_reply.started":"2022-07-05T06:44:00.426464Z","shell.execute_reply":"2022-07-05T06:44:01.404831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**From the heatmap plotted above, we can see that SalePrice is highly correlated to OverallQual (0.79).**","metadata":{}},{"cell_type":"code","source":"#If plot the Heat map for features with correlation more than 75% we get below plot \n\ncorrmat = df_train.corr()\nf, ax = plt.subplots(figsize=(15, 15))\nsns.heatmap(corrmat, vmin = 0,vmax=1, square=True, cmap = 'plasma', annot = True,mask= corrmat < 0.75, fmt = '.1f', \n            linecolor = 'black', center = 0,annot_kws={\"size\": 7},)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:01.409468Z","iopub.execute_input":"2022-07-05T06:44:01.410139Z","iopub.status.idle":"2022-07-05T06:44:02.774378Z","shell.execute_reply.started":"2022-07-05T06:44:01.410072Z","shell.execute_reply":"2022-07-05T06:44:02.773195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Univariate Analysis**","metadata":{}},{"cell_type":"markdown","source":"#### **1. Analysing Categorical Variables**","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(18, 12))\n\nplt.subplot(2, 2, 1)\nsns.countplot(x='MSSubClass', data=df_train)\n\nplt.subplot(2, 2, 2)\nsns.countplot(x='MSZoning', data=df_train)\n\nplt.subplot(2, 2, 3)\nsns.countplot(x='Street', data=df_train)\n\nplt.subplot(2, 2, 4)\nsns.countplot(x='Alley', data=df_train)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:02.776821Z","iopub.execute_input":"2022-07-05T06:44:02.777569Z","iopub.status.idle":"2022-07-05T06:44:03.397928Z","shell.execute_reply.started":"2022-07-05T06:44:02.777505Z","shell.execute_reply":"2022-07-05T06:44:03.396704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Insights-**:\n* As observed the count is most for MSSubClass as 20.\n* MSZoning as RL i.e. Residential Low Density.\n* Street is mostly Paved and most houses have No Alley access.","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(18, 12))\n\nplt.subplot(2, 2, 1)\nsns.countplot(x='LotShape', data=df_train)\n\nplt.subplot(2, 2, 2)\nsns.countplot(x='LandContour', data=df_train)\n\nplt.subplot(2, 2, 3)\nsns.countplot(x='Utilities', data=df_train)\n\nplt.subplot(2, 2, 4)\nsns.countplot(x='LotConfig', data=df_train)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:03.399669Z","iopub.execute_input":"2022-07-05T06:44:03.400380Z","iopub.status.idle":"2022-07-05T06:44:04.227854Z","shell.execute_reply.started":"2022-07-05T06:44:03.400332Z","shell.execute_reply":"2022-07-05T06:44:04.226811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Insights**:\n\n* As observed the count is most for LotShape as Regular.\n* LandContourNear Flat/Level as Near Flat/Level.\n* Utilities is mostly AllPub and LotConfig is mostly Inside.","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(18, 12))\n\nplt.subplot(2, 2, 1)\nsns.countplot(x='LandSlope', data=df_train)\n\nplt.subplot(2, 2, 2)\nsns.countplot(x='Neighborhood', data=df_train)\nplt.xticks(rotation = 90)\n\nplt.subplot(2, 2, 3)\nsns.countplot(x='Condition1', data=df_train)\n\nplt.subplot(2, 2, 4)\nsns.countplot(x='Condition2', data=df_train)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:04.229542Z","iopub.execute_input":"2022-07-05T06:44:04.229979Z","iopub.status.idle":"2022-07-05T06:44:05.392234Z","shell.execute_reply.started":"2022-07-05T06:44:04.229931Z","shell.execute_reply":"2022-07-05T06:44:05.391061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Insights**:\n\n* As observed Land Shape for most houses is Gtl.\n* Neighborhood is Northwest Ames.\n* Condition1 is Normal and Condition2 is Normal.","metadata":{}},{"cell_type":"code","source":"\nplt.figure(figsize=(18, 12))\n\nplt.subplot(2, 2, 1)\nsns.countplot(x='RoofStyle', data=df_train)\n\nplt.subplot(2, 2, 2)\nsns.countplot(x='RoofMatl', data=df_train)\n\nplt.subplot(2, 2, 3)\nsns.countplot(x='Exterior1st', data=df_train)\nplt.xticks(rotation = 90)\n\nplt.subplot(2, 2, 4)\nsns.countplot(x='Exterior2nd', data=df_train)\nplt.xticks(rotation = 90)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:05.394019Z","iopub.execute_input":"2022-07-05T06:44:05.394493Z","iopub.status.idle":"2022-07-05T06:44:06.333268Z","shell.execute_reply.started":"2022-07-05T06:44:05.394445Z","shell.execute_reply":"2022-07-05T06:44:06.332012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Insights**:\n\n* As observed RoofStyle is mostly Gable.\n* RoofMatl is mostly Standard (Composite) Shingle.\n* Exterior1st is VinylSd and Exterior2nd is VinylSd.","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(18, 12))\n\nplt.subplot(2, 2, 1)\nsns.countplot(x='MasVnrType', data=df_train)\n\nplt.subplot(2, 2, 2)\nsns.countplot(x='ExterQual', data=df_train)\n\nplt.subplot(2, 2, 3)\nsns.countplot(x='ExterCond', data=df_train)\n\nplt.subplot(2, 2, 4)\nsns.countplot(x='Foundation', data=df_train)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:06.334993Z","iopub.execute_input":"2022-07-05T06:44:06.335638Z","iopub.status.idle":"2022-07-05T06:44:06.891922Z","shell.execute_reply.started":"2022-07-05T06:44:06.335591Z","shell.execute_reply":"2022-07-05T06:44:06.890811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Insights**:\n\n* As observed MasVnrType is mostly None.\n* ExterQual is Typical.\n* ExterCond is Typical and Foundation is mostly Poured Contrete or Cinder Block.","metadata":{}},{"cell_type":"code","source":"\nplt.figure(figsize=(18, 12))\n\nplt.subplot(2, 2, 1)\nsns.countplot(x='BsmtQual', data=df_train)\n\nplt.subplot(2, 2, 2)\nsns.countplot(x='BsmtCond', data=df_train)\n\nplt.subplot(2, 2, 3)\nsns.countplot(x='BsmtExposure', data=df_train)\n\nplt.subplot(2, 2, 4)\nsns.countplot(x='BsmtFinType1', data=df_train)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:06.893636Z","iopub.execute_input":"2022-07-05T06:44:06.894323Z","iopub.status.idle":"2022-07-05T06:44:07.447900Z","shell.execute_reply.started":"2022-07-05T06:44:06.894278Z","shell.execute_reply":"2022-07-05T06:44:07.446786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Insights**:\n\n* As observed BsmtQual is mostly Typical or Good.\n* BsmtCond is Typical.\n* BsmtExposure is no and BsmtFinType1 is mostly BsmtFinType1 or Unfinished.","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(18, 12))\n\nplt.subplot(2, 2, 1)\nsns.countplot(x='GarageType', data=df_train)\n\nplt.subplot(2, 2, 2)\nsns.countplot(x='GarageFinish', data=df_train)\n\nplt.subplot(2, 2, 3)\nsns.countplot(x='GarageQual', data=df_train)\n\nplt.subplot(2, 2, 4)\nsns.countplot(x='GarageCond', data=df_train)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:07.449571Z","iopub.execute_input":"2022-07-05T06:44:07.450441Z","iopub.status.idle":"2022-07-05T06:44:08.014914Z","shell.execute_reply.started":"2022-07-05T06:44:07.450382Z","shell.execute_reply":"2022-07-05T06:44:08.013751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Insights**:\n\n* As observed GarageType is mostly Attached to home.\n* GarageFinish is mostly Unfinished.\n* GarageQual is mostly Typical and GarageCond is mostly Typical.","metadata":{}},{"cell_type":"code","source":"\nplt.figure(figsize=(18, 12))\n\nplt.subplot(1, 2, 1)\nsns.countplot(x='SaleType', data=df_train)\n\nplt.subplot(1, 2, 2)\nsns.countplot(x='SaleCondition', data=df_train)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:08.016526Z","iopub.execute_input":"2022-07-05T06:44:08.017263Z","iopub.status.idle":"2022-07-05T06:44:08.384913Z","shell.execute_reply.started":"2022-07-05T06:44:08.017219Z","shell.execute_reply":"2022-07-05T06:44:08.383822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Insights**:\n\n* As observed SaleType is mostly Warranty Deed - Conventional and SaleCondition is mostly Normal.","metadata":{}},{"cell_type":"markdown","source":"#### **2.Analysing Numerical Variables**","metadata":{}},{"cell_type":"markdown","source":"**I. Analysing Sale Price of the houses on the basis of the year in which it's built-**","metadata":{}},{"cell_type":"code","source":"numeric_variable = df_train.select_dtypes(['float64','int64'])\nplt.figure(figsize = (15,10))\n# mean sales price year-wise\nnumeric_variable.groupby(['YearBuilt'])['SalePrice'].mean().plot(kind='line')\nplt.ylabel('Mean Sales Price')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:08.386535Z","iopub.execute_input":"2022-07-05T06:44:08.387205Z","iopub.status.idle":"2022-07-05T06:44:08.626776Z","shell.execute_reply.started":"2022-07-05T06:44:08.387156Z","shell.execute_reply":"2022-07-05T06:44:08.625517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Insights-**\n* On the basis of line plot, we can see that new houses have higher prices.","metadata":{}},{"cell_type":"markdown","source":"***II. Analysing Basement Area***","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize = (14,8))\nplt.subplot(2,2,1)\nsns.distplot(df_train['BsmtFinSF1'])\nplt.subplot(2,2,2)\nsns.distplot(df_train['BsmtFinSF2'])\nplt.subplot(2,2,3)\nsns.distplot(df_train['BsmtUnfSF'])\nplt.subplot(2,2,4)\nsns.distplot(df_train['TotalBsmtSF'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:08.628455Z","iopub.execute_input":"2022-07-05T06:44:08.629109Z","iopub.status.idle":"2022-07-05T06:44:09.849948Z","shell.execute_reply.started":"2022-07-05T06:44:08.629035Z","shell.execute_reply":"2022-07-05T06:44:09.848863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Since \"BsmtFinSF2\" shows no variance so I have decided to drop this column**\n","metadata":{}},{"cell_type":"code","source":"df_train.drop(['BsmtFinSF2'], axis = 1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:09.851505Z","iopub.execute_input":"2022-07-05T06:44:09.852135Z","iopub.status.idle":"2022-07-05T06:44:09.860131Z","shell.execute_reply.started":"2022-07-05T06:44:09.852071Z","shell.execute_reply":"2022-07-05T06:44:09.858889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"***III. Analysing how basement area is affecting the sale price of the houses.***","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize = (19,5))\nplt.subplot(1,3,1)\nsns.scatterplot(x = 'BsmtFinSF1', y = 'SalePrice', data = numeric_variable)\nplt.subplot(1,3,2)\nsns.scatterplot(x = 'BsmtUnfSF', y = 'SalePrice', data = numeric_variable)\nplt.subplot(1,3,3)\nsns.scatterplot(x = 'TotalBsmtSF', y = 'SalePrice', data = numeric_variable)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:09.861564Z","iopub.execute_input":"2022-07-05T06:44:09.862007Z","iopub.status.idle":"2022-07-05T06:44:10.377438Z","shell.execute_reply.started":"2022-07-05T06:44:09.861953Z","shell.execute_reply":"2022-07-05T06:44:10.376345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Insights**:\n\n* Basement area does not really affecting the sale price of the houses.\n* As mostly the basement area of the houses are between 0 to 2000 range.","metadata":{}},{"cell_type":"markdown","source":"***IV. Analysing the following columns :***\n* 1stFlrSF: First Floor square feet\n* 2ndFlrSF: Second floor square feet\n* LowQualFinSF: Low quality finished square feet (all floors)\n* GrLivArea: Above grade (ground) living area square feet","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize = (14,8))\nplt.subplot(2,2,1)\nsns.distplot(numeric_variable['1stFlrSF'])\nplt.subplot(2,2,2)\nsns.distplot(numeric_variable['2ndFlrSF'])\nplt.subplot(2,2,3)\nsns.distplot(numeric_variable['LowQualFinSF'])\nplt.subplot(2,2,4)\nsns.distplot(numeric_variable['GrLivArea'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:10.379118Z","iopub.execute_input":"2022-07-05T06:44:10.379759Z","iopub.status.idle":"2022-07-05T06:44:11.378769Z","shell.execute_reply.started":"2022-07-05T06:44:10.379713Z","shell.execute_reply":"2022-07-05T06:44:11.377656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Dropping the column \"LowQualFinSF\" \ndf_train.drop(['LowQualFinSF'], axis = 1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:11.385232Z","iopub.execute_input":"2022-07-05T06:44:11.385579Z","iopub.status.idle":"2022-07-05T06:44:11.392340Z","shell.execute_reply.started":"2022-07-05T06:44:11.385517Z","shell.execute_reply":"2022-07-05T06:44:11.391208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"***V. Analysing the columns with the sale price having following meanings:***\n* BsmtFullBath: Basement full bathrooms\n* BsmtHalfBath: Basement half bathrooms\n* FullBath: Full bathrooms above grade\n* HalfBath: Half bath above grade","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize = (16,8))\nplt.subplot(2,2,1)\nsns.scatterplot(x = 'BsmtFullBath', y = 'SalePrice', data = numeric_variable)\nplt.subplot(2,2,2)\nsns.scatterplot(x = 'BsmtHalfBath', y = 'SalePrice', data = numeric_variable)\nplt.subplot(2,2,3)\nsns.scatterplot(x = 'FullBath', y = 'SalePrice', data = numeric_variable)\nplt.subplot(2,2,4)\nsns.scatterplot(x = 'HalfBath', y = 'SalePrice', data = numeric_variable)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:11.396262Z","iopub.execute_input":"2022-07-05T06:44:11.397023Z","iopub.status.idle":"2022-07-05T06:44:12.117106Z","shell.execute_reply.started":"2022-07-05T06:44:11.396977Z","shell.execute_reply":"2022-07-05T06:44:12.116006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"***VI. Analysing columns with following meanings:***\n* WoodDeckSF: Wood deck area in square feet\n* OpenPorchSF: Open porch area in square feet\n* EnclosedPorch: Enclosed porch area in square feet\n* 3SsnPorch: Three season porch area in square feet\n* ScreenPorch: Screen porch area in square feet\n* PoolArea: Pool area in square feet","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize = (16,8))\nplt.subplot(2,3,1)\nsns.distplot(numeric_variable['WoodDeckSF'])\nplt.subplot(2,3,2)\nsns.distplot(numeric_variable['OpenPorchSF'])\nplt.subplot(2,3,3)\nsns.distplot(numeric_variable['EnclosedPorch'])\nplt.subplot(2,3,4)\nsns.distplot(numeric_variable['3SsnPorch'])\nplt.subplot(2,3,5)\nsns.distplot(numeric_variable['ScreenPorch'])\nplt.subplot(2,3,6)\nsns.distplot(numeric_variable['PoolArea'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:12.118811Z","iopub.execute_input":"2022-07-05T06:44:12.119263Z","iopub.status.idle":"2022-07-05T06:44:13.579072Z","shell.execute_reply.started":"2022-07-05T06:44:12.119216Z","shell.execute_reply":"2022-07-05T06:44:13.578037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Dropping (EnclosedPorch, 3SsnPorch, ScreenPorch and PoolArea) because of their low variance\ndf_train.drop(['EnclosedPorch','3SsnPorch', 'ScreenPorch', 'PoolArea'], axis = 1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:13.580760Z","iopub.execute_input":"2022-07-05T06:44:13.581210Z","iopub.status.idle":"2022-07-05T06:44:13.588730Z","shell.execute_reply.started":"2022-07-05T06:44:13.581168Z","shell.execute_reply":"2022-07-05T06:44:13.587233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.scatterplot(x = 'MiscVal', y = 'SalePrice', data = numeric_variable)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:13.590869Z","iopub.execute_input":"2022-07-05T06:44:13.591417Z","iopub.status.idle":"2022-07-05T06:44:13.810125Z","shell.execute_reply.started":"2022-07-05T06:44:13.591372Z","shell.execute_reply":"2022-07-05T06:44:13.809012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Dropping the following columns as it is adding less value to the data set\ndf_train.drop(['MiscVal','Id','BedroomAbvGr','KitchenAbvGr'], axis = 1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:13.811784Z","iopub.execute_input":"2022-07-05T06:44:13.812317Z","iopub.status.idle":"2022-07-05T06:44:13.819393Z","shell.execute_reply.started":"2022-07-05T06:44:13.812262Z","shell.execute_reply":"2022-07-05T06:44:13.818216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Misssing Value Treatment**","metadata":{}},{"cell_type":"code","source":"# Checking Column-wise Total Count and Percentage of Missing Values\ncount = pd.DataFrame(df_train.isnull().sum().sort_values(ascending=False), columns=['null_counts'])\npercent = pd.DataFrame(round(100*(df_train.isnull().sum()/df_train.shape[0]),2).sort_values(ascending=False)\\\n                          ,columns=['null_percentage'])\nMissing_Value_Table = pd.concat([count, percent], axis = 1)\nMissing_Value_Table.head(20)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:13.821052Z","iopub.execute_input":"2022-07-05T06:44:13.821757Z","iopub.status.idle":"2022-07-05T06:44:13.856575Z","shell.execute_reply.started":"2022-07-05T06:44:13.821713Z","shell.execute_reply":"2022-07-05T06:44:13.855327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\nIn order to effectively train our model we build, we must first deal with the missing values. There are missing values for both numerical and categorical data. For numerical imputing, we would typically fill the missing values with a measure like median, mean, or mode. For categorical columns will see how to deal with.\n\n* **First we will drop \"Alley\", \"PoolQC\", \"Fence\" and \"MiscFeature\" as they have a high percentage of null values (i.e greater than 80%).**\n* **Although here, from the data dictionary we can understand that the null values have significance as Null in \"Alley\" means No Alley, in \"PoolQC\" means No Pool, in \"Fence\" means no fence and in \"MiscFeature\" means none.**\n* **However, even if we impute it these columns will show no variance and hence we will drop them directly.**","metadata":{}},{"cell_type":"code","source":"# Checking for unique values in high percentage missing values column\nhigh_percnt_missing_features = ['Alley','PoolQC','MiscFeature','Fence']\nfor miss in high_percnt_missing_features:\n    print('Unique Values Count for {}'.format(miss))\n    print(df_train[miss].value_counts(dropna=False))\n    print('\\n')","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:13.857996Z","iopub.execute_input":"2022-07-05T06:44:13.858714Z","iopub.status.idle":"2022-07-05T06:44:13.879582Z","shell.execute_reply.started":"2022-07-05T06:44:13.858670Z","shell.execute_reply":"2022-07-05T06:44:13.878342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Dropping the columns with null values greater than 80%.\ndf_train.drop([\"Alley\",\"PoolQC\",\"Fence\",\"MiscFeature\"],axis=1,inplace=True)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:13.881184Z","iopub.execute_input":"2022-07-05T06:44:13.881618Z","iopub.status.idle":"2022-07-05T06:44:13.889115Z","shell.execute_reply.started":"2022-07-05T06:44:13.881576Z","shell.execute_reply":"2022-07-05T06:44:13.887650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checking the shape after dropping the columns\ndf_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:13.891504Z","iopub.execute_input":"2022-07-05T06:44:13.892021Z","iopub.status.idle":"2022-07-05T06:44:13.901713Z","shell.execute_reply.started":"2022-07-05T06:44:13.891978Z","shell.execute_reply":"2022-07-05T06:44:13.900073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Looking at the unique values in 'FireplaceQu' column\ndf_train['FireplaceQu'].value_counts(dropna=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:13.903860Z","iopub.execute_input":"2022-07-05T06:44:13.904519Z","iopub.status.idle":"2022-07-05T06:44:13.918663Z","shell.execute_reply.started":"2022-07-05T06:44:13.904442Z","shell.execute_reply":"2022-07-05T06:44:13.916720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Checking the values in \"Fireplaces\" column corresponding to the null values in \"FireplaceQu\" column\ndf_train.loc[df_train['FireplaceQu'].isnull(),['Fireplaces','FireplaceQu']].head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:13.920946Z","iopub.execute_input":"2022-07-05T06:44:13.921608Z","iopub.status.idle":"2022-07-05T06:44:13.938130Z","shell.execute_reply.started":"2022-07-05T06:44:13.921565Z","shell.execute_reply":"2022-07-05T06:44:13.936419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**From above we can see that Fireplace Quality is null because fireplace is not present (value is 0). From the data dictionary we can see that No Fireplace is indicated by \"NA\". Hence, we will impute the null values with \"Not_Present\".**","metadata":{}},{"cell_type":"code","source":"# Replacing the null values in the \"FireplaceQu\" column with \"Not_Present\"\ndf_train['FireplaceQu'].fillna('Not_Present',inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:13.940021Z","iopub.execute_input":"2022-07-05T06:44:13.940526Z","iopub.status.idle":"2022-07-05T06:44:13.946586Z","shell.execute_reply.started":"2022-07-05T06:44:13.940481Z","shell.execute_reply":"2022-07-05T06:44:13.945058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Visualizing LotFrontage \nsns.distplot(df_train['LotFrontage'].dropna())\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:13.948564Z","iopub.execute_input":"2022-07-05T06:44:13.949424Z","iopub.status.idle":"2022-07-05T06:44:14.241495Z","shell.execute_reply.started":"2022-07-05T06:44:13.949382Z","shell.execute_reply":"2022-07-05T06:44:14.240186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Conclusion:**\n\n* We know 17.7% of the records are missing with values for LotFrontage. This would be a lot of records to be dropped if we proceed records with missing values.\n* And if we impute these missing values with value 0 indicating unknown maybe, we would be altering the distribution for the feature.\n* Hence we can find out the mean of the LotFrontage and impute the missing value with the mean so that the distribution remains intact.","metadata":{}},{"cell_type":"code","source":"# Filling the missing values in \"LotFrontage\" column with the mean values.\ndf_train['LotFrontage'].fillna(df_train['LotFrontage'].mean(),inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:14.244128Z","iopub.execute_input":"2022-07-05T06:44:14.244595Z","iopub.status.idle":"2022-07-05T06:44:14.252969Z","shell.execute_reply.started":"2022-07-05T06:44:14.244539Z","shell.execute_reply":"2022-07-05T06:44:14.251783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Moving ahead wrt GarageType, GarageYrBlt, GarageFinish, GarageCond and GarageQual are attributes related to Garage and all of them are having same percenatge of missing values. This implies that the garrage does not exist in such house.**\n* GarageYrBlt: Year garage was built (Categorical)\n* GarageType: Garage location (Categorical)\n* GarageFinish: Interior finish of the garage(Categorical)\n* GarageArea: Size of garage in square feet (Numerical)\n* GarageQual: Garage quality(Categorical)\n\nAs per Data definition NA in these fields means no garage \n* GarageFinish: NA means \"None\"\n* GarageQual: NA means \"None\"\n* GarageCond: NA means \"None\"\n* GarageYrBlt: NA means 0\n* GarageType: NA means \"None","metadata":{}},{"cell_type":"code","source":"# Checking for values in Garage related columns corresponding to the null values in \"GarageType\" column\ndf_train.loc[df_train['GarageType'].isnull(),['GarageType','GarageYrBlt','GarageFinish','GarageCars',\n                                                              'GarageArea','GarageQual','GarageCond']].head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:14.255234Z","iopub.execute_input":"2022-07-05T06:44:14.255925Z","iopub.status.idle":"2022-07-05T06:44:14.279242Z","shell.execute_reply.started":"2022-07-05T06:44:14.255885Z","shell.execute_reply":"2022-07-05T06:44:14.278175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Conlusion:**\n\n* Since the values are null for all garage related columns, we can conclude that there is no garage.\n* Hence, we will impute \"GarageType\", \"GarageFinish\", \"GarageQual\", \"GarageCond\" with a new level 'Not Present' indicating no garage as per the data dictionary.","metadata":{}},{"cell_type":"code","source":"# Imputing null values in the above columns with \"Not_Present\"\ndf_train.loc[df_train.GarageType.isnull() , ['GarageType','GarageFinish','GarageQual',\n                                                             'GarageCond']] = 'Not_Present'","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:14.280935Z","iopub.execute_input":"2022-07-05T06:44:14.281367Z","iopub.status.idle":"2022-07-05T06:44:14.291781Z","shell.execute_reply.started":"2022-07-05T06:44:14.281327Z","shell.execute_reply":"2022-07-05T06:44:14.290605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Visualising GarageYrBlt \nsns.distplot(df_train['GarageYrBlt'].dropna())\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:14.293304Z","iopub.execute_input":"2022-07-05T06:44:14.294043Z","iopub.status.idle":"2022-07-05T06:44:14.922142Z","shell.execute_reply.started":"2022-07-05T06:44:14.294001Z","shell.execute_reply":"2022-07-05T06:44:14.920889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Conclusion:**\n\n* We can see same percentage of null values for each of the features of Garage including GarageYrBlt.\n* Hence GarageYrBlt would be having missing values for those houses that dont have garage and for which other garage features are NULL as well.\n* We can see that there is no garage and hence there are null values in the Garage Year built column. We will impute it with the current year such that the age is 0.","metadata":{}},{"cell_type":"code","source":"# Imputing the null values with 2021\ndf_train[\"GarageYrBlt\"].fillna(2021, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:14.926769Z","iopub.execute_input":"2022-07-05T06:44:14.927693Z","iopub.status.idle":"2022-07-05T06:44:14.939816Z","shell.execute_reply.started":"2022-07-05T06:44:14.927651Z","shell.execute_reply":"2022-07-05T06:44:14.938437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Moving ahead with attributes related to basement-**\n* BsmtQual: Evaluates the height of the basement\n* BsmtFinType1: Rating of basement finished area\n* BsmtCond: Evaluates the general condition of the basement","metadata":{}},{"cell_type":"markdown","source":"All three are basement categorical features with the same missing values percentages, so \"NA\" implies no basement for those houses.","metadata":{}},{"cell_type":"code","source":"# Replacing NA in BsmtExposure, BsmtFinType2,  BsmtFinType1, BsmtCond, BsmtQual with 'Not_Present' since NA means 'No basement'.\ndf_train.loc[df_train.BsmtQual.isnull() , ['BsmtQual','BsmtCond','BsmtExposure',\n                                                           'BsmtFinType1','BsmtFinType2']]=\"Not_Present\"","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:14.944354Z","iopub.execute_input":"2022-07-05T06:44:14.946504Z","iopub.status.idle":"2022-07-05T06:44:14.962300Z","shell.execute_reply.started":"2022-07-05T06:44:14.946436Z","shell.execute_reply":"2022-07-05T06:44:14.960596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:14.968986Z","iopub.execute_input":"2022-07-05T06:44:14.969418Z","iopub.status.idle":"2022-07-05T06:44:14.990159Z","shell.execute_reply.started":"2022-07-05T06:44:14.969379Z","shell.execute_reply":"2022-07-05T06:44:14.988229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**BsmtFinType2 : Rating of basement finished area (if multiple types)**<br>\n**BsmtExposure : Refers to walkout or garden level walls**<br>\nBoth are categorical basement feature with same missing percenatges , \"NA\" means no basement.","metadata":{}},{"cell_type":"code","source":"df_train['BsmtExposure'].replace(np.nan,'Not_Present',inplace=True)\ndf_train['BsmtFinType2'].replace(np.nan,'Not_Present',inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:14.992385Z","iopub.execute_input":"2022-07-05T06:44:14.994306Z","iopub.status.idle":"2022-07-05T06:44:15.005189Z","shell.execute_reply.started":"2022-07-05T06:44:14.994261Z","shell.execute_reply":"2022-07-05T06:44:15.003983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Checking the values in \"MasVnrType\" column corresponding to null values in \"MasVnrArea\" column \ndf_train.loc[df_train.MasVnrArea.isnull(), ['MasVnrArea','MasVnrType']]","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:15.006921Z","iopub.execute_input":"2022-07-05T06:44:15.007694Z","iopub.status.idle":"2022-07-05T06:44:15.030883Z","shell.execute_reply.started":"2022-07-05T06:44:15.007650Z","shell.execute_reply":"2022-07-05T06:44:15.029174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checking the values in \"MasVnrArea\" corresponding the \"None\" values in \"MasVnrType\" column\ndf_train.loc[df_train.MasVnrType == 'None', ['MasVnrArea','MasVnrType']].head()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:15.032981Z","iopub.execute_input":"2022-07-05T06:44:15.033851Z","iopub.status.idle":"2022-07-05T06:44:15.060700Z","shell.execute_reply.started":"2022-07-05T06:44:15.033809Z","shell.execute_reply":"2022-07-05T06:44:15.059557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Missing values in those features mean that there is no masonry veneer in those houses.**","metadata":{}},{"cell_type":"code","source":"# Imputing the null values in \"MasVnrType\" with \"Not_Present\".\ndf_train['MasVnrType'].fillna('Not_Present',inplace=True)\n# Imputing the \"MasVnrArea\" column with 0.\ndf_train['MasVnrArea'].fillna(0,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:15.062201Z","iopub.execute_input":"2022-07-05T06:44:15.062875Z","iopub.status.idle":"2022-07-05T06:44:15.070495Z","shell.execute_reply.started":"2022-07-05T06:44:15.062835Z","shell.execute_reply":"2022-07-05T06:44:15.069180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Looking at the unique values in 'Electrical' column\ndf_train['Electrical'].value_counts(dropna=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:15.072034Z","iopub.execute_input":"2022-07-05T06:44:15.072810Z","iopub.status.idle":"2022-07-05T06:44:15.094948Z","shell.execute_reply.started":"2022-07-05T06:44:15.072754Z","shell.execute_reply":"2022-07-05T06:44:15.093792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Replacing the null values in the \"Electrical\" column with the mode which is Standard Circuit Breakers & Romex(SBrkr)\ndf_train['Electrical'].fillna(df_train['Electrical'].mode()[0],inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:15.099408Z","iopub.execute_input":"2022-07-05T06:44:15.101001Z","iopub.status.idle":"2022-07-05T06:44:15.112578Z","shell.execute_reply.started":"2022-07-05T06:44:15.100912Z","shell.execute_reply":"2022-07-05T06:44:15.111189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Lets Check Missing Values again\ndf_train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:15.115971Z","iopub.execute_input":"2022-07-05T06:44:15.118314Z","iopub.status.idle":"2022-07-05T06:44:15.141740Z","shell.execute_reply.started":"2022-07-05T06:44:15.118271Z","shell.execute_reply":"2022-07-05T06:44:15.139897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**We can see that there are no more missing values in the dataset. Hence we have successfully cleaned the dataset.**","metadata":{}},{"cell_type":"markdown","source":"## **Feature Engineering**","metadata":{}},{"cell_type":"markdown","source":"#### 1. There are few Numerical Features but actually Categorical so transoform them as Categorical","metadata":{}},{"cell_type":"code","source":"df_train['MSSubClass'] = df_train['MSSubClass'].apply(str)\n#Changing OverallCond into a categorical variable\ndf_train['OverallCond'] = df_train['OverallCond'].astype(str)\n#Year and month sold are transformed into categorical features.\ndf_train['YrSold'] = df_train['YrSold'].astype(str)\ndf_train['MoSold'] = df_train['MoSold'].astype(str)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:15.147566Z","iopub.execute_input":"2022-07-05T06:44:15.148177Z","iopub.status.idle":"2022-07-05T06:44:15.178436Z","shell.execute_reply.started":"2022-07-05T06:44:15.148134Z","shell.execute_reply":"2022-07-05T06:44:15.176449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data distribution in Street column\ndf_train['Street'].astype('category').value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:15.185918Z","iopub.execute_input":"2022-07-05T06:44:15.187808Z","iopub.status.idle":"2022-07-05T06:44:15.206017Z","shell.execute_reply.started":"2022-07-05T06:44:15.187775Z","shell.execute_reply":"2022-07-05T06:44:15.204402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data distribution in Utilities column\ndf_train['Utilities'].astype('category').value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:15.208296Z","iopub.execute_input":"2022-07-05T06:44:15.208714Z","iopub.status.idle":"2022-07-05T06:44:15.222826Z","shell.execute_reply.started":"2022-07-05T06:44:15.208673Z","shell.execute_reply":"2022-07-05T06:44:15.221311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Based on data distribution in each column seen earlier, We have found out that 'Street','Utilities' have very low variance And Id column has all unique values So let's drop these columns as they won't be that useful for analysis.**","metadata":{}},{"cell_type":"code","source":"df_train.drop(['Street','Utilities'], axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:15.225033Z","iopub.execute_input":"2022-07-05T06:44:15.225483Z","iopub.status.idle":"2022-07-05T06:44:15.237138Z","shell.execute_reply.started":"2022-07-05T06:44:15.225429Z","shell.execute_reply":"2022-07-05T06:44:15.233619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Checking if the target variable is normally distributed or not\nplt.figure(figsize=(10,6))\nsns.distplot(df_train['SalePrice'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:15.238830Z","iopub.execute_input":"2022-07-05T06:44:15.239165Z","iopub.status.idle":"2022-07-05T06:44:15.598294Z","shell.execute_reply.started":"2022-07-05T06:44:15.239124Z","shell.execute_reply":"2022-07-05T06:44:15.595810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Conclusion**:\n\n* The distribution is a bit skewed towards right. We can transform it to represent a normal distribution.\n* Lets try with a very general transformation function log and see if that helps here.","metadata":{}},{"cell_type":"code","source":"# using log to transform target variable\nplt.figure(figsize=(10,6))\nsns.distplot(np.log1p(df_train.SalePrice))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:15.600815Z","iopub.execute_input":"2022-07-05T06:44:15.601658Z","iopub.status.idle":"2022-07-05T06:44:15.925251Z","shell.execute_reply.started":"2022-07-05T06:44:15.601549Z","shell.execute_reply":"2022-07-05T06:44:15.923927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Conclusion**:\n\n* So with log, the distribution appears close to normal. So we can use this transformation for our target variable and move ahead.\n* All predictions by the model will then be in log values and we will need to take the antilog to get the actual value.","metadata":{}},{"cell_type":"code","source":"#So target distribution is right skewed so have to normalize target first.\n\ndf_train[\"SalePrice\"] = np.log1p(df_train[\"SalePrice\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:15.927405Z","iopub.execute_input":"2022-07-05T06:44:15.927892Z","iopub.status.idle":"2022-07-05T06:44:15.934614Z","shell.execute_reply.started":"2022-07-05T06:44:15.927842Z","shell.execute_reply":"2022-07-05T06:44:15.933253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### **2. Categorical Encoding**","metadata":{}},{"cell_type":"code","source":"cat_cols = ('FireplaceQu', 'BsmtQual', 'BsmtCond', 'GarageQual', 'GarageCond', \n        'ExterQual', 'ExterCond','HeatingQC', 'KitchenQual', 'BsmtFinType1', \n        'BsmtFinType2', 'Functional', 'BsmtExposure', 'GarageFinish', 'LandSlope',\n        'LotShape', 'PavedDrive', 'CentralAir', 'MSSubClass', 'OverallCond', \n        'YrSold', 'MoSold')","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:15.936423Z","iopub.execute_input":"2022-07-05T06:44:15.937145Z","iopub.status.idle":"2022-07-05T06:44:15.947750Z","shell.execute_reply.started":"2022-07-05T06:44:15.937088Z","shell.execute_reply":"2022-07-05T06:44:15.946446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#apply LabelEncoder to categorical features\nfor col in cat_cols:\n    lb = LabelEncoder() \n    lb.fit(list(df_train[col].values)) \n    df_train[col] = lb.transform(list(df_train[col].values))\n\n# shape        \nprint('Shape all_data: {}'.format(df_train.shape))","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:15.949684Z","iopub.execute_input":"2022-07-05T06:44:15.950712Z","iopub.status.idle":"2022-07-05T06:44:16.025233Z","shell.execute_reply.started":"2022-07-05T06:44:15.950667Z","shell.execute_reply":"2022-07-05T06:44:16.023788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#generating list of numerical columns\nNumFeatures = []\nfor col in list(df_train):\n    if df_train[col].dtypes != 'object':\n        NumFeatures.append(col)  \nprint('Numerical columns:\\n',NumFeatures)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:16.027242Z","iopub.execute_input":"2022-07-05T06:44:16.027725Z","iopub.status.idle":"2022-07-05T06:44:16.038729Z","shell.execute_reply.started":"2022-07-05T06:44:16.027683Z","shell.execute_reply":"2022-07-05T06:44:16.037466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### **3. Creating New Features**","metadata":{}},{"cell_type":"code","source":"df_train['Total_Bathrooms'] = (df_train['FullBath'] + (0.5 * df_train['HalfBath']) +\n                               df_train['BsmtFullBath'] + (0.5 * df_train['BsmtHalfBath']))\ndf_train['YrBltRemod'] = df_train['YearBuilt'] + df_train['YearRemodAdd']\ndf_train['Total_SF'] = df_train['TotalBsmtSF'] + df_train['1stFlrSF'] + df_train['2ndFlrSF']\ndf_train[\"LivLotRatio\"] = df_train['GrLivArea']/df_train['LotArea']","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:16.040526Z","iopub.execute_input":"2022-07-05T06:44:16.041430Z","iopub.status.idle":"2022-07-05T06:44:16.056375Z","shell.execute_reply.started":"2022-07-05T06:44:16.041264Z","shell.execute_reply":"2022-07-05T06:44:16.055278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### **4. Handling Skewed Features**","metadata":{}},{"cell_type":"code","source":"skewed_feats = df_train[NumFeatures].apply(lambda x: skew(x.dropna())).sort_values(ascending=False)\nprint(\"Skewed features :\\n\")\n\nskewed_df = pd.DataFrame()\nskewed_df['Skewness_value'] = skewed_feats\nskewed_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:16.057715Z","iopub.execute_input":"2022-07-05T06:44:16.058453Z","iopub.status.idle":"2022-07-05T06:44:16.115128Z","shell.execute_reply.started":"2022-07-05T06:44:16.058412Z","shell.execute_reply":"2022-07-05T06:44:16.113658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top_10_skewed_features = skewed_df.head(10).index\nprint(top_10_skewed_features)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:16.117213Z","iopub.execute_input":"2022-07-05T06:44:16.117632Z","iopub.status.idle":"2022-07-05T06:44:16.125532Z","shell.execute_reply.started":"2022-07-05T06:44:16.117593Z","shell.execute_reply":"2022-07-05T06:44:16.124305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Visualize the top 10 skewed features distribution with a histogram and maximum likelihood gaussian distribution fit:","metadata":{}},{"cell_type":"code","source":"def CategoryFeaturePlot(columns):\n    fig = plt.figure(figsize=(23,7))\n    for i, col in   enumerate(columns):\n        plt.subplot(2,5, i+1)\n        sns.distplot(df_train[col],fit=norm, kde=False)\n        plt.tight_layout()\n    fig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:16.127582Z","iopub.execute_input":"2022-07-05T06:44:16.128396Z","iopub.status.idle":"2022-07-05T06:44:16.138426Z","shell.execute_reply.started":"2022-07-05T06:44:16.128355Z","shell.execute_reply":"2022-07-05T06:44:16.137045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CategoryFeaturePlot(top_10_skewed_features)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:16.148856Z","iopub.execute_input":"2022-07-05T06:44:16.149273Z","iopub.status.idle":"2022-07-05T06:44:20.881875Z","shell.execute_reply.started":"2022-07-05T06:44:16.149242Z","shell.execute_reply":"2022-07-05T06:44:20.880787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Normalize skewed features with boxcox1p","metadata":{}},{"cell_type":"code","source":"skewness = skewed_df[abs(skewed_df) > 0.70]\n\nskewed_features = skewness.index\nlam = 0.15\nfor feat in skewed_features:\n  \n    df_train[feat] = boxcox1p(df_train[feat], lam)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:20.884687Z","iopub.execute_input":"2022-07-05T06:44:20.885311Z","iopub.status.idle":"2022-07-05T06:44:20.919655Z","shell.execute_reply.started":"2022-07-05T06:44:20.885267Z","shell.execute_reply":"2022-07-05T06:44:20.918653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Visualize the distributions after normalization","metadata":{}},{"cell_type":"code","source":"normalized_features = skewness.head(10).index\nprint(normalized_features)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:20.921283Z","iopub.execute_input":"2022-07-05T06:44:20.921919Z","iopub.status.idle":"2022-07-05T06:44:20.929003Z","shell.execute_reply.started":"2022-07-05T06:44:20.921877Z","shell.execute_reply":"2022-07-05T06:44:20.927764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CategoryFeaturePlot(normalized_features)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:20.930582Z","iopub.execute_input":"2022-07-05T06:44:20.931506Z","iopub.status.idle":"2022-07-05T06:44:24.711398Z","shell.execute_reply.started":"2022-07-05T06:44:20.931463Z","shell.execute_reply":"2022-07-05T06:44:24.710284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### **5. Encoding Categorical features with dummy encoding**","metadata":{}},{"cell_type":"code","source":"dummy_df = pd.get_dummies(df_train,drop_first=True)\nprint(\"Shape of all data : {}\".format(dummy_df.shape))","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:24.713176Z","iopub.execute_input":"2022-07-05T06:44:24.713904Z","iopub.status.idle":"2022-07-05T06:44:24.749225Z","shell.execute_reply.started":"2022-07-05T06:44:24.713856Z","shell.execute_reply":"2022-07-05T06:44:24.747894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dummy_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:24.750930Z","iopub.execute_input":"2022-07-05T06:44:24.751359Z","iopub.status.idle":"2022-07-05T06:44:24.871288Z","shell.execute_reply.started":"2022-07-05T06:44:24.751319Z","shell.execute_reply":"2022-07-05T06:44:24.870066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Data Cleaning And Feature Engineering On Test Data**","metadata":{}},{"cell_type":"code","source":"df_test = pd.read_csv('../input/house-prices-advanced-regression-techniques/test.csv')\nmerge_df = pd.concat([train_df.drop('SalePrice',axis=1),df_test],axis=0)\n# Store the Id column\nID = df_test['Id']\ndrop = ['BsmtFinSF2','LowQualFinSF','EnclosedPorch',\\\n'3SsnPorch', 'ScreenPorch', 'PoolArea','MiscVal','Id','BedroomAbvGr','KitchenAbvGr',\\\n'Alley','PoolQC','Fence','MiscFeature']\nmerge_df.drop(drop, axis = 1,inplace=True)\n\n# Replacing the null values in the \"FireplaceQu\" column with \"Not_Present\"\nmerge_df['FireplaceQu'].fillna('Not_Present',inplace=True)\n# Filling the missing values in \"LotFrontage\" column with the mean values.\nmerge_df['LotFrontage'].fillna(df_test['LotFrontage'].mean(),inplace=True)\n# categorical 'GarageType', 'GarageFinish', 'GarageQual', 'GarageCond' fill with None\nfor col in ('GarageType', 'GarageFinish', 'GarageQual', 'GarageCond'):\n    merge_df[col] = merge_df[col].fillna('Not_Present')\n# Imputing the null values with 2020\nmerge_df[\"GarageYrBlt\"].fillna(0, inplace=True)\n\n# Replacing NA in BsmtExposure, BsmtFinType2,  BsmtFinType1, BsmtCond, BsmtQual with 'Not_Present' since NA means 'No basement'.\n# merge_df.loc[merge_df.BsmtQual.isnull() , ['BsmtQual','BsmtCond','BsmtExposure',\n#                                                            'BsmtFinType1','BsmtFinType2']]=\"Not_Present\"\nfor col in ('BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2'):\n    merge_df[col] = merge_df[col].fillna('Not_Present')\nmerge_df['BsmtExposure'].replace(np.nan,'Not_Present',inplace=True)\nmerge_df['BsmtFinType2'].replace(np.nan,'Not_Present',inplace=True)   \n\n# Imputing the null values in \"MasVnrType\" with \"Not_Present\".\nmerge_df['MasVnrType'].fillna('Not_Present',inplace=True)\n# Imputing the \"MasVnrArea\" column with 0.\nmerge_df['MasVnrArea'].fillna(0,inplace=True)      \n\n# Replacing the null values in the \"Electrical\" column with the mode which is Standard Circuit Breakers & Romex(SBrkr)\nmerge_df['Electrical'].fillna(merge_df['Electrical'].mode()[0],inplace=True)    \n\nmerge_df['MSSubClass'] = merge_df['MSSubClass'].apply(str)\n#Changing OverallCond into a categorical variable\nmerge_df['OverallCond'] = merge_df['OverallCond'].astype(str)\n#Year and month sold are transformed into categorical features.\nmerge_df['YrSold'] = merge_df['YrSold'].astype(str)\nmerge_df['MoSold'] = merge_df['MoSold'].astype(str)\n   \n#dropping street and utilities column\nmerge_df.drop(['Street','Utilities'], axis=1,inplace=True)  \n# Fill in 0 for no basement\nfor col in ('BsmtFinSF1', 'BsmtUnfSF','TotalBsmtSF', 'BsmtFullBath', 'BsmtHalfBath','GarageCars','GarageArea'):\n    merge_df[col] = merge_df[col].fillna(0)\n    \n# For MSZoning 'RL' is predominant so will take that for missing ones\nmerge_df['MSZoning'] = merge_df['MSZoning'].fillna(merge_df['MSZoning'].mode()[0])\n# For Exterior1st and Exterior2nd since very few missing will take most predominant one\nmerge_df['Exterior1st'] = merge_df['Exterior1st'].fillna(merge_df['Exterior1st'].mode()[0])\nmerge_df['Exterior2nd'] = merge_df['Exterior2nd'].fillna(merge_df['Exterior2nd'].mode()[0])\n# For KitchenQual 'TA' is predominant so will take that for missing ones\nmerge_df['KitchenQual'] = merge_df['KitchenQual'].fillna(merge_df['KitchenQual'].mode()[0])\n# For SaleType 'WD' is predominant so will take that for missing ones\nmerge_df['SaleType'] = merge_df['SaleType'].fillna(merge_df['SaleType'].mode()[0])\n# For Functional if no data that means typical\nmerge_df[\"Functional\"] = merge_df[\"Functional\"].fillna(merge_df.Functional.mode()[0])\n# For SaleType 'WD' is predominant so will take that for missing ones\nmerge_df['SaleType'] = merge_df['SaleType'].fillna(merge_df['SaleType'].mode()[0])\n\n\ntest_cat_cols = ('FireplaceQu', 'BsmtQual', 'BsmtCond', 'GarageQual', 'GarageCond', \n        'ExterQual', 'ExterCond','HeatingQC', 'KitchenQual', 'BsmtFinType1', \n        'BsmtFinType2', 'Functional', 'BsmtExposure', 'GarageFinish', 'LandSlope',\n        'LotShape', 'PavedDrive', 'CentralAir', 'MSSubClass', 'OverallCond', \n        'YrSold', 'MoSold')\n#apply LabelEncoder to categorical features\nfor col in test_cat_cols:\n    lb = LabelEncoder() \n    lb.fit(list(merge_df[col].values)) \n    merge_df[col] = lb.transform(list(merge_df[col].values))\n\n#generating list of numerical columns\nNumFeaturesTest = []\nfor col in list(merge_df):\n    if merge_df[col].dtypes != 'object':\n        NumFeaturesTest.append(col)      \nmerge_df['Total_Bathrooms'] = (merge_df['FullBath'] + (0.5 * merge_df['HalfBath']) +\n                               merge_df['BsmtFullBath'] + (0.5 * merge_df['BsmtHalfBath']))\nmerge_df['YrBltRemod'] = merge_df['YearBuilt'] + merge_df['YearRemodAdd']\nmerge_df['Total_SF'] = merge_df['TotalBsmtSF'] + merge_df['1stFlrSF'] + merge_df['2ndFlrSF']\nmerge_df[\"LivLotRatio\"] = merge_df['GrLivArea']/merge_df['LotArea']\n\nskewed_feats_test = merge_df[NumFeaturesTest].apply(lambda x: skew(x.dropna())).sort_values(ascending=False)\nskewed_df_test = pd.DataFrame()\nskewed_df_test['Skewness_value'] = skewed_feats_test \n\nskewness_test = skewed_df_test[abs(skewed_df_test) > 0.70]\n\nskewed_features_test = skewness_test.index\nlam = 0.15\nfor feat in skewed_features_test:\n  \n    merge_df[feat] = boxcox1p(merge_df[feat], lam)\n\nfinal_dummy_df = pd.get_dummies(merge_df,drop_first=True)\nprint(\"Shape of Merged data : {}\".format(final_dummy_df.shape))","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:24.872742Z","iopub.execute_input":"2022-07-05T06:44:24.873366Z","iopub.status.idle":"2022-07-05T06:44:25.174476Z","shell.execute_reply.started":"2022-07-05T06:44:24.873324Z","shell.execute_reply":"2022-07-05T06:44:25.173308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_dummy_df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:25.176106Z","iopub.execute_input":"2022-07-05T06:44:25.176834Z","iopub.status.idle":"2022-07-05T06:44:25.193962Z","shell.execute_reply.started":"2022-07-05T06:44:25.176775Z","shell.execute_reply":"2022-07-05T06:44:25.192677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Model Building**","metadata":{}},{"cell_type":"markdown","source":"**1. Train and Test Data Split**","metadata":{}},{"cell_type":"code","source":"train_data = final_dummy_df.iloc[:len(df_train),:]\ntrain_data = pd.concat([train_data,df_train['SalePrice']],axis=1)\ntest_data = final_dummy_df.iloc[len(df_train):,:]\nprint(train_data.shape,test_data.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:25.195504Z","iopub.execute_input":"2022-07-05T06:44:25.196000Z","iopub.status.idle":"2022-07-05T06:44:25.209003Z","shell.execute_reply.started":"2022-07-05T06:44:25.195961Z","shell.execute_reply":"2022-07-05T06:44:25.207732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**2. Separate dependent and independent variable from train data**","metadata":{}},{"cell_type":"code","source":"# Separate dependent and independent variable from train data\nX = train_data.drop('SalePrice', axis=1)\ny = train_data['SalePrice']","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:25.211077Z","iopub.execute_input":"2022-07-05T06:44:25.211930Z","iopub.status.idle":"2022-07-05T06:44:25.220366Z","shell.execute_reply.started":"2022-07-05T06:44:25.211888Z","shell.execute_reply":"2022-07-05T06:44:25.219200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X.shape,y.shape,test_data.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:25.221924Z","iopub.execute_input":"2022-07-05T06:44:25.222789Z","iopub.status.idle":"2022-07-05T06:44:25.230850Z","shell.execute_reply.started":"2022-07-05T06:44:25.222711Z","shell.execute_reply":"2022-07-05T06:44:25.229644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**3. Helper Function**","metadata":{}},{"cell_type":"code","source":"#Validation function\nkfold = KFold(n_splits=10, random_state=50, shuffle=True)\n\ndef rmse_cv(model, X=X):\n    rmse = np.sqrt(-cross_val_score(model, X, y, scoring=\"neg_mean_squared_error\", cv=kfold))\n    return rmse","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:25.233278Z","iopub.execute_input":"2022-07-05T06:44:25.234213Z","iopub.status.idle":"2022-07-05T06:44:25.241404Z","shell.execute_reply.started":"2022-07-05T06:44:25.234170Z","shell.execute_reply":"2022-07-05T06:44:25.240257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define error metrics\ndef rmsle(y, y_pred):\n    return np.sqrt(mean_squared_error(y, y_pred))","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:25.243012Z","iopub.execute_input":"2022-07-05T06:44:25.243577Z","iopub.status.idle":"2022-07-05T06:44:25.257270Z","shell.execute_reply.started":"2022-07-05T06:44:25.243533Z","shell.execute_reply":"2022-07-05T06:44:25.255785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**4. Lets try Different Models**","metadata":{}},{"cell_type":"markdown","source":"### Lasso Regression","metadata":{}},{"cell_type":"code","source":"model_lasso = make_pipeline(StandardScaler(),Lasso())","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:25.259136Z","iopub.execute_input":"2022-07-05T06:44:25.259927Z","iopub.status.idle":"2022-07-05T06:44:25.269336Z","shell.execute_reply.started":"2022-07-05T06:44:25.259885Z","shell.execute_reply":"2022-07-05T06:44:25.268227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = {}\n\nscore = rmse_cv(model_lasso)\nprint(\"model_lasso: {:.4f} ({:.4f})\".format(score.mean(), score.std()))\nscores['model_lasso'] = (score.mean(), score.std())","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:25.270518Z","iopub.execute_input":"2022-07-05T06:44:25.271452Z","iopub.status.idle":"2022-07-05T06:44:26.067573Z","shell.execute_reply.started":"2022-07-05T06:44:25.271394Z","shell.execute_reply":"2022-07-05T06:44:26.066208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Ridge Regression**","metadata":{}},{"cell_type":"code","source":"model_ridge = make_pipeline(StandardScaler(),Ridge())","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:26.069713Z","iopub.execute_input":"2022-07-05T06:44:26.071610Z","iopub.status.idle":"2022-07-05T06:44:26.080300Z","shell.execute_reply.started":"2022-07-05T06:44:26.071566Z","shell.execute_reply":"2022-07-05T06:44:26.078527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = {}\n\nscore = rmse_cv(model_ridge)\nprint(\"model Ridge: {:.4f} ({:.4f})\".format(score.mean(), score.std()))\nscores['model_ridge'] = (score.mean(), score.std())","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:26.085240Z","iopub.execute_input":"2022-07-05T06:44:26.085667Z","iopub.status.idle":"2022-07-05T06:44:26.883583Z","shell.execute_reply.started":"2022-07-05T06:44:26.085623Z","shell.execute_reply":"2022-07-05T06:44:26.882383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Random Forest","metadata":{}},{"cell_type":"code","source":"# Random Forest Regressor\nmodel_rf = RandomForestRegressor(n_estimators=1000,\n                          max_depth=15,\n                          min_samples_split=5,\n                          min_samples_leaf=5,\n                          max_features=None,\n                          oob_score=True,\n                          random_state=50)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:26.885736Z","iopub.execute_input":"2022-07-05T06:44:26.886489Z","iopub.status.idle":"2022-07-05T06:44:26.896187Z","shell.execute_reply.started":"2022-07-05T06:44:26.886427Z","shell.execute_reply":"2022-07-05T06:44:26.894198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = rmse_cv(model_rf)\nprint(\"model_rf: {:.4f} ({:.4f})\".format(score.mean(), score.std()))\nscores['model_rf'] = (score.mean(), score.std())","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:44:26.899436Z","iopub.execute_input":"2022-07-05T06:44:26.900498Z","iopub.status.idle":"2022-07-05T06:47:09.026453Z","shell.execute_reply.started":"2022-07-05T06:44:26.900412Z","shell.execute_reply":"2022-07-05T06:47:09.025248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### XGBoost","metadata":{}},{"cell_type":"code","source":"# XGBoost Regressor\nmodel_xgb = XGBRegressor(learning_rate=0.01,\n                       n_estimators=7000,\n                       max_depth=15,\n                       min_child_weight=1.5,\n                       gamma=0.0,\n                       subsample=0.2,\n                       colsample_bytree=0.7,\n                       objective='reg:squarederror',\n                       nthread=-1,\n                       scale_pos_weight=1,\n                       seed=50,\n                       reg_alpha=0.9,\n                       reg_lambda=0.6,\n                       random_state=50) ","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:47:09.028204Z","iopub.execute_input":"2022-07-05T06:47:09.028913Z","iopub.status.idle":"2022-07-05T06:47:09.036536Z","shell.execute_reply.started":"2022-07-05T06:47:09.028840Z","shell.execute_reply":"2022-07-05T06:47:09.035292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = rmse_cv(model_xgb)\nprint(\"model_xgboost: {:.4f} ({:.4f})\".format(score.mean(), score.std()))\nscores['model_xgb'] = (score.mean(), score.std())","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:47:09.037947Z","iopub.execute_input":"2022-07-05T06:47:09.038489Z","iopub.status.idle":"2022-07-05T06:50:22.610389Z","shell.execute_reply.started":"2022-07-05T06:47:09.038445Z","shell.execute_reply":"2022-07-05T06:50:22.609141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Light GBM","metadata":{}},{"cell_type":"code","source":"model_lgb = LGBMRegressor(objective='regression', \n                       num_leaves=6,\n                       learning_rate=0.01, \n                       n_estimators=7500,\n                       max_bin=200, \n                       subsample=0.8 , \n                       subsample_freq=4,  \n                       bagging_seed=50,\n                       colsample_bytree=0.2, \n                       feature_fraction_seed=50,\n                       min_child_weight=0.001, \n                       verbose=-1,\n                       random_state=50) ","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:50:22.611962Z","iopub.execute_input":"2022-07-05T06:50:22.612628Z","iopub.status.idle":"2022-07-05T06:50:22.620705Z","shell.execute_reply.started":"2022-07-05T06:50:22.612581Z","shell.execute_reply":"2022-07-05T06:50:22.619655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = {}\n\nscore = rmse_cv(model_lgb)\nprint(\"model_lightgbm: {:.4f} ({:.4f})\".format(score.mean(), score.std()))\nscores['model_lgb'] = (score.mean(), score.std())","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:50:22.621918Z","iopub.execute_input":"2022-07-05T06:50:22.622603Z","iopub.status.idle":"2022-07-05T06:51:10.757038Z","shell.execute_reply.started":"2022-07-05T06:50:22.622560Z","shell.execute_reply":"2022-07-05T06:51:10.755872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**5. Model fitting and Prediction for different Models**","metadata":{}},{"cell_type":"code","source":"lasso_model = model_lasso.fit(X, y)\nlasso_model_pred = lasso_model.predict(X)\nlasso_pred = np.floor(np.expm1(lasso_model.predict(test_data)))\nprint(rmsle(y, lasso_model_pred))","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:51:10.758644Z","iopub.execute_input":"2022-07-05T06:51:10.759297Z","iopub.status.idle":"2022-07-05T06:51:10.804025Z","shell.execute_reply.started":"2022-07-05T06:51:10.759252Z","shell.execute_reply":"2022-07-05T06:51:10.802906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ridge_model = model_ridge.fit(X, y)\nridge_model_pred = ridge_model.predict(X)\nridge_pred = np.floor(np.expm1(ridge_model.predict(test_data)))\nprint(rmsle(y, ridge_model_pred))","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:51:10.805561Z","iopub.execute_input":"2022-07-05T06:51:10.806225Z","iopub.status.idle":"2022-07-05T06:51:10.881132Z","shell.execute_reply.started":"2022-07-05T06:51:10.806155Z","shell.execute_reply":"2022-07-05T06:51:10.879507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf_model = model_rf.fit(X, y)\nrf_model_pred = rf_model.predict(X)\nrf_pred = np.floor(np.expm1(rf_model.predict(test_data)))\nprint(rmsle(y, rf_model_pred))","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:51:10.883906Z","iopub.execute_input":"2022-07-05T06:51:10.885400Z","iopub.status.idle":"2022-07-05T06:51:29.943165Z","shell.execute_reply.started":"2022-07-05T06:51:10.885357Z","shell.execute_reply":"2022-07-05T06:51:29.941952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xgb_model = model_xgb.fit(X, y)\nxgb_model_pred = xgb_model.predict(X)\nxgb_pred = np.floor(np.expm1(xgb_model.predict(test_data)))\nprint(rmsle(y, xgb_model_pred))","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:51:29.944826Z","iopub.execute_input":"2022-07-05T06:51:29.945283Z","iopub.status.idle":"2022-07-05T06:51:50.043798Z","shell.execute_reply.started":"2022-07-05T06:51:29.945240Z","shell.execute_reply":"2022-07-05T06:51:50.042615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lgb_model = model_lgb.fit(X, y)\nlgb_model_pred = lgb_model.predict(X)\nlgb_pred = np.floor(np.expm1(lgb_model.predict(test_data)))\nprint(rmsle(y, lgb_model_pred))","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:51:50.045532Z","iopub.execute_input":"2022-07-05T06:51:50.046204Z","iopub.status.idle":"2022-07-05T06:51:55.571038Z","shell.execute_reply.started":"2022-07-05T06:51:50.046157Z","shell.execute_reply":"2022-07-05T06:51:55.569894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Submission","metadata":{}},{"cell_type":"code","source":"submission = pd.DataFrame({'Id': ID, 'SalePrice': lgb_pred})\n\nsubmission.to_csv('submission.csv',index=False)\n\nprint(\"Submitted successfully!\")","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:51:55.572647Z","iopub.execute_input":"2022-07-05T06:51:55.573149Z","iopub.status.idle":"2022-07-05T06:51:55.592018Z","shell.execute_reply.started":"2022-07-05T06:51:55.573075Z","shell.execute_reply":"2022-07-05T06:51:55.590383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ensemble = ((lgb_pred * 0.30) + (xgb_pred * 0.25)  + (rf_pred * .05) + \n            (ridge_pred * .04) + (lasso_pred * .03))","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:51:55.593607Z","iopub.execute_input":"2022-07-05T06:51:55.594255Z","iopub.status.idle":"2022-07-05T06:51:55.600763Z","shell.execute_reply.started":"2022-07-05T06:51:55.594186Z","shell.execute_reply":"2022-07-05T06:51:55.599424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_ensemble = pd.DataFrame({'Id': ID, 'SalePrice': ensemble})\n\nsubmission_ensemble.to_csv('submission_ensemble.csv',index=False)\n\nprint(\"Submitted successfully!\")","metadata":{"execution":{"iopub.status.busy":"2022-07-05T06:51:55.602642Z","iopub.execute_input":"2022-07-05T06:51:55.603155Z","iopub.status.idle":"2022-07-05T06:51:55.622363Z","shell.execute_reply.started":"2022-07-05T06:51:55.603076Z","shell.execute_reply":"2022-07-05T06:51:55.620581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}