{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import mean_absolute_percentage_error, mean_squared_error, r2_score\nfrom sklearn.preprocessing import StandardScaler, LabelEncoder\nfrom sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor\nfrom sklearn.tree import DecisionTreeRegressor\nfrom sklearn.linear_model import LinearRegression\nfrom xgboost import XGBRegressor","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:49:55.100329Z","iopub.execute_input":"2022-07-24T19:49:55.100977Z","iopub.status.idle":"2022-07-24T19:49:56.972497Z","shell.execute_reply.started":"2022-07-24T19:49:55.100880Z","shell.execute_reply":"2022-07-24T19:49:56.971011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train data","metadata":{}},{"cell_type":"markdown","source":"**Loading the data**","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv(r\"../input/house-prices-advanced-regression-techniques/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:16.554611Z","iopub.execute_input":"2022-07-24T19:50:16.555024Z","iopub.status.idle":"2022-07-24T19:50:16.598565Z","shell.execute_reply.started":"2022-07-24T19:50:16.554991Z","shell.execute_reply":"2022-07-24T19:50:16.597650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:17.983813Z","iopub.execute_input":"2022-07-24T19:50:17.984662Z","iopub.status.idle":"2022-07-24T19:50:18.023046Z","shell.execute_reply.started":"2022-07-24T19:50:17.984620Z","shell.execute_reply":"2022-07-24T19:50:18.021783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:18.846278Z","iopub.execute_input":"2022-07-24T19:50:18.846978Z","iopub.status.idle":"2022-07-24T19:50:18.854328Z","shell.execute_reply.started":"2022-07-24T19:50:18.846929Z","shell.execute_reply":"2022-07-24T19:50:18.853283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:19.591736Z","iopub.execute_input":"2022-07-24T19:50:19.592797Z","iopub.status.idle":"2022-07-24T19:50:19.602101Z","shell.execute_reply.started":"2022-07-24T19:50:19.592740Z","shell.execute_reply":"2022-07-24T19:50:19.600713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dropping the first column\ndf = df.drop(['Id'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:20.366470Z","iopub.execute_input":"2022-07-24T19:50:20.367271Z","iopub.status.idle":"2022-07-24T19:50:20.377602Z","shell.execute_reply.started":"2022-07-24T19:50:20.367226Z","shell.execute_reply":"2022-07-24T19:50:20.376379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:21.158826Z","iopub.execute_input":"2022-07-24T19:50:21.159262Z","iopub.status.idle":"2022-07-24T19:50:21.191967Z","shell.execute_reply.started":"2022-07-24T19:50:21.159213Z","shell.execute_reply":"2022-07-24T19:50:21.190547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe(include='all')","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:21.806559Z","iopub.execute_input":"2022-07-24T19:50:21.806991Z","iopub.status.idle":"2022-07-24T19:50:21.977156Z","shell.execute_reply.started":"2022-07-24T19:50:21.806955Z","shell.execute_reply":"2022-07-24T19:50:21.976012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Cleaning and Preprocess","metadata":{}},{"cell_type":"markdown","source":"Some of the columns have 'NA' as a category of the values.","metadata":{}},{"cell_type":"code","source":"col_with_NA_as_category = ['Alley','BsmtQual','BsmtCond', 'BsmtExposure', 'BsmtFinType1',\n                           'BsmtFinType2','FireplaceQu', 'GarageType', 'GarageFinish',\n                           'GarageQual','GarageCond','PoolQC','Fence', 'MiscFeature']","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:23.983158Z","iopub.execute_input":"2022-07-24T19:50:23.983548Z","iopub.status.idle":"2022-07-24T19:50:23.988699Z","shell.execute_reply.started":"2022-07-24T19:50:23.983516Z","shell.execute_reply":"2022-07-24T19:50:23.987737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checking for null values\nnull = df.isnull().sum()\nnull = null[null.values>0]","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:24.728251Z","iopub.execute_input":"2022-07-24T19:50:24.729265Z","iopub.status.idle":"2022-07-24T19:50:24.738336Z","shell.execute_reply.started":"2022-07-24T19:50:24.729226Z","shell.execute_reply":"2022-07-24T19:50:24.737352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"null.sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:25.617960Z","iopub.execute_input":"2022-07-24T19:50:25.618690Z","iopub.status.idle":"2022-07-24T19:50:25.626611Z","shell.execute_reply.started":"2022-07-24T19:50:25.618653Z","shell.execute_reply":"2022-07-24T19:50:25.625808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We should remove the null values only for those columns which does not have 'NA' as a category of values.","metadata":{}},{"cell_type":"code","source":"null_col = [i for i in null.index if i not in col_with_NA_as_category]\nnull_col","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:27.959994Z","iopub.execute_input":"2022-07-24T19:50:27.961229Z","iopub.status.idle":"2022-07-24T19:50:27.968703Z","shell.execute_reply.started":"2022-07-24T19:50:27.961185Z","shell.execute_reply":"2022-07-24T19:50:27.967905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# removing null values for those columns\ndf[['LotFrontage']] = pd.DataFrame(SimpleImputer(strategy='mean').fit_transform(df[['LotFrontage']]))\ndf[['MasVnrArea']] = pd.DataFrame(SimpleImputer(strategy='mean').fit_transform(df[['MasVnrArea']]))\ndf[['MasVnrType']] = pd.DataFrame(SimpleImputer(strategy='most_frequent').fit_transform(df[['MasVnrType']]))\ndf[['Electrical']] = pd.DataFrame(SimpleImputer(strategy='most_frequent').fit_transform(df[['Electrical']]))\ndf[['GarageYrBlt']] = pd.DataFrame(SimpleImputer(strategy='most_frequent').fit_transform(df[['GarageYrBlt']]))","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:28.655697Z","iopub.execute_input":"2022-07-24T19:50:28.656122Z","iopub.status.idle":"2022-07-24T19:50:28.686347Z","shell.execute_reply.started":"2022-07-24T19:50:28.656082Z","shell.execute_reply":"2022-07-24T19:50:28.685461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# replacing NA with Not Applicable for the columns with \"NA\" as a value category\nfor col in col_with_NA_as_category:\n    df[col].fillna('Not Applicable', inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:29.590658Z","iopub.execute_input":"2022-07-24T19:50:29.591601Z","iopub.status.idle":"2022-07-24T19:50:29.603515Z","shell.execute_reply.started":"2022-07-24T19:50:29.591561Z","shell.execute_reply":"2022-07-24T19:50:29.602031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.isnull().sum()[df.isnull().sum().values>0]","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:30.271709Z","iopub.execute_input":"2022-07-24T19:50:30.272683Z","iopub.status.idle":"2022-07-24T19:50:30.289093Z","shell.execute_reply.started":"2022-07-24T19:50:30.272634Z","shell.execute_reply":"2022-07-24T19:50:30.287922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"No more null values present in the dataset.","metadata":{}},{"cell_type":"markdown","source":"**Our dataset has some categorical columns which contain numerical values**","metadata":{}},{"cell_type":"code","source":"categorical_columns_with_numerical_values = ['MSSubClass','OverallQual','OverallCond','YearBuilt','BsmtFullBath',\n                                            'BsmtHalfBath','FullBath','HalfBath','BedroomAbvGr','KitchenAbvGr','TotRmsAbvGrd',\n                                            'Fireplaces','GarageYrBlt','GarageCars','MoSold','YrSold']","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:32.915882Z","iopub.execute_input":"2022-07-24T19:50:32.916569Z","iopub.status.idle":"2022-07-24T19:50:32.921829Z","shell.execute_reply.started":"2022-07-24T19:50:32.916527Z","shell.execute_reply":"2022-07-24T19:50:32.920637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# changing the data type for those columns\nfor i in categorical_columns_with_numerical_values:\n    df[i] = df[i].astype(str)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:34.014038Z","iopub.execute_input":"2022-07-24T19:50:34.014434Z","iopub.status.idle":"2022-07-24T19:50:34.041147Z","shell.execute_reply.started":"2022-07-24T19:50:34.014401Z","shell.execute_reply":"2022-07-24T19:50:34.040109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checking the corelation between the target variable and the other numerical columns\ncorr = df.corr().sort_values(by='SalePrice', ascending=False)[['SalePrice']]\ncorr","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:34.743052Z","iopub.execute_input":"2022-07-24T19:50:34.743466Z","iopub.status.idle":"2022-07-24T19:50:34.762075Z","shell.execute_reply.started":"2022-07-24T19:50:34.743427Z","shell.execute_reply":"2022-07-24T19:50:34.761038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The numerical columns with very low corelation with the target variable can be removed.","metadata":{}},{"cell_type":"code","source":"col_to_be_removed = corr.index[7:]\ndf = df.drop(col_to_be_removed,axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:36.347460Z","iopub.execute_input":"2022-07-24T19:50:36.348085Z","iopub.status.idle":"2022-07-24T19:50:36.357453Z","shell.execute_reply.started":"2022-07-24T19:50:36.348051Z","shell.execute_reply":"2022-07-24T19:50:36.356306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now dividing all the columns as Categorical and Numerical.","metadata":{}},{"cell_type":"code","source":"numeric_col = [i for i in corr.index[1:] if i not in col_to_be_removed]\ncategorical_col = [i for i in df.columns[:-1] if i not in numeric_col]","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:37.365033Z","iopub.execute_input":"2022-07-24T19:50:37.365432Z","iopub.status.idle":"2022-07-24T19:50:37.371076Z","shell.execute_reply.started":"2022-07-24T19:50:37.365386Z","shell.execute_reply":"2022-07-24T19:50:37.370027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"No. of Categorical columns: \", len(categorical_col))\nprint(\"No. of Numerical columns: \", len(numeric_col))","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:38.032108Z","iopub.execute_input":"2022-07-24T19:50:38.032892Z","iopub.status.idle":"2022-07-24T19:50:38.039188Z","shell.execute_reply.started":"2022-07-24T19:50:38.032837Z","shell.execute_reply":"2022-07-24T19:50:38.038064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Dealing with Numerical columns**","metadata":{}},{"cell_type":"code","source":"# checking for outliers\nk=1\nplt.figure(figsize=(10,8))\nfor col in numeric_col:\n    plt.subplot(2,3,k)\n    sns.boxplot(x=df[col])\n    k+=1\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:39.430057Z","iopub.execute_input":"2022-07-24T19:50:39.431056Z","iopub.status.idle":"2022-07-24T19:50:39.953663Z","shell.execute_reply.started":"2022-07-24T19:50:39.431017Z","shell.execute_reply":"2022-07-24T19:50:39.952518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# a function to remove outliers\ndef remove_outlier(column):\n    p25 = column.describe()[4]\n    p75 = column.describe()[6]\n    IQR = p75 - p25\n    ul = p75 + 1.5*IQR\n    ll = p25 - 1.5*IQR\n    column.mask(column>ul,ul,inplace=True)\n    column.mask(column<ll,ll,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:40.016323Z","iopub.execute_input":"2022-07-24T19:50:40.016752Z","iopub.status.idle":"2022-07-24T19:50:40.024119Z","shell.execute_reply.started":"2022-07-24T19:50:40.016717Z","shell.execute_reply":"2022-07-24T19:50:40.022813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# removing outliers\nfor col in numeric_col:\n    if col!='YearRemodAdd':\n        remove_outlier(df[col])","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:40.670700Z","iopub.execute_input":"2022-07-24T19:50:40.671558Z","iopub.status.idle":"2022-07-24T19:50:40.704762Z","shell.execute_reply.started":"2022-07-24T19:50:40.671492Z","shell.execute_reply":"2022-07-24T19:50:40.703590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checking again\nk=1\nplt.figure(figsize=(10,8))\nfor col in numeric_col:\n    plt.subplot(2,3,k)\n    sns.boxplot(x=df[col])\n    k+=1\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:41.199130Z","iopub.execute_input":"2022-07-24T19:50:41.199569Z","iopub.status.idle":"2022-07-24T19:50:41.697654Z","shell.execute_reply.started":"2022-07-24T19:50:41.199530Z","shell.execute_reply":"2022-07-24T19:50:41.696522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"So all outliers are removed from the dataset","metadata":{}},{"cell_type":"code","source":"# scaling the data\ndf[numeric_col] = StandardScaler().fit_transform(df[numeric_col])","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:42.446501Z","iopub.execute_input":"2022-07-24T19:50:42.446961Z","iopub.status.idle":"2022-07-24T19:50:42.459237Z","shell.execute_reply.started":"2022-07-24T19:50:42.446912Z","shell.execute_reply":"2022-07-24T19:50:42.457822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Dealing with Categorical columns**","metadata":{}},{"cell_type":"code","source":"# a function to create a bar plot for any categorical columns\ndef bar_plot(col):\n    d = df[['SalePrice']].groupby(df[col]).mean().reset_index()\n    sns.barplot(x=d[col], y=d['SalePrice'], order=d.sort_values(by='SalePrice', ascending=True)[col], palette='Greens')\n    plt.title(col+'vs. SalePrice')\n","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:44.598324Z","iopub.execute_input":"2022-07-24T19:50:44.599026Z","iopub.status.idle":"2022-07-24T19:50:44.605451Z","shell.execute_reply.started":"2022-07-24T19:50:44.598986Z","shell.execute_reply":"2022-07-24T19:50:44.604593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# creating barplot for the categorical columns which contains numerical data\nplt.figure(figsize=(20,20))\ni=1\nfor col in categorical_columns_with_numerical_values:\n    plt.subplot(4,4,i)\n    bar_plot(col)\n    i+=1\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:45.546130Z","iopub.execute_input":"2022-07-24T19:50:45.546796Z","iopub.status.idle":"2022-07-24T19:50:50.619666Z","shell.execute_reply.started":"2022-07-24T19:50:45.546757Z","shell.execute_reply":"2022-07-24T19:50:50.618028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# other categorical columns visualisation\nplt.figure(figsize=(20,45))\ni=1\nfor col in [j for j in categorical_col if j not in categorical_columns_with_numerical_values]:\n    plt.subplot(9,5,i)\n    bar_plot(col)\n    i+=1\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:50.621815Z","iopub.execute_input":"2022-07-24T19:50:50.622445Z","iopub.status.idle":"2022-07-24T19:50:57.041104Z","shell.execute_reply.started":"2022-07-24T19:50:50.622395Z","shell.execute_reply":"2022-07-24T19:50:57.039942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The columns for which we can't see much difference in the target variable, those columns can be dropped.","metadata":{}},{"cell_type":"code","source":"# dropping those unnecessary columns\ndrop = ['MoSold','BsmtHalfBath','BsmtFullBath','YrSold','LandSlope']\ndf = df.drop(drop,axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:57.042343Z","iopub.execute_input":"2022-07-24T19:50:57.043115Z","iopub.status.idle":"2022-07-24T19:50:57.051691Z","shell.execute_reply.started":"2022-07-24T19:50:57.043077Z","shell.execute_reply":"2022-07-24T19:50:57.050360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# removing those columns from the categorical columns list\nfor i in drop:\n    categorical_col.remove(i)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:57.055004Z","iopub.execute_input":"2022-07-24T19:50:57.055484Z","iopub.status.idle":"2022-07-24T19:50:57.062193Z","shell.execute_reply.started":"2022-07-24T19:50:57.055436Z","shell.execute_reply":"2022-07-24T19:50:57.060909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(categorical_col)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:57.063767Z","iopub.execute_input":"2022-07-24T19:50:57.064767Z","iopub.status.idle":"2022-07-24T19:50:57.076774Z","shell.execute_reply.started":"2022-07-24T19:50:57.064722Z","shell.execute_reply":"2022-07-24T19:50:57.075388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now we have 54 Categorical columns","metadata":{}},{"cell_type":"code","source":"# Label Encoding those columns\nfor col in categorical_col:\n    df[col] = LabelEncoder().fit_transform(df[col])","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:57.153848Z","iopub.execute_input":"2022-07-24T19:50:57.155039Z","iopub.status.idle":"2022-07-24T19:50:57.216133Z","shell.execute_reply.started":"2022-07-24T19:50:57.154976Z","shell.execute_reply":"2022-07-24T19:50:57.214921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Splitting the data and creating model","metadata":{}},{"cell_type":"code","source":"x = df.iloc[:,:-1]\ny = df.iloc[:,-1]","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:58.545524Z","iopub.execute_input":"2022-07-24T19:50:58.546191Z","iopub.status.idle":"2022-07-24T19:50:58.554087Z","shell.execute_reply.started":"2022-07-24T19:50:58.546144Z","shell.execute_reply":"2022-07-24T19:50:58.553070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=42, test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:59.012994Z","iopub.execute_input":"2022-07-24T19:50:59.013673Z","iopub.status.idle":"2022-07-24T19:50:59.022920Z","shell.execute_reply.started":"2022-07-24T19:50:59.013600Z","shell.execute_reply":"2022-07-24T19:50:59.021948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# creating 5 different models\nRF = RandomForestRegressor().fit(x_train, y_train)\nDT = DecisionTreeRegressor().fit(x_train, y_train)\nGBR = GradientBoostingRegressor().fit(x_train, y_train)\nLR = LinearRegression().fit(x_train, y_train)\nXGB = XGBRegressor().fit(x_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:50:59.618526Z","iopub.execute_input":"2022-07-24T19:50:59.619368Z","iopub.status.idle":"2022-07-24T19:51:01.937910Z","shell.execute_reply.started":"2022-07-24T19:50:59.619327Z","shell.execute_reply":"2022-07-24T19:51:01.936800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# the evaluation metrics\nmodels = [LR, DT, RF, GBR, XGB]\nRMSE = [mean_squared_error(y_test, mod.predict(x_test))**0.5 for mod in models]\nMAPE = [mean_absolute_percentage_error(y_test, mod.predict(x_test)) for mod in models]\nR2_Score = [r2_score(y_test, mod.predict(x_test)) for mod in models]","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:51:01.940244Z","iopub.execute_input":"2022-07-24T19:51:01.940722Z","iopub.status.idle":"2022-07-24T19:51:02.195303Z","shell.execute_reply.started":"2022-07-24T19:51:01.940678Z","shell.execute_reply":"2022-07-24T19:51:02.194370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# comparing 5 models\nModels = ['Linear Regression','Decision Tree','Random Forest','Gradient Boosting','XgBoost']\nevaluation = pd.DataFrame({'Models':Models,'RMSE':RMSE,'MAPE':MAPE, 'R2_Score':R2_Score})","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:51:02.196939Z","iopub.execute_input":"2022-07-24T19:51:02.197639Z","iopub.status.idle":"2022-07-24T19:51:02.205182Z","shell.execute_reply.started":"2022-07-24T19:51:02.197593Z","shell.execute_reply":"2022-07-24T19:51:02.203887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"evaluation","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:51:02.207707Z","iopub.execute_input":"2022-07-24T19:51:02.208578Z","iopub.status.idle":"2022-07-24T19:51:02.231621Z","shell.execute_reply.started":"2022-07-24T19:51:02.208533Z","shell.execute_reply":"2022-07-24T19:51:02.230493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**We got the highest R2_score from XgBoost model**","metadata":{}},{"cell_type":"markdown","source":"# Test Data","metadata":{}},{"cell_type":"code","source":"# loading the data\ntest = pd.read_csv(r\"../input/house-prices-advanced-regression-techniques/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:51:10.600545Z","iopub.execute_input":"2022-07-24T19:51:10.600945Z","iopub.status.idle":"2022-07-24T19:51:10.632691Z","shell.execute_reply.started":"2022-07-24T19:51:10.600912Z","shell.execute_reply":"2022-07-24T19:51:10.631775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# saving this variable\nid = test['Id']","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:51:11.335017Z","iopub.execute_input":"2022-07-24T19:51:11.335643Z","iopub.status.idle":"2022-07-24T19:51:11.340048Z","shell.execute_reply.started":"2022-07-24T19:51:11.335606Z","shell.execute_reply":"2022-07-24T19:51:11.339125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# changing data types\nfor i in categorical_columns_with_numerical_values:\n    test[i] = test[i].astype(str)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:51:12.042122Z","iopub.execute_input":"2022-07-24T19:51:12.042519Z","iopub.status.idle":"2022-07-24T19:51:12.067276Z","shell.execute_reply.started":"2022-07-24T19:51:12.042484Z","shell.execute_reply":"2022-07-24T19:51:12.066126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dropping extra columns\ntest = test.drop([col for col in test if col not in df.columns], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:51:12.938313Z","iopub.execute_input":"2022-07-24T19:51:12.939001Z","iopub.status.idle":"2022-07-24T19:51:12.947885Z","shell.execute_reply.started":"2022-07-24T19:51:12.938960Z","shell.execute_reply":"2022-07-24T19:51:12.946895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Dealing with null values in test data**","metadata":{}},{"cell_type":"code","source":"nulls = test.isnull().sum()[test.isnull().sum().values>0].index\nother_cols = [col for col in nulls if col not in col_with_NA_as_category]","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:51:14.199644Z","iopub.execute_input":"2022-07-24T19:51:14.200076Z","iopub.status.idle":"2022-07-24T19:51:14.217320Z","shell.execute_reply.started":"2022-07-24T19:51:14.200038Z","shell.execute_reply":"2022-07-24T19:51:14.216350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"other_cols","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:51:15.055645Z","iopub.execute_input":"2022-07-24T19:51:15.056065Z","iopub.status.idle":"2022-07-24T19:51:15.063080Z","shell.execute_reply.started":"2022-07-24T19:51:15.056028Z","shell.execute_reply":"2022-07-24T19:51:15.062030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[['MasVnrArea']] = pd.DataFrame(SimpleImputer(strategy='mean').fit_transform(test[['MasVnrArea']]))\ntest[['GarageArea']] = pd.DataFrame(SimpleImputer(strategy='mean').fit_transform(test[['GarageArea']]))\ntest[['TotalBsmtSF']] = pd.DataFrame(SimpleImputer(strategy='mean').fit_transform(test[['TotalBsmtSF']]))","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:51:15.862577Z","iopub.execute_input":"2022-07-24T19:51:15.863646Z","iopub.status.idle":"2022-07-24T19:51:15.882186Z","shell.execute_reply.started":"2022-07-24T19:51:15.863606Z","shell.execute_reply":"2022-07-24T19:51:15.881213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in other_cols:\n    if col not in ['MasVnrArea','GarageArea','TotalBsmtSF']:\n        test[[col]] = pd.DataFrame(SimpleImputer(strategy='most_frequent').fit_transform(test[[col]]))","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:51:16.662334Z","iopub.execute_input":"2022-07-24T19:51:16.663143Z","iopub.status.idle":"2022-07-24T19:51:16.704410Z","shell.execute_reply.started":"2022-07-24T19:51:16.663098Z","shell.execute_reply":"2022-07-24T19:51:16.703290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in col_with_NA_as_category:\n    test[col].fillna('Not Applicable', inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:51:17.393850Z","iopub.execute_input":"2022-07-24T19:51:17.394283Z","iopub.status.idle":"2022-07-24T19:51:17.408828Z","shell.execute_reply.started":"2022-07-24T19:51:17.394248Z","shell.execute_reply":"2022-07-24T19:51:17.407591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isnull().sum()[test.isnull().sum().values>0]","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:51:18.193353Z","iopub.execute_input":"2022-07-24T19:51:18.193780Z","iopub.status.idle":"2022-07-24T19:51:18.215274Z","shell.execute_reply.started":"2022-07-24T19:51:18.193747Z","shell.execute_reply":"2022-07-24T19:51:18.214230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"All nulls removed","metadata":{}},{"cell_type":"code","source":"# outliers check\nk=1\nplt.figure(figsize=(10,8))\nfor col in numeric_col:\n    plt.subplot(2,3,k)\n    sns.boxplot(x=test[col])\n    k+=1\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:51:19.610341Z","iopub.execute_input":"2022-07-24T19:51:19.610767Z","iopub.status.idle":"2022-07-24T19:51:20.087278Z","shell.execute_reply.started":"2022-07-24T19:51:19.610729Z","shell.execute_reply":"2022-07-24T19:51:20.085947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# removing outliers\nfor col in numeric_col:\n    if col!='YearRemodAdd':\n        remove_outlier(test[col])","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:51:20.401233Z","iopub.execute_input":"2022-07-24T19:51:20.402368Z","iopub.status.idle":"2022-07-24T19:51:20.436674Z","shell.execute_reply.started":"2022-07-24T19:51:20.402325Z","shell.execute_reply":"2022-07-24T19:51:20.435606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# normalisation\ntest[numeric_col] = StandardScaler().fit_transform(test[numeric_col])","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:51:21.386364Z","iopub.execute_input":"2022-07-24T19:51:21.386749Z","iopub.status.idle":"2022-07-24T19:51:21.399575Z","shell.execute_reply.started":"2022-07-24T19:51:21.386719Z","shell.execute_reply":"2022-07-24T19:51:21.398410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# LabelEncoding\nfor col in categorical_col:\n    test[col] = LabelEncoder().fit_transform(test[col])","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:51:22.365200Z","iopub.execute_input":"2022-07-24T19:51:22.365626Z","iopub.status.idle":"2022-07-24T19:51:22.426855Z","shell.execute_reply.started":"2022-07-24T19:51:22.365588Z","shell.execute_reply":"2022-07-24T19:51:22.425404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"XgBoost gave the best score so selecting that for predicting test data","metadata":{}},{"cell_type":"code","source":"pred = XGB.predict(test)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:51:24.152841Z","iopub.execute_input":"2022-07-24T19:51:24.153277Z","iopub.status.idle":"2022-07-24T19:51:24.168788Z","shell.execute_reply.started":"2022-07-24T19:51:24.153239Z","shell.execute_reply":"2022-07-24T19:51:24.167912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'Id':id.values,'SalePrice':pred})","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:51:25.089308Z","iopub.execute_input":"2022-07-24T19:51:25.090130Z","iopub.status.idle":"2022-07-24T19:51:25.095723Z","shell.execute_reply.started":"2022-07-24T19:51:25.090085Z","shell.execute_reply":"2022-07-24T19:51:25.094941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:51:26.072850Z","iopub.execute_input":"2022-07-24T19:51:26.073705Z","iopub.status.idle":"2022-07-24T19:51:26.085684Z","shell.execute_reply.started":"2022-07-24T19:51:26.073649Z","shell.execute_reply":"2022-07-24T19:51:26.084719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T19:51:38.699942Z","iopub.execute_input":"2022-07-24T19:51:38.700727Z","iopub.status.idle":"2022-07-24T19:51:38.711508Z","shell.execute_reply.started":"2022-07-24T19:51:38.700681Z","shell.execute_reply":"2022-07-24T19:51:38.710491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}