{"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":"### IMPORT BASIC LIBRAIES","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"id":"Yi1GOnjqV7Dz","execution":{"iopub.status.busy":"2022-08-03T11:41:43.145031Z","iopub.execute_input":"2022-08-03T11:41:43.145454Z","iopub.status.idle":"2022-08-03T11:41:43.788459Z","shell.execute_reply.started":"2022-08-03T11:41:43.145421Z","shell.execute_reply":"2022-08-03T11:41:43.786495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### SETTING THE ENVIRONMENT","metadata":{"id":"95WqrPOPbvI3"}},{"cell_type":"code","source":"%matplotlib inline\nimport warnings\nwarnings.filterwarnings('ignore')\npd.options.display.float_format = \"{:,.1f}\".format","metadata":{"id":"aWMTCuKCbnRu","execution":{"iopub.status.busy":"2022-08-03T11:41:43.791169Z","iopub.execute_input":"2022-08-03T11:41:43.794283Z","iopub.status.idle":"2022-08-03T11:41:43.802133Z","shell.execute_reply.started":"2022-08-03T11:41:43.794234Z","shell.execute_reply":"2022-08-03T11:41:43.800826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### GET DATA","metadata":{"id":"Qiz2rSiMYTCt"}},{"cell_type":"code","source":"train = pd.read_csv(\"../input/house-prices-advanced-regression-techniques/train.csv\")\ntest = pd.read_csv(\"../input/house-prices-advanced-regression-techniques/test.csv\")\nprint(train.shape)\nprint(test.shape)","metadata":{"id":"42EnAYEZWyLI","outputId":"373c46ed-e209-42bf-bd9f-3dc566a5ce28","execution":{"iopub.status.busy":"2022-08-03T11:41:43.803653Z","iopub.execute_input":"2022-08-03T11:41:43.804654Z","iopub.status.idle":"2022-08-03T11:41:43.900743Z","shell.execute_reply.started":"2022-08-03T11:41:43.804607Z","shell.execute_reply":"2022-08-03T11:41:43.899441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### EXPLORE THE DATA INSIDE OUT","metadata":{"id":"OZcPA_jLYVrl"}},{"cell_type":"code","source":"train.info()","metadata":{"id":"xwvxWmbWoaPW","outputId":"6ce059a9-6943-4a57-ae15-399c386ea33f","execution":{"iopub.status.busy":"2022-08-03T11:41:43.902984Z","iopub.execute_input":"2022-08-03T11:41:43.903363Z","iopub.status.idle":"2022-08-03T11:41:43.942019Z","shell.execute_reply.started":"2022-08-03T11:41:43.903332Z","shell.execute_reply":"2022-08-03T11:41:43.940578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.info()","metadata":{"id":"7cVKM7pi4tK0","outputId":"c1f5f64e-d1a8-4a36-b30b-c3224ab702bf","execution":{"iopub.status.busy":"2022-08-03T11:41:43.943354Z","iopub.execute_input":"2022-08-03T11:41:43.943717Z","iopub.status.idle":"2022-08-03T11:41:43.966934Z","shell.execute_reply.started":"2022-08-03T11:41:43.943686Z","shell.execute_reply":"2022-08-03T11:41:43.965398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### CHECK NULL VALUES IN TRAIN & TEST DATASETS","metadata":{"id":"NUbV7u4tVfZj"}},{"cell_type":"code","source":"train.isnull().sum()[train.isnull().sum()>0]","metadata":{"id":"y_fjVj5qXRmD","outputId":"15a5b774-5b28-4dc1-e96d-e8bc95b8cc40","execution":{"iopub.status.busy":"2022-08-03T11:41:43.968832Z","iopub.execute_input":"2022-08-03T11:41:43.969333Z","iopub.status.idle":"2022-08-03T11:41:43.993422Z","shell.execute_reply.started":"2022-08-03T11:41:43.969287Z","shell.execute_reply":"2022-08-03T11:41:43.992183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isnull().sum()[test.isnull().sum()>0]","metadata":{"id":"C88O6SpH48We","outputId":"0de42f1c-0968-4c5f-9d29-38289ee8ad3d","execution":{"iopub.status.busy":"2022-08-03T11:41:43.995319Z","iopub.execute_input":"2022-08-03T11:41:43.996271Z","iopub.status.idle":"2022-08-03T11:41:44.015507Z","shell.execute_reply.started":"2022-08-03T11:41:43.996227Z","shell.execute_reply":"2022-08-03T11:41:44.014438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### CHECK THE NUMERIC COLUMNS DATA DISTRIBUTION","metadata":{"id":"CNBKSHRxVtOc"}},{"cell_type":"code","source":"train.describe().transpose()","metadata":{"id":"O8Qv8WFDgLut","outputId":"0b1fa19d-846a-40f3-cd6d-7a7bf5bc2bec","execution":{"iopub.status.busy":"2022-08-03T11:41:44.016708Z","iopub.execute_input":"2022-08-03T11:41:44.017766Z","iopub.status.idle":"2022-08-03T11:41:44.123282Z","shell.execute_reply.started":"2022-08-03T11:41:44.017731Z","shell.execute_reply":"2022-08-03T11:41:44.122166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.describe().transpose()","metadata":{"id":"k9sOLwPo5I11","outputId":"27749318-a17e-493d-a0f9-8c83afa18994","execution":{"iopub.status.busy":"2022-08-03T11:41:44.124667Z","iopub.execute_input":"2022-08-03T11:41:44.124981Z","iopub.status.idle":"2022-08-03T11:41:44.227208Z","shell.execute_reply.started":"2022-08-03T11:41:44.124953Z","shell.execute_reply":"2022-08-03T11:41:44.225880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### CHECK CORRELATION","metadata":{"id":"NiPHNIZkWb82"}},{"cell_type":"code","source":"train_corr = train.corr()\nfiltered_corr = train_corr[((train_corr >= 0.4) | (train_corr <= -0.4)) & (train_corr !=1.0)]\nplt.figure(figsize=(30,10))\nsns.heatmap(filtered_corr, annot=True, cmap=\"Greens\")\nplt.show()","metadata":{"id":"j_XzhX_wb8vO","outputId":"3a896daf-0bd6-487f-e7af-eedc7b785ed0","execution":{"iopub.status.busy":"2022-08-03T11:41:44.232312Z","iopub.execute_input":"2022-08-03T11:41:44.232892Z","iopub.status.idle":"2022-08-03T11:41:45.781856Z","shell.execute_reply.started":"2022-08-03T11:41:44.232852Z","shell.execute_reply":"2022-08-03T11:41:45.780587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### PLOTTING HIGHLY CORRELATED VARIABLES AGAINST THE TARGET VARIABLE","metadata":{"id":"Ks8kN-poWsYu"}},{"cell_type":"code","source":"print(train[['OverallQual', 'SalePrice']].corr())\nplt.figure(figsize=(10,5))\nsns.scatterplot(data=train,x='OverallQual', y='SalePrice',color='red')\nplt.show()","metadata":{"id":"-Hy1npfftNBY","outputId":"13d51efb-e462-42f0-ce38-b644bd305eae","execution":{"iopub.status.busy":"2022-08-03T11:41:45.783417Z","iopub.execute_input":"2022-08-03T11:41:45.783757Z","iopub.status.idle":"2022-08-03T11:41:45.978677Z","shell.execute_reply.started":"2022-08-03T11:41:45.783727Z","shell.execute_reply":"2022-08-03T11:41:45.977773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train[['GrLivArea', 'SalePrice']].corr())\nplt.figure(figsize=(10,5))\nsns.scatterplot(data=train,x='GrLivArea', y='SalePrice',color='red')\nplt.show()","metadata":{"id":"REXqAbqevMfH","outputId":"6fbdf71a-9683-40c1-b82d-e6328b8769a2","execution":{"iopub.status.busy":"2022-08-03T11:41:45.980259Z","iopub.execute_input":"2022-08-03T11:41:45.980994Z","iopub.status.idle":"2022-08-03T11:41:46.166946Z","shell.execute_reply.started":"2022-08-03T11:41:45.980954Z","shell.execute_reply":"2022-08-03T11:41:46.165627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train[['TotalBsmtSF', 'SalePrice']].corr())\nplt.figure(figsize=(10,5))\nsns.scatterplot(data=train,x='TotalBsmtSF', y='SalePrice',color='red')\nplt.show()","metadata":{"id":"6V4GYQySu3YS","outputId":"188817e4-15ac-4fad-db39-e36f2d266725","execution":{"iopub.status.busy":"2022-08-03T11:41:46.168492Z","iopub.execute_input":"2022-08-03T11:41:46.169365Z","iopub.status.idle":"2022-08-03T11:41:46.363125Z","shell.execute_reply.started":"2022-08-03T11:41:46.169318Z","shell.execute_reply":"2022-08-03T11:41:46.361814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train[['1stFlrSF', 'SalePrice']].corr())\nplt.figure(figsize=(10,5))\nsns.scatterplot(data=train,x='1stFlrSF', y='SalePrice',color='red')\nplt.show()","metadata":{"id":"inlicyfnvBfu","outputId":"6c7bee10-e891-4113-bde3-3f6357f677b5","execution":{"iopub.status.busy":"2022-08-03T11:41:46.365146Z","iopub.execute_input":"2022-08-03T11:41:46.365599Z","iopub.status.idle":"2022-08-03T11:41:46.545527Z","shell.execute_reply.started":"2022-08-03T11:41:46.365535Z","shell.execute_reply":"2022-08-03T11:41:46.544209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train[['GarageCars', 'SalePrice']].corr())\nplt.figure(figsize=(10,5))\nsns.scatterplot(data=train,x='GarageCars', y='SalePrice',color='red')\nplt.show()","metadata":{"id":"w-ounr6NvT6-","outputId":"716047fd-5847-4ebf-a87f-6470b80ebf12","execution":{"iopub.status.busy":"2022-08-03T11:41:46.547596Z","iopub.execute_input":"2022-08-03T11:41:46.548049Z","iopub.status.idle":"2022-08-03T11:41:46.745070Z","shell.execute_reply.started":"2022-08-03T11:41:46.548004Z","shell.execute_reply":"2022-08-03T11:41:46.743806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train[['GarageArea', 'SalePrice']].corr())\nplt.figure(figsize=(10,5))\nsns.scatterplot(data=train,x='GarageArea', y='SalePrice',color='red')\nplt.show()","metadata":{"id":"74yP0wXTvi_t","outputId":"6814b4f0-3032-40ea-c17c-eb6e8ec765c6","execution":{"iopub.status.busy":"2022-08-03T11:41:46.748827Z","iopub.execute_input":"2022-08-03T11:41:46.749337Z","iopub.status.idle":"2022-08-03T11:41:46.944817Z","shell.execute_reply.started":"2022-08-03T11:41:46.749292Z","shell.execute_reply":"2022-08-03T11:41:46.943237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### SPLIT CATEGORICAL & NUMERIC COLUMNS FOR TRAIN & TEST DATASETS","metadata":{"id":"-PQy55UYvo26"}},{"cell_type":"code","source":"train_cat = train.select_dtypes(include='object')\ntrain_num = train.select_dtypes(exclude='object')","metadata":{"id":"yHWwx-BAvNti","execution":{"iopub.status.busy":"2022-08-03T11:41:46.948258Z","iopub.execute_input":"2022-08-03T11:41:46.949337Z","iopub.status.idle":"2022-08-03T11:41:46.958905Z","shell.execute_reply.started":"2022-08-03T11:41:46.949294Z","shell.execute_reply":"2022-08-03T11:41:46.957387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_cat.head()","metadata":{"id":"0sM4z15Lwf28","outputId":"c1cf1008-3052-4ea3-d891-9aa056f20a5e","execution":{"iopub.status.busy":"2022-08-03T11:41:46.960778Z","iopub.execute_input":"2022-08-03T11:41:46.961164Z","iopub.status.idle":"2022-08-03T11:41:46.994882Z","shell.execute_reply.started":"2022-08-03T11:41:46.961129Z","shell.execute_reply":"2022-08-03T11:41:46.993341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_num.head()","metadata":{"id":"iYfwUPHBws42","outputId":"e0ba41bf-7fe2-4a09-bb18-18241f247a7a","execution":{"iopub.status.busy":"2022-08-03T11:41:46.996873Z","iopub.execute_input":"2022-08-03T11:41:46.997726Z","iopub.status.idle":"2022-08-03T11:41:47.022506Z","shell.execute_reply.started":"2022-08-03T11:41:46.997674Z","shell.execute_reply":"2022-08-03T11:41:47.021334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_cat = test.select_dtypes(include='object')\ntest_num = test.select_dtypes(exclude='object')","metadata":{"id":"FeVDJBsr7ZQP","execution":{"iopub.status.busy":"2022-08-03T11:41:47.024063Z","iopub.execute_input":"2022-08-03T11:41:47.024506Z","iopub.status.idle":"2022-08-03T11:41:47.036810Z","shell.execute_reply.started":"2022-08-03T11:41:47.024462Z","shell.execute_reply":"2022-08-03T11:41:47.035932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### IMPUTE NULL VALUES FROM NUMERIC COLUMNS","metadata":{"id":"Hv5YkbLWxHkL"}},{"cell_type":"code","source":"train_num.isnull().sum()[train_num.isnull().sum()>0]","metadata":{"id":"ZM7WRF5lxFRf","outputId":"cab555ce-ff3f-48d4-c5d6-ac1fd7e0ec8d","execution":{"iopub.status.busy":"2022-08-03T11:41:47.038541Z","iopub.execute_input":"2022-08-03T11:41:47.039884Z","iopub.status.idle":"2022-08-03T11:41:47.052457Z","shell.execute_reply.started":"2022-08-03T11:41:47.039737Z","shell.execute_reply":"2022-08-03T11:41:47.051505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_num.isnull().sum()[test_num.isnull().sum()>0]","metadata":{"id":"gBmJ0ttD9MSu","outputId":"078c46d5-035d-4c2a-b816-4e8a5623451f","execution":{"iopub.status.busy":"2022-08-03T11:41:47.055403Z","iopub.execute_input":"2022-08-03T11:41:47.056244Z","iopub.status.idle":"2022-08-03T11:41:47.069732Z","shell.execute_reply.started":"2022-08-03T11:41:47.056206Z","shell.execute_reply":"2022-08-03T11:41:47.068133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### CHECK CORRELATION BETWEEN NUMBERIC COLUMNS WITH LARGE MISSING VALUES","metadata":{"id":"EiWMRe7KXUpg"}},{"cell_type":"code","source":"train_num[['LotArea','1stFlrSF','LotFrontage']].corr()","metadata":{"id":"FERzmJrox3lV","outputId":"6d045ab4-a736-451e-f1c4-b20d69dde374","execution":{"iopub.status.busy":"2022-08-03T11:41:47.071682Z","iopub.execute_input":"2022-08-03T11:41:47.072035Z","iopub.status.idle":"2022-08-03T11:41:47.085468Z","shell.execute_reply.started":"2022-08-03T11:41:47.072005Z","shell.execute_reply":"2022-08-03T11:41:47.084228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.regplot(data=train_num,x='LotArea', y='LotFrontage')\nplt.show()","metadata":{"id":"Vds1-7NezE87","outputId":"fec4e4f7-7a0f-487d-88b9-ae20290ea6a8","execution":{"iopub.status.busy":"2022-08-03T11:41:47.087152Z","iopub.execute_input":"2022-08-03T11:41:47.087888Z","iopub.status.idle":"2022-08-03T11:41:47.396931Z","shell.execute_reply.started":"2022-08-03T11:41:47.087842Z","shell.execute_reply":"2022-08-03T11:41:47.395625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.regplot(data=train_num,x='1stFlrSF', y='LotFrontage')\nplt.show()","metadata":{"id":"OXWzHVgazu1t","outputId":"d09eccad-3a0f-4415-d59c-0fc0d55bf745","execution":{"iopub.status.busy":"2022-08-03T11:41:47.398993Z","iopub.execute_input":"2022-08-03T11:41:47.400032Z","iopub.status.idle":"2022-08-03T11:41:47.700594Z","shell.execute_reply.started":"2022-08-03T11:41:47.399966Z","shell.execute_reply":"2022-08-03T11:41:47.699349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### IMPUT LARGE MISSING VALUES USING REGRESSION","metadata":{"id":"DssB7TkR0N6_"}},{"cell_type":"code","source":"from sklearn.linear_model import LinearRegression\nlr = LinearRegression()\nlr.fit(train_num[['LotArea', '1stFlrSF']][train_num['LotFrontage'].isnull()==False], train_num['LotFrontage'][train_num['LotFrontage'].isnull()==False])\nprint(lr.coef_)\nprint(lr.intercept_)","metadata":{"id":"KAu5tDRG0yAw","outputId":"e64e20de-a108-4c57-eb27-e294875e71c6","execution":{"iopub.status.busy":"2022-08-03T11:41:47.702403Z","iopub.execute_input":"2022-08-03T11:41:47.703705Z","iopub.status.idle":"2022-08-03T11:41:48.091335Z","shell.execute_reply.started":"2022-08-03T11:41:47.703655Z","shell.execute_reply":"2022-08-03T11:41:48.090140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_num['LotFrontage'][train_num['LotFrontage'].isnull()] = (lr.intercept_) + (train_num['LotArea']*lr.coef_[0]) + (train_num['1stFlrSF']*lr.coef_[1])","metadata":{"id":"3QkFyFdBJ-3p","execution":{"iopub.status.busy":"2022-08-03T11:41:48.092776Z","iopub.execute_input":"2022-08-03T11:41:48.093100Z","iopub.status.idle":"2022-08-03T11:41:48.105147Z","shell.execute_reply.started":"2022-08-03T11:41:48.093071Z","shell.execute_reply":"2022-08-03T11:41:48.103904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_num['LotFrontage'][test_num['LotFrontage'].isnull()] = (lr.intercept_) + (test_num['LotArea']*lr.coef_[0]) + (test_num['1stFlrSF']*lr.coef_[1])","metadata":{"id":"dFRiEwwp8fJg","execution":{"iopub.status.busy":"2022-08-03T11:41:48.113849Z","iopub.execute_input":"2022-08-03T11:41:48.114251Z","iopub.status.idle":"2022-08-03T11:41:48.125298Z","shell.execute_reply.started":"2022-08-03T11:41:48.114220Z","shell.execute_reply":"2022-08-03T11:41:48.123956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### IMPUTE SMALL MISSING VALUES WITH MEAN OR MEDIAN","metadata":{"id":"rD8Nzgb5YZ6x"}},{"cell_type":"code","source":"train_num['MasVnrArea'].fillna(train_num['MasVnrArea'].median(), inplace=True)\ntrain_num['GarageYrBlt'].fillna(train_num['GarageYrBlt'].median(), inplace=True)","metadata":{"id":"04b8JWZT9B7B","execution":{"iopub.status.busy":"2022-08-03T11:41:48.127061Z","iopub.execute_input":"2022-08-03T11:41:48.127588Z","iopub.status.idle":"2022-08-03T11:41:48.139934Z","shell.execute_reply.started":"2022-08-03T11:41:48.127524Z","shell.execute_reply":"2022-08-03T11:41:48.138576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_num['MasVnrArea'].fillna(train_num['MasVnrArea'].median(), inplace=True)\ntest_num['GarageYrBlt'].fillna(train_num['GarageYrBlt'].median(), inplace=True)\ntest_num['MasVnrArea'].fillna(train_num['MasVnrArea'].median(), inplace=True)\ntest_num['BsmtFinSF1'].fillna(train_num['BsmtFinSF1'].median(), inplace=True)\ntest_num['BsmtFinSF2'].fillna(train_num['BsmtFinSF2'].median(), inplace=True)\ntest_num['BsmtUnfSF'].fillna(train_num['BsmtUnfSF'].median(), inplace=True)\ntest_num['TotalBsmtSF'].fillna(train_num['TotalBsmtSF'].median(), inplace=True)\ntest_num['BsmtFullBath'].fillna(train_num['BsmtFullBath'].median(), inplace=True)\ntest_num['BsmtHalfBath'].fillna(train_num['BsmtHalfBath'].median(), inplace=True)\ntest_num['GarageYrBlt'].fillna(train_num['GarageYrBlt'].median(), inplace=True)\ntest_num['GarageCars'].fillna(train_num['GarageCars'].median(), inplace=True)\ntest_num['GarageArea'].fillna(train_num['GarageArea'].median(), inplace=True)","metadata":{"id":"H6tnBJcnM6tf","execution":{"iopub.status.busy":"2022-08-03T11:41:48.141769Z","iopub.execute_input":"2022-08-03T11:41:48.142228Z","iopub.status.idle":"2022-08-03T11:41:48.165750Z","shell.execute_reply.started":"2022-08-03T11:41:48.142185Z","shell.execute_reply":"2022-08-03T11:41:48.164517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_num.isnull().sum()[train_num.isnull().sum()>0]","metadata":{"id":"B0rPhx3wQO1n","outputId":"cb586a50-c56d-4fe5-f91b-88af8f2737f5","execution":{"iopub.status.busy":"2022-08-03T11:41:48.168109Z","iopub.execute_input":"2022-08-03T11:41:48.168483Z","iopub.status.idle":"2022-08-03T11:41:48.183926Z","shell.execute_reply.started":"2022-08-03T11:41:48.168450Z","shell.execute_reply":"2022-08-03T11:41:48.182490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_num.isnull().sum()[test_num.isnull().sum()>0]","metadata":{"id":"IY-Oj6iU91hD","outputId":"b8b0add3-ea51-4a07-ca72-1fcfdcb95478","execution":{"iopub.status.busy":"2022-08-03T11:41:48.185552Z","iopub.execute_input":"2022-08-03T11:41:48.185950Z","iopub.status.idle":"2022-08-03T11:41:48.197992Z","shell.execute_reply.started":"2022-08-03T11:41:48.185918Z","shell.execute_reply":"2022-08-03T11:41:48.196780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### IMPUTING MISSING VALUES FROM CATEGORICAL COLUMNS","metadata":{"id":"tFbSBSmBSde1"}},{"cell_type":"code","source":"train_cat.isnull().sum()[train_cat.isnull().sum()>0]","metadata":{"id":"hImZKv6DSw5P","outputId":"922242ce-f4bb-4991-b670-0572086fbc62","execution":{"iopub.status.busy":"2022-08-03T11:41:48.199612Z","iopub.execute_input":"2022-08-03T11:41:48.200076Z","iopub.status.idle":"2022-08-03T11:41:48.220525Z","shell.execute_reply.started":"2022-08-03T11:41:48.200040Z","shell.execute_reply":"2022-08-03T11:41:48.218661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_cat.isnull().sum()[test_cat.isnull().sum()>0]","metadata":{"id":"BIzsfU5y-C0m","outputId":"d0418bc8-1e5c-42f0-c5d6-6c006cef3bcf","execution":{"iopub.status.busy":"2022-08-03T11:41:48.222732Z","iopub.execute_input":"2022-08-03T11:41:48.223139Z","iopub.status.idle":"2022-08-03T11:41:48.243073Z","shell.execute_reply.started":"2022-08-03T11:41:48.223104Z","shell.execute_reply":"2022-08-03T11:41:48.241762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### DIVIDE COLUMNS BASED ON NA IN DATA DESCRIPTION","metadata":{"id":"BgJL4GkSY6Hi"}},{"cell_type":"code","source":"# Replacing with Mode\nl_1=['MSZoning', 'Utilities', 'Exterior1st', 'Exterior2nd', 'MasVnrType', 'Electrical', 'KitchenQual', 'Functional','SaleType']\nl_2=['Alley', 'BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2', \n           'FireplaceQu', 'GarageType', 'GarageFinish', 'GarageQual', 'GarageCond','PoolQC', 'Fence', 'MiscFeature']\nfor i in l_1:\n  train_cat[i].fillna(train_cat[i].mode()[0],inplace=True)\n  test_cat[i].fillna(train_cat[i].mode()[0],inplace=True)\n\n# Replacing with 'N_A' (Not Available)\nfor i in l_2:\n  train_cat[i].fillna('N_A',inplace=True)\n  test_cat[i].fillna('N_A',inplace=True)","metadata":{"id":"2Y919VG3jzFq","execution":{"iopub.status.busy":"2022-08-03T11:41:48.244170Z","iopub.execute_input":"2022-08-03T11:41:48.244518Z","iopub.status.idle":"2022-08-03T11:41:48.285021Z","shell.execute_reply.started":"2022-08-03T11:41:48.244488Z","shell.execute_reply":"2022-08-03T11:41:48.283880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_cat.isnull().sum()[train_cat.isnull().sum()>0]","metadata":{"id":"DAQDF319n18z","outputId":"bbb0fefb-1058-414a-9aa5-5c5dbff79f76","execution":{"iopub.status.busy":"2022-08-03T11:41:48.286366Z","iopub.execute_input":"2022-08-03T11:41:48.286765Z","iopub.status.idle":"2022-08-03T11:41:48.303476Z","shell.execute_reply.started":"2022-08-03T11:41:48.286732Z","shell.execute_reply":"2022-08-03T11:41:48.301876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_cat.isnull().sum()[test_cat.isnull().sum()>0]","metadata":{"id":"Z2oMnn-f-c_I","outputId":"d26297cc-8136-4bb7-f3f5-b29a2eafd4a6","execution":{"iopub.status.busy":"2022-08-03T11:41:48.305453Z","iopub.execute_input":"2022-08-03T11:41:48.305910Z","iopub.status.idle":"2022-08-03T11:41:48.327648Z","shell.execute_reply.started":"2022-08-03T11:41:48.305874Z","shell.execute_reply":"2022-08-03T11:41:48.326314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### EXTRACT TARGET VARIABLE","metadata":{"id":"sRHBiV8i_Fxf"}},{"cell_type":"code","source":"y_train = train_num['SalePrice']\ntrain_num.drop('SalePrice',axis=1,inplace=True)","metadata":{"id":"6g6j6aB4-tL-","execution":{"iopub.status.busy":"2022-08-03T11:41:48.329007Z","iopub.execute_input":"2022-08-03T11:41:48.329726Z","iopub.status.idle":"2022-08-03T11:41:48.336121Z","shell.execute_reply.started":"2022-08-03T11:41:48.329677Z","shell.execute_reply":"2022-08-03T11:41:48.334885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### SCALING & ENCODING","metadata":{"id":"UCDWz0_PoVzt"}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()\nscaler.fit(train_num)\ntrain_num_s = pd.DataFrame(scaler.transform(train_num), columns=train_num.columns, index=train_num.index)\ntest_num_s = pd.DataFrame(scaler.transform(test_num), columns=test_num.columns, index=test_num.index)\ntrain_num_s.head()","metadata":{"id":"5f1AXlzpoUBf","outputId":"6dba9ceb-7457-4b55-f94b-e742e3d10d06","execution":{"iopub.status.busy":"2022-08-03T11:41:48.337278Z","iopub.execute_input":"2022-08-03T11:41:48.338335Z","iopub.status.idle":"2022-08-03T11:41:48.376099Z","shell.execute_reply.started":"2022-08-03T11:41:48.338298Z","shell.execute_reply":"2022-08-03T11:41:48.374722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_num_s.head()","metadata":{"id":"jqEdpGQUArgx","outputId":"2360ab70-d1ab-4ce4-ddc9-5c1124c2c4f4","execution":{"iopub.status.busy":"2022-08-03T11:41:48.377919Z","iopub.execute_input":"2022-08-03T11:41:48.378614Z","iopub.status.idle":"2022-08-03T11:41:48.398293Z","shell.execute_reply.started":"2022-08-03T11:41:48.378550Z","shell.execute_reply":"2022-08-03T11:41:48.397089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import OneHotEncoder\nOHE = OneHotEncoder(drop='first', sparse=False)\nOHE.fit(train_cat)\ntrain_cat_OHE = pd.DataFrame(OHE.transform(train_cat), columns = OHE.get_feature_names_out(train_cat.columns), index=(train_cat.index))\ntest_cat_OHE = pd.DataFrame(OHE.transform(test_cat), columns = OHE.get_feature_names_out(test_cat.columns), index=(test_cat.index))\ntrain_cat_OHE.head()","metadata":{"id":"Yt-NLVQVozag","outputId":"3e4fe385-756a-4601-e907-2ef15bc5c49f","execution":{"iopub.status.busy":"2022-08-03T11:41:48.399898Z","iopub.execute_input":"2022-08-03T11:41:48.400622Z","iopub.status.idle":"2022-08-03T11:41:48.501901Z","shell.execute_reply.started":"2022-08-03T11:41:48.400585Z","shell.execute_reply":"2022-08-03T11:41:48.500546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_cat_OHE.head()","metadata":{"id":"AwG4hPybBJsH","outputId":"da767fb2-e3dd-400d-ae43-40235b15233c","execution":{"iopub.status.busy":"2022-08-03T11:41:48.503519Z","iopub.execute_input":"2022-08-03T11:41:48.504471Z","iopub.status.idle":"2022-08-03T11:41:48.528244Z","shell.execute_reply.started":"2022-08-03T11:41:48.504430Z","shell.execute_reply":"2022-08-03T11:41:48.527279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### CONCAT CATEGORICAL & NUMERIC DATASETS","metadata":{"id":"xwiYRQjBBOZ9"}},{"cell_type":"code","source":"X_train = pd.concat([train_num_s, train_cat_OHE], axis = 1)\nX_test = pd.concat([test_num_s, test_cat_OHE], axis = 1)","metadata":{"id":"MBBKnxvFBTWA","execution":{"iopub.status.busy":"2022-08-03T11:41:48.529537Z","iopub.execute_input":"2022-08-03T11:41:48.530072Z","iopub.status.idle":"2022-08-03T11:41:48.544050Z","shell.execute_reply.started":"2022-08-03T11:41:48.530041Z","shell.execute_reply":"2022-08-03T11:41:48.542551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### LOG TRANSFORMATION OF TARGET VARIABLE","metadata":{"id":"m3vEb1deDBz5"}},{"cell_type":"code","source":"y_train.hist()","metadata":{"id":"1FMowpiwDA3m","outputId":"623a795a-d140-4cda-92bc-2b7fd1bbc4d7","execution":{"iopub.status.busy":"2022-08-03T11:41:48.545585Z","iopub.execute_input":"2022-08-03T11:41:48.546679Z","iopub.status.idle":"2022-08-03T11:41:48.733821Z","shell.execute_reply.started":"2022-08-03T11:41:48.546629Z","shell.execute_reply":"2022-08-03T11:41:48.732422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_log = np.log(y_train)\ny_train_log.hist()","metadata":{"id":"NY_SJbPfDf5j","outputId":"64d4e10c-2a3c-4a45-9d7b-62366bdee74b","execution":{"iopub.status.busy":"2022-08-03T11:41:48.735545Z","iopub.execute_input":"2022-08-03T11:41:48.736273Z","iopub.status.idle":"2022-08-03T11:41:48.888211Z","shell.execute_reply.started":"2022-08-03T11:41:48.736227Z","shell.execute_reply":"2022-08-03T11:41:48.887322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### MODEL SELECTION & PRIDICTION","metadata":{"id":"3Fr9GV_SEsAX"}},{"cell_type":"markdown","source":"1. LINEAR REGRESSION","metadata":{"id":"5KfFUtOhGIhw"}},{"cell_type":"code","source":"from sklearn.linear_model import LinearRegression\nlr_reg = LinearRegression()\nlr_reg.fit(X_train, y_train_log)\ny_train_lr_reg = lr_reg.predict(X_train)\ny_test_lr_reg = lr_reg.predict(X_test)","metadata":{"id":"KR-9xz4bErdb","execution":{"iopub.status.busy":"2022-08-03T11:41:48.889627Z","iopub.execute_input":"2022-08-03T11:41:48.891049Z","iopub.status.idle":"2022-08-03T11:41:48.984020Z","shell.execute_reply.started":"2022-08-03T11:41:48.891000Z","shell.execute_reply":"2022-08-03T11:41:48.982386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"2. LASSO","metadata":{"id":"ETzATXGNGNP_"}},{"cell_type":"code","source":"from sklearn.linear_model import Lasso\nls_reg = Lasso()\nls_reg.fit(X_train, y_train_log)\ny_train_ls_reg = ls_reg.predict(X_train)\ny_test_ls_reg = ls_reg.predict(X_test)","metadata":{"id":"fjldA1kCGcE1","execution":{"iopub.status.busy":"2022-08-03T11:41:48.986449Z","iopub.execute_input":"2022-08-03T11:41:48.986999Z","iopub.status.idle":"2022-08-03T11:41:49.072225Z","shell.execute_reply.started":"2022-08-03T11:41:48.986952Z","shell.execute_reply":"2022-08-03T11:41:49.069734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"3. RIDGE","metadata":{"id":"MqWdufFsG4gn"}},{"cell_type":"code","source":"from sklearn.linear_model import Lasso\nrd_reg = Lasso()\nrd_reg.fit(X_train, y_train_log)\ny_train_rd_reg = rd_reg.predict(X_train)\ny_test_rd_reg = rd_reg.predict(X_test)","metadata":{"id":"J3theg0-G563","execution":{"iopub.status.busy":"2022-08-03T11:41:49.075969Z","iopub.execute_input":"2022-08-03T11:41:49.076516Z","iopub.status.idle":"2022-08-03T11:41:49.144281Z","shell.execute_reply.started":"2022-08-03T11:41:49.076470Z","shell.execute_reply":"2022-08-03T11:41:49.142952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"4. GRADIANT BOOSTING REGRESSION","metadata":{"id":"IFZynHRrHDWD"}},{"cell_type":"code","source":"from sklearn.ensemble import GradientBoostingRegressor\ngb_reg = GradientBoostingRegressor()\ngb_reg.fit(X_train, y_train_log)\ny_train_gb_reg = gb_reg.predict(X_train)\ny_test_gb_reg = gb_reg.predict(X_test)","metadata":{"id":"lA3N8WjJHkzx","execution":{"iopub.status.busy":"2022-08-03T11:41:49.150201Z","iopub.execute_input":"2022-08-03T11:41:49.152191Z","iopub.status.idle":"2022-08-03T11:41:50.438875Z","shell.execute_reply.started":"2022-08-03T11:41:49.152136Z","shell.execute_reply":"2022-08-03T11:41:50.437519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"5. RANDOM FOREST REGRESSION","metadata":{"id":"s-hjkMiRH9N5"}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor\nrf_reg = RandomForestRegressor()\nrf_reg.fit(X_train, y_train_log)\ny_train_rf_reg = rf_reg.predict(X_train)\ny_test_rf_reg = rf_reg.predict(X_test)","metadata":{"id":"Xbr-A0z8H85a","execution":{"iopub.status.busy":"2022-08-03T11:41:50.440221Z","iopub.execute_input":"2022-08-03T11:41:50.440570Z","iopub.status.idle":"2022-08-03T11:41:53.794174Z","shell.execute_reply.started":"2022-08-03T11:41:50.440526Z","shell.execute_reply":"2022-08-03T11:41:53.793292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"6. XG BOOST REGRESSION","metadata":{"id":"Y_G_pLTuIawJ"}},{"cell_type":"code","source":"import xgboost as xg\nxg_reg = xg.XGBRegressor()\nxg_reg.fit(X_train, y_train_log)\ny_train_xg_reg = xg_reg.predict(X_train)\ny_test_xg_reg = xg_reg.predict(X_test)","metadata":{"id":"9Wn-YcQAIdKS","outputId":"61887564-4792-48e3-b63f-894f011e480c","execution":{"iopub.status.busy":"2022-08-03T11:41:53.795292Z","iopub.execute_input":"2022-08-03T11:41:53.796011Z","iopub.status.idle":"2022-08-03T11:41:55.185143Z","shell.execute_reply.started":"2022-08-03T11:41:53.795971Z","shell.execute_reply":"2022-08-03T11:41:55.184057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### EVALUATING THE BEST MODEL","metadata":{"id":"mJMARt-7I-aw"}},{"cell_type":"code","source":"from sklearn.metrics import r2_score, mean_squared_error","metadata":{"id":"7Q2ADSYcJDIf","execution":{"iopub.status.busy":"2022-08-03T11:41:55.186648Z","iopub.execute_input":"2022-08-03T11:41:55.187730Z","iopub.status.idle":"2022-08-03T11:41:55.192679Z","shell.execute_reply.started":"2022-08-03T11:41:55.187684Z","shell.execute_reply":"2022-08-03T11:41:55.191130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr_r2score = r2_score(y_train, np.exp(y_train_lr_reg))\nls_r2score = r2_score(y_train, np.exp(y_train_ls_reg))\nrd_r2score = r2_score(y_train, np.exp(y_train_rd_reg))\ngb_r2score = r2_score(y_train, np.exp(y_train_gb_reg))\nrf_r2score = r2_score(y_train, np.exp(y_train_rf_reg))\nxg_r2score = r2_score(y_train, np.exp(y_train_xg_reg))\n\nlr_rmse = np.sqrt(mean_squared_error(y_train, np.exp(y_train_lr_reg)))\nls_rmse = np.sqrt(mean_squared_error(y_train, np.exp(y_train_ls_reg)))\nrd_rmse = np.sqrt(mean_squared_error(y_train, np.exp(y_train_rd_reg)))\ngb_rmse = np.sqrt(mean_squared_error(y_train, np.exp(y_train_gb_reg)))\nrf_rmse = np.sqrt(mean_squared_error(y_train, np.exp(y_train_rf_reg)))\nxg_rmse = np.sqrt(mean_squared_error(y_train, np.exp(y_train_xg_reg)))","metadata":{"id":"ZxasQXjGJN-D","execution":{"iopub.status.busy":"2022-08-03T11:41:55.194210Z","iopub.execute_input":"2022-08-03T11:41:55.195263Z","iopub.status.idle":"2022-08-03T11:41:55.212494Z","shell.execute_reply.started":"2022-08-03T11:41:55.195225Z","shell.execute_reply":"2022-08-03T11:41:55.211020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.Series([lr_r2score, ls_r2score, rd_r2score, gb_r2score, rf_r2score, xg_r2score], index= ['lr_r2score', 'ls_r2score', 'rd_r2score', 'gb_r2score', 'rf_r2score', 'xg_r2score'])","metadata":{"id":"YtJn3l7VKiSb","outputId":"f0afe79e-77bb-4da6-cb0c-5ab4be4d6bf6","execution":{"iopub.status.busy":"2022-08-03T11:41:55.214077Z","iopub.execute_input":"2022-08-03T11:41:55.214663Z","iopub.status.idle":"2022-08-03T11:41:55.225408Z","shell.execute_reply.started":"2022-08-03T11:41:55.214627Z","shell.execute_reply":"2022-08-03T11:41:55.224505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.Series([lr_rmse, ls_rmse, rd_rmse, gb_rmse, rf_rmse, xg_rmse], index= ['lr_rmse', 'ls_rmse', 'rd_rmse', 'gb_rmse', 'rf_rmse', 'xg_rmse'])","metadata":{"id":"rkyhKlHSLNFL","outputId":"95aec7c3-094a-428a-cac5-641d113a795b","execution":{"iopub.status.busy":"2022-08-03T11:41:55.226727Z","iopub.execute_input":"2022-08-03T11:41:55.227509Z","iopub.status.idle":"2022-08-03T11:41:55.244529Z","shell.execute_reply.started":"2022-08-03T11:41:55.227472Z","shell.execute_reply":"2022-08-03T11:41:55.243211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['SalePrice'] = np.exp(y_test_gb_reg)","metadata":{"id":"Xao2hyYkMT0m","execution":{"iopub.status.busy":"2022-08-03T11:41:55.246006Z","iopub.execute_input":"2022-08-03T11:41:55.246491Z","iopub.status.idle":"2022-08-03T11:41:55.258838Z","shell.execute_reply.started":"2022-08-03T11:41:55.246445Z","shell.execute_reply":"2022-08-03T11:41:55.257790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[['Id', 'SalePrice']].to_csv('output_2_GB.csv', index=False)","metadata":{"id":"ZPZxs5UxNAHR","execution":{"iopub.status.busy":"2022-08-03T11:41:55.260346Z","iopub.execute_input":"2022-08-03T11:41:55.261426Z","iopub.status.idle":"2022-08-03T11:41:55.291040Z","shell.execute_reply.started":"2022-08-03T11:41:55.261391Z","shell.execute_reply":"2022-08-03T11:41:55.290044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### GB_boost - Looks best on Kaggle","metadata":{"id":"Sc2bM93rQsWu"}}]}