{"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)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.linear_model import ElasticNet\nfrom sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score \nfrom sklearn.model_selection import GridSearchCV\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","execution":{"iopub.status.busy":"2022-07-16T09:52:16.329848Z","iopub.execute_input":"2022-07-16T09:52:16.330345Z","iopub.status.idle":"2022-07-16T09:52:16.342077Z","shell.execute_reply.started":"2022-07-16T09:52:16.330309Z","shell.execute_reply":"2022-07-16T09:52:16.340809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/house-prices-advanced-regression-techniques/train.csv\")\ntest_df = pd.read_csv(\"/kaggle/input/house-prices-advanced-regression-techniques/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:16.343876Z","iopub.execute_input":"2022-07-16T09:52:16.344989Z","iopub.status.idle":"2022-07-16T09:52:16.399269Z","shell.execute_reply.started":"2022-07-16T09:52:16.344955Z","shell.execute_reply":"2022-07-16T09:52:16.397953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:16.400837Z","iopub.execute_input":"2022-07-16T09:52:16.401279Z","iopub.status.idle":"2022-07-16T09:52:16.432959Z","shell.execute_reply.started":"2022-07-16T09:52:16.401235Z","shell.execute_reply":"2022-07-16T09:52:16.431808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Checking for outliers","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10,5),dpi=200)\nsns.histplot(x=train_df['SalePrice'])","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:16.436127Z","iopub.execute_input":"2022-07-16T09:52:16.436994Z","iopub.status.idle":"2022-07-16T09:52:16.819238Z","shell.execute_reply.started":"2022-07-16T09:52:16.436945Z","shell.execute_reply":"2022-07-16T09:52:16.817968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxplot(data=train_df,x='SalePrice')","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:16.820803Z","iopub.execute_input":"2022-07-16T09:52:16.821778Z","iopub.status.idle":"2022-07-16T09:52:16.941638Z","shell.execute_reply.started":"2022-07-16T09:52:16.821695Z","shell.execute_reply":"2022-07-16T09:52:16.940529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['SalePrice'].describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:16.943363Z","iopub.execute_input":"2022-07-16T09:52:16.944031Z","iopub.status.idle":"2022-07-16T09:52:16.956237Z","shell.execute_reply.started":"2022-07-16T09:52:16.943987Z","shell.execute_reply":"2022-07-16T09:52:16.955066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Correlation with respect to SalePrice","metadata":{}},{"cell_type":"code","source":"train_df.corr()['SalePrice'].sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:16.958064Z","iopub.execute_input":"2022-07-16T09:52:16.958809Z","iopub.status.idle":"2022-07-16T09:52:16.983288Z","shell.execute_reply.started":"2022-07-16T09:52:16.958759Z","shell.execute_reply":"2022-07-16T09:52:16.982126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,5),dpi=200)\nsns.scatterplot(x=train_df['GrLivArea'],y=train_df['SalePrice'])","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:16.985053Z","iopub.execute_input":"2022-07-16T09:52:16.985762Z","iopub.status.idle":"2022-07-16T09:52:17.266470Z","shell.execute_reply.started":"2022-07-16T09:52:16.985698Z","shell.execute_reply":"2022-07-16T09:52:17.265317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Two points at the right bottom looks suspicious","metadata":{}},{"cell_type":"code","source":"train_df[train_df['GrLivArea']>4000]","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:17.268151Z","iopub.execute_input":"2022-07-16T09:52:17.268837Z","iopub.status.idle":"2022-07-16T09:52:17.297600Z","shell.execute_reply.started":"2022-07-16T09:52:17.268792Z","shell.execute_reply":"2022-07-16T09:52:17.296428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,5),dpi=200)\nsns.scatterplot(x=train_df['OverallQual'],y=train_df['SalePrice'])","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:17.303190Z","iopub.execute_input":"2022-07-16T09:52:17.303837Z","iopub.status.idle":"2022-07-16T09:52:17.610910Z","shell.execute_reply.started":"2022-07-16T09:52:17.303794Z","shell.execute_reply":"2022-07-16T09:52:17.609738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Overall quality is 10 and for less price there is something wrong here and this is where domain knowledge is required, anyways let's assume those two are outliers and remove them","metadata":{}},{"cell_type":"code","source":"train_df[(train_df['OverallQual']==10) & (train_df['SalePrice']<200000)]","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:17.612432Z","iopub.execute_input":"2022-07-16T09:52:17.612824Z","iopub.status.idle":"2022-07-16T09:52:17.641556Z","shell.execute_reply.started":"2022-07-16T09:52:17.612792Z","shell.execute_reply":"2022-07-16T09:52:17.640798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"drop_ind = train_df[(train_df['OverallQual']==10) & (train_df['SalePrice']<200000)].index","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:17.643093Z","iopub.execute_input":"2022-07-16T09:52:17.643409Z","iopub.status.idle":"2022-07-16T09:52:17.655312Z","shell.execute_reply.started":"2022-07-16T09:52:17.643380Z","shell.execute_reply":"2022-07-16T09:52:17.654480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = train_df.drop(drop_ind,axis=0)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:17.656809Z","iopub.execute_input":"2022-07-16T09:52:17.657142Z","iopub.status.idle":"2022-07-16T09:52:17.675420Z","shell.execute_reply.started":"2022-07-16T09:52:17.657111Z","shell.execute_reply":"2022-07-16T09:52:17.674478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,5),dpi=200)\nsns.scatterplot(x=train_df['GrLivArea'],y=train_df['SalePrice'])","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:17.676916Z","iopub.execute_input":"2022-07-16T09:52:17.677264Z","iopub.status.idle":"2022-07-16T09:52:18.012785Z","shell.execute_reply.started":"2022-07-16T09:52:17.677231Z","shell.execute_reply":"2022-07-16T09:52:18.011525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,10),dpi=200)\nsns.heatmap(train_df.corr(),annot=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:18.014403Z","iopub.execute_input":"2022-07-16T09:52:18.014833Z","iopub.status.idle":"2022-07-16T09:52:19.419214Z","shell.execute_reply.started":"2022-07-16T09:52:18.014792Z","shell.execute_reply":"2022-07-16T09:52:19.417701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dealing with missing values\n# There might be missing values in test data as well","metadata":{}},{"cell_type":"code","source":"test_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:19.420923Z","iopub.execute_input":"2022-07-16T09:52:19.421273Z","iopub.status.idle":"2022-07-16T09:52:19.446178Z","shell.execute_reply.started":"2022-07-16T09:52:19.421238Z","shell.execute_reply":"2022-07-16T09:52:19.445152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Combining both datasets","metadata":{}},{"cell_type":"code","source":"main_df = pd.concat([train_df,test_df],ignore_index=True)\nmain_df","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:19.447981Z","iopub.execute_input":"2022-07-16T09:52:19.448297Z","iopub.status.idle":"2022-07-16T09:52:19.496909Z","shell.execute_reply.started":"2022-07-16T09:52:19.448266Z","shell.execute_reply":"2022-07-16T09:52:19.495779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:19.498323Z","iopub.execute_input":"2022-07-16T09:52:19.498617Z","iopub.status.idle":"2022-07-16T09:52:19.536251Z","shell.execute_reply.started":"2022-07-16T09:52:19.498591Z","shell.execute_reply":"2022-07-16T09:52:19.535062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# finding Relative frequency of missing data","metadata":{}},{"cell_type":"code","source":"100 * main_df.isnull().sum() / len(main_df)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:19.537509Z","iopub.execute_input":"2022-07-16T09:52:19.537944Z","iopub.status.idle":"2022-07-16T09:52:19.569838Z","shell.execute_reply.started":"2022-07-16T09:52:19.537897Z","shell.execute_reply":"2022-07-16T09:52:19.568704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This function will help to find relative frequency of missing data\ndef percent_missing(df):\n    percentage = 100 * main_df.isnull().sum() / len(main_df)\n    percentage = percentage[percentage > 0]\n    percentage = percentage.sort_values(ascending=False)\n    return percentage","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:19.571134Z","iopub.execute_input":"2022-07-16T09:52:19.571590Z","iopub.status.idle":"2022-07-16T09:52:19.577446Z","shell.execute_reply.started":"2022-07-16T09:52:19.571551Z","shell.execute_reply":"2022-07-16T09:52:19.576680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"miss_data_rel_freq = percent_missing(main_df)\nmiss_data_rel_freq","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:19.578807Z","iopub.execute_input":"2022-07-16T09:52:19.579212Z","iopub.status.idle":"2022-07-16T09:52:19.613565Z","shell.execute_reply.started":"2022-07-16T09:52:19.579171Z","shell.execute_reply":"2022-07-16T09:52:19.612421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,5),dpi=200)\nsns.barplot(x=miss_data_rel_freq.index,y=miss_data_rel_freq.values)\nplt.xticks(rotation=90)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:19.615243Z","iopub.execute_input":"2022-07-16T09:52:19.616116Z","iopub.status.idle":"2022-07-16T09:52:20.137616Z","shell.execute_reply.started":"2022-07-16T09:52:19.616069Z","shell.execute_reply":"2022-07-16T09:52:20.136412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# If data is missing less than 1 percent we might consider dropping them but not if it belongs to test dataset\n# You need to read data description before filling or dropping the data","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10,5),dpi=200)\nsns.barplot(x=miss_data_rel_freq.index,y=miss_data_rel_freq.values)\nplt.xticks(rotation=90)\nplt.ylim(0,1)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:20.139228Z","iopub.execute_input":"2022-07-16T09:52:20.139621Z","iopub.status.idle":"2022-07-16T09:52:20.660196Z","shell.execute_reply.started":"2022-07-16T09:52:20.139584Z","shell.execute_reply":"2022-07-16T09:52:20.659283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"miss_data_rel_freq[miss_data_rel_freq < 1]","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:20.661702Z","iopub.execute_input":"2022-07-16T09:52:20.662308Z","iopub.status.idle":"2022-07-16T09:52:20.670269Z","shell.execute_reply.started":"2022-07-16T09:52:20.662272Z","shell.execute_reply":"2022-07-16T09:52:20.669497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_df[main_df['Electrical'].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:20.671536Z","iopub.execute_input":"2022-07-16T09:52:20.672182Z","iopub.status.idle":"2022-07-16T09:52:20.702676Z","shell.execute_reply.started":"2022-07-16T09:52:20.672149Z","shell.execute_reply":"2022-07-16T09:52:20.701947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_df = main_df.dropna(axis=0,subset=['Electrical'])","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:20.703990Z","iopub.execute_input":"2022-07-16T09:52:20.704865Z","iopub.status.idle":"2022-07-16T09:52:20.712671Z","shell.execute_reply.started":"2022-07-16T09:52:20.704832Z","shell.execute_reply":"2022-07-16T09:52:20.711906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_df[main_df['GarageArea'].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:20.714208Z","iopub.execute_input":"2022-07-16T09:52:20.714788Z","iopub.status.idle":"2022-07-16T09:52:20.741066Z","shell.execute_reply.started":"2022-07-16T09:52:20.714724Z","shell.execute_reply":"2022-07-16T09:52:20.740152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_df['GarageArea'].describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:20.749990Z","iopub.execute_input":"2022-07-16T09:52:20.750644Z","iopub.status.idle":"2022-07-16T09:52:20.761275Z","shell.execute_reply.started":"2022-07-16T09:52:20.750606Z","shell.execute_reply":"2022-07-16T09:52:20.760188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_df['GarageArea'] = main_df['GarageArea'].fillna(472)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:20.762615Z","iopub.execute_input":"2022-07-16T09:52:20.763422Z","iopub.status.idle":"2022-07-16T09:52:20.768819Z","shell.execute_reply.started":"2022-07-16T09:52:20.763391Z","shell.execute_reply":"2022-07-16T09:52:20.768005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_df[main_df['GarageCars'].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:20.770105Z","iopub.execute_input":"2022-07-16T09:52:20.771064Z","iopub.status.idle":"2022-07-16T09:52:20.800634Z","shell.execute_reply.started":"2022-07-16T09:52:20.771026Z","shell.execute_reply":"2022-07-16T09:52:20.799786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_df['GarageCars'].describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:20.802034Z","iopub.execute_input":"2022-07-16T09:52:20.802346Z","iopub.status.idle":"2022-07-16T09:52:20.813876Z","shell.execute_reply.started":"2022-07-16T09:52:20.802316Z","shell.execute_reply":"2022-07-16T09:52:20.812746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_df['GarageCars'] = main_df['GarageCars'].fillna(2)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:20.815149Z","iopub.execute_input":"2022-07-16T09:52:20.816027Z","iopub.status.idle":"2022-07-16T09:52:20.822061Z","shell.execute_reply.started":"2022-07-16T09:52:20.815990Z","shell.execute_reply":"2022-07-16T09:52:20.821262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"miss_data_rel_freq = percent_missing(main_df)\nmiss_data_rel_freq[miss_data_rel_freq < 1]","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:20.823578Z","iopub.execute_input":"2022-07-16T09:52:20.824062Z","iopub.status.idle":"2022-07-16T09:52:20.852481Z","shell.execute_reply.started":"2022-07-16T09:52:20.824028Z","shell.execute_reply":"2022-07-16T09:52:20.851023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_df[main_df['BsmtFullBath'].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:20.854132Z","iopub.execute_input":"2022-07-16T09:52:20.854716Z","iopub.status.idle":"2022-07-16T09:52:20.879839Z","shell.execute_reply.started":"2022-07-16T09:52:20.854680Z","shell.execute_reply":"2022-07-16T09:52:20.878648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_df[main_df['BsmtHalfBath'].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:20.881544Z","iopub.execute_input":"2022-07-16T09:52:20.882253Z","iopub.status.idle":"2022-07-16T09:52:20.909065Z","shell.execute_reply.started":"2022-07-16T09:52:20.882206Z","shell.execute_reply":"2022-07-16T09:52:20.907801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_df[main_df['BsmtUnfSF'].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:20.911258Z","iopub.execute_input":"2022-07-16T09:52:20.911698Z","iopub.status.idle":"2022-07-16T09:52:20.935828Z","shell.execute_reply.started":"2022-07-16T09:52:20.911654Z","shell.execute_reply":"2022-07-16T09:52:20.934664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_df[main_df['TotalBsmtSF'].isnull()]['BsmtExposure']","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:20.937561Z","iopub.execute_input":"2022-07-16T09:52:20.938247Z","iopub.status.idle":"2022-07-16T09:52:20.949163Z","shell.execute_reply.started":"2022-07-16T09:52:20.938205Z","shell.execute_reply":"2022-07-16T09:52:20.947583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# filling missing basement numeric columns to 0\nbase_num_cols = ['BsmtFinSF1','BsmtFinSF2','BsmtUnfSF','TotalBsmtSF','BsmtFullBath','BsmtHalfBath']\nmain_df[base_num_cols] = main_df[base_num_cols].fillna(0) \n\n# filling missing basement string columns to NA\nbase_str_cols = ['BsmtQual','BsmtCond','BsmtExposure','BsmtFinType1','BsmtFinType2']\nmain_df[base_str_cols] = main_df[base_str_cols].fillna('None')","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:20.951175Z","iopub.execute_input":"2022-07-16T09:52:20.951599Z","iopub.status.idle":"2022-07-16T09:52:20.966881Z","shell.execute_reply.started":"2022-07-16T09:52:20.951567Z","shell.execute_reply":"2022-07-16T09:52:20.965533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"miss_data_rel_freq = percent_missing(main_df)\nmiss_data_rel_freq[miss_data_rel_freq < 1]","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:20.968170Z","iopub.execute_input":"2022-07-16T09:52:20.969091Z","iopub.status.idle":"2022-07-16T09:52:20.994268Z","shell.execute_reply.started":"2022-07-16T09:52:20.969053Z","shell.execute_reply":"2022-07-16T09:52:20.993071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,5),dpi=200)\nsns.barplot(x=miss_data_rel_freq.index,y=miss_data_rel_freq.values)\nplt.xticks(rotation=90)\nplt.ylim(0,1)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:20.995823Z","iopub.execute_input":"2022-07-16T09:52:20.996256Z","iopub.status.idle":"2022-07-16T09:52:21.401827Z","shell.execute_reply.started":"2022-07-16T09:52:20.996214Z","shell.execute_reply":"2022-07-16T09:52:21.400973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_df[main_df['SaleType'].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:21.402911Z","iopub.execute_input":"2022-07-16T09:52:21.403437Z","iopub.status.idle":"2022-07-16T09:52:21.432328Z","shell.execute_reply.started":"2022-07-16T09:52:21.403404Z","shell.execute_reply":"2022-07-16T09:52:21.431047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_df['SaleType'] = main_df['SaleType'].fillna(\"Oth\")","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:21.433942Z","iopub.execute_input":"2022-07-16T09:52:21.434353Z","iopub.status.idle":"2022-07-16T09:52:21.441647Z","shell.execute_reply.started":"2022-07-16T09:52:21.434312Z","shell.execute_reply":"2022-07-16T09:52:21.440564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_df['Exterior1st'] = main_df['Exterior1st'].fillna('Other')\n\nmain_df['Exterior2nd'] = main_df['Exterior2nd'].fillna('Other')","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:21.443698Z","iopub.execute_input":"2022-07-16T09:52:21.445112Z","iopub.status.idle":"2022-07-16T09:52:21.455494Z","shell.execute_reply.started":"2022-07-16T09:52:21.445064Z","shell.execute_reply":"2022-07-16T09:52:21.454163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_df[main_df['MSZoning'].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:21.457529Z","iopub.execute_input":"2022-07-16T09:52:21.458882Z","iopub.status.idle":"2022-07-16T09:52:21.484974Z","shell.execute_reply.started":"2022-07-16T09:52:21.458837Z","shell.execute_reply":"2022-07-16T09:52:21.484145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Replacing data with maximum values","metadata":{}},{"cell_type":"code","source":"sns.countplot(x = main_df['MSZoning'])","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:21.486133Z","iopub.execute_input":"2022-07-16T09:52:21.486592Z","iopub.status.idle":"2022-07-16T09:52:21.653872Z","shell.execute_reply.started":"2022-07-16T09:52:21.486562Z","shell.execute_reply":"2022-07-16T09:52:21.653054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_df['MSZoning'] = main_df['MSZoning'].fillna(\"RL\")","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:21.655033Z","iopub.execute_input":"2022-07-16T09:52:21.655491Z","iopub.status.idle":"2022-07-16T09:52:21.661433Z","shell.execute_reply.started":"2022-07-16T09:52:21.655461Z","shell.execute_reply":"2022-07-16T09:52:21.660685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_df[main_df['Functional'].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:21.662515Z","iopub.execute_input":"2022-07-16T09:52:21.663092Z","iopub.status.idle":"2022-07-16T09:52:21.694324Z","shell.execute_reply.started":"2022-07-16T09:52:21.663060Z","shell.execute_reply":"2022-07-16T09:52:21.693483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x = main_df['Functional'])","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:21.695615Z","iopub.execute_input":"2022-07-16T09:52:21.696231Z","iopub.status.idle":"2022-07-16T09:52:21.889117Z","shell.execute_reply.started":"2022-07-16T09:52:21.696196Z","shell.execute_reply":"2022-07-16T09:52:21.888073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_df['Functional'] = main_df['Functional'].fillna(\"Typ\")","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:21.890466Z","iopub.execute_input":"2022-07-16T09:52:21.890786Z","iopub.status.idle":"2022-07-16T09:52:21.897042Z","shell.execute_reply.started":"2022-07-16T09:52:21.890750Z","shell.execute_reply":"2022-07-16T09:52:21.896026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_df[main_df['Utilities'].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:21.898253Z","iopub.execute_input":"2022-07-16T09:52:21.899166Z","iopub.status.idle":"2022-07-16T09:52:21.929403Z","shell.execute_reply.started":"2022-07-16T09:52:21.899125Z","shell.execute_reply":"2022-07-16T09:52:21.928362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x = main_df['Utilities'])","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:21.930968Z","iopub.execute_input":"2022-07-16T09:52:21.931487Z","iopub.status.idle":"2022-07-16T09:52:22.095235Z","shell.execute_reply.started":"2022-07-16T09:52:21.931457Z","shell.execute_reply":"2022-07-16T09:52:22.094085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_df['Utilities'] = main_df['Utilities'].fillna(\"AllPub\")","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:22.096716Z","iopub.execute_input":"2022-07-16T09:52:22.097165Z","iopub.status.idle":"2022-07-16T09:52:22.103915Z","shell.execute_reply.started":"2022-07-16T09:52:22.097122Z","shell.execute_reply":"2022-07-16T09:52:22.102798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_df[main_df['KitchenQual'].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:22.105603Z","iopub.execute_input":"2022-07-16T09:52:22.106899Z","iopub.status.idle":"2022-07-16T09:52:22.133640Z","shell.execute_reply.started":"2022-07-16T09:52:22.106850Z","shell.execute_reply":"2022-07-16T09:52:22.132641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x = main_df['KitchenQual'])","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:22.135206Z","iopub.execute_input":"2022-07-16T09:52:22.135893Z","iopub.status.idle":"2022-07-16T09:52:22.314572Z","shell.execute_reply.started":"2022-07-16T09:52:22.135847Z","shell.execute_reply":"2022-07-16T09:52:22.313451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_df['KitchenQual'] = main_df['KitchenQual'].fillna(\"TA\")","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:22.316060Z","iopub.execute_input":"2022-07-16T09:52:22.317382Z","iopub.status.idle":"2022-07-16T09:52:22.325000Z","shell.execute_reply.started":"2022-07-16T09:52:22.317334Z","shell.execute_reply":"2022-07-16T09:52:22.323795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_df[main_df['MasVnrType'].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:22.326476Z","iopub.execute_input":"2022-07-16T09:52:22.326887Z","iopub.status.idle":"2022-07-16T09:52:22.369786Z","shell.execute_reply.started":"2022-07-16T09:52:22.326857Z","shell.execute_reply":"2022-07-16T09:52:22.369016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_df[main_df['MasVnrArea'].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:22.371048Z","iopub.execute_input":"2022-07-16T09:52:22.371552Z","iopub.status.idle":"2022-07-16T09:52:22.409907Z","shell.execute_reply.started":"2022-07-16T09:52:22.371521Z","shell.execute_reply":"2022-07-16T09:52:22.408780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Filling nan MasVnrType with None\nmain_df['MasVnrType'] = main_df['MasVnrType'].fillna(\"None\")\n\n# Filling nan MasVnrArea with 0\nmain_df['MasVnrArea'] = main_df['MasVnrArea'].fillna(0)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:22.411196Z","iopub.execute_input":"2022-07-16T09:52:22.411519Z","iopub.status.idle":"2022-07-16T09:52:22.420484Z","shell.execute_reply.started":"2022-07-16T09:52:22.411490Z","shell.execute_reply":"2022-07-16T09:52:22.419588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"miss_data_rel_freq = percent_missing(main_df)\nmiss_data_rel_freq","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:22.421561Z","iopub.execute_input":"2022-07-16T09:52:22.422571Z","iopub.status.idle":"2022-07-16T09:52:22.448089Z","shell.execute_reply.started":"2022-07-16T09:52:22.422541Z","shell.execute_reply":"2022-07-16T09:52:22.447351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,5),dpi=200)\nsns.barplot(x=miss_data_rel_freq.index,y=miss_data_rel_freq.values)\nplt.xticks(rotation=90)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:22.449027Z","iopub.execute_input":"2022-07-16T09:52:22.449649Z","iopub.status.idle":"2022-07-16T09:52:22.761887Z","shell.execute_reply.started":"2022-07-16T09:52:22.449621Z","shell.execute_reply":"2022-07-16T09:52:22.760787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gar_str_cols = ['GarageFinish','GarageQual','GarageCond','GarageType']\nmain_df[gar_str_cols] = main_df[gar_str_cols].fillna(\"NA\")\n\nmain_df['GarageYrBlt'] = main_df['GarageYrBlt'].fillna(0)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:22.763441Z","iopub.execute_input":"2022-07-16T09:52:22.763999Z","iopub.status.idle":"2022-07-16T09:52:22.775693Z","shell.execute_reply.started":"2022-07-16T09:52:22.763944Z","shell.execute_reply":"2022-07-16T09:52:22.774923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"miss_data_rel_freq = percent_missing(main_df)\nmiss_data_rel_freq","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:22.776822Z","iopub.execute_input":"2022-07-16T09:52:22.777285Z","iopub.status.idle":"2022-07-16T09:52:22.807129Z","shell.execute_reply.started":"2022-07-16T09:52:22.777255Z","shell.execute_reply":"2022-07-16T09:52:22.806060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,5),dpi=200)\nsns.barplot(x=miss_data_rel_freq.index,y=miss_data_rel_freq.values)\nplt.xticks(rotation=90)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:22.808188Z","iopub.execute_input":"2022-07-16T09:52:22.809044Z","iopub.status.idle":"2022-07-16T09:52:23.071603Z","shell.execute_reply.started":"2022-07-16T09:52:22.809013Z","shell.execute_reply":"2022-07-16T09:52:23.070812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"not_available = ['PoolQC','MiscFeature','Alley','Fence','FireplaceQu']\nmain_df[not_available] = main_df[not_available].fillna(\"NA\")","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:23.072591Z","iopub.execute_input":"2022-07-16T09:52:23.073541Z","iopub.status.idle":"2022-07-16T09:52:23.082810Z","shell.execute_reply.started":"2022-07-16T09:52:23.073508Z","shell.execute_reply":"2022-07-16T09:52:23.081502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# LotFrontage might be connected to Neighborhood feature (Requires Domain Knowledge but let's assume)","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10,10),dpi=200)\nsns.boxplot(x=main_df['LotFrontage'],y=main_df['Neighborhood'],orient='h')","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:23.084107Z","iopub.execute_input":"2022-07-16T09:52:23.084421Z","iopub.status.idle":"2022-07-16T09:52:23.806047Z","shell.execute_reply.started":"2022-07-16T09:52:23.084381Z","shell.execute_reply":"2022-07-16T09:52:23.804931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Filling mean values of this neighborhood in missing lot frontage feature","metadata":{}},{"cell_type":"code","source":"main_df['LotFrontage'] = main_df.groupby('Neighborhood')['LotFrontage'].transform(lambda value: value.fillna(value.mean()))","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:23.807665Z","iopub.execute_input":"2022-07-16T09:52:23.808477Z","iopub.status.idle":"2022-07-16T09:52:23.824183Z","shell.execute_reply.started":"2022-07-16T09:52:23.808437Z","shell.execute_reply":"2022-07-16T09:52:23.823117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"miss_data_rel_freq = percent_missing(main_df)\nmiss_data_rel_freq","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:23.825914Z","iopub.execute_input":"2022-07-16T09:52:23.826223Z","iopub.status.idle":"2022-07-16T09:52:23.849519Z","shell.execute_reply.started":"2022-07-16T09:52:23.826193Z","shell.execute_reply":"2022-07-16T09:52:23.848385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dealing with categorical data","metadata":{}},{"cell_type":"code","source":"# This is a categorical column (read data description)\nmain_df['MSSubClass'] = main_df['MSSubClass'].apply(str) ","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:23.851656Z","iopub.execute_input":"2022-07-16T09:52:23.852632Z","iopub.status.idle":"2022-07-16T09:52:23.861867Z","shell.execute_reply.started":"2022-07-16T09:52:23.852563Z","shell.execute_reply":"2022-07-16T09:52:23.860510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"categorical_df = main_df.select_dtypes(include='object')\ncategorical_df","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:23.863682Z","iopub.execute_input":"2022-07-16T09:52:23.864369Z","iopub.status.idle":"2022-07-16T09:52:23.899415Z","shell.execute_reply.started":"2022-07-16T09:52:23.864326Z","shell.execute_reply":"2022-07-16T09:52:23.898624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numeric_df = main_df.select_dtypes(exclude='object')\nnumeric_df","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:23.900641Z","iopub.execute_input":"2022-07-16T09:52:23.901168Z","iopub.status.idle":"2022-07-16T09:52:23.933040Z","shell.execute_reply.started":"2022-07-16T09:52:23.901138Z","shell.execute_reply":"2022-07-16T09:52:23.932231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"categorical_dummies = pd.get_dummies(categorical_df,drop_first=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:23.934268Z","iopub.execute_input":"2022-07-16T09:52:23.934771Z","iopub.status.idle":"2022-07-16T09:52:23.988466Z","shell.execute_reply.started":"2022-07-16T09:52:23.934707Z","shell.execute_reply":"2022-07-16T09:52:23.987258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"categorical_dummies","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:23.989929Z","iopub.execute_input":"2022-07-16T09:52:23.990245Z","iopub.status.idle":"2022-07-16T09:52:24.014549Z","shell.execute_reply.started":"2022-07-16T09:52:23.990203Z","shell.execute_reply":"2022-07-16T09:52:24.013504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_df = pd.concat([numeric_df,categorical_dummies],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:24.016207Z","iopub.execute_input":"2022-07-16T09:52:24.016499Z","iopub.status.idle":"2022-07-16T09:52:24.023604Z","shell.execute_reply.started":"2022-07-16T09:52:24.016472Z","shell.execute_reply":"2022-07-16T09:52:24.022852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_df","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:24.024911Z","iopub.execute_input":"2022-07-16T09:52:24.025707Z","iopub.status.idle":"2022-07-16T09:52:24.058256Z","shell.execute_reply.started":"2022-07-16T09:52:24.025672Z","shell.execute_reply":"2022-07-16T09:52:24.057180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Now let's separate the dataframe\n# Those who have target will be training data and rest will be test data","metadata":{}},{"cell_type":"code","source":"final_train_df = final_df.dropna()\nfinal_train_df","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:24.059386Z","iopub.execute_input":"2022-07-16T09:52:24.060259Z","iopub.status.idle":"2022-07-16T09:52:24.100452Z","shell.execute_reply.started":"2022-07-16T09:52:24.060212Z","shell.execute_reply":"2022-07-16T09:52:24.099468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_test_df = final_df[final_df['SalePrice'].isnull()]\nfinal_test_df","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:24.101911Z","iopub.execute_input":"2022-07-16T09:52:24.102213Z","iopub.status.idle":"2022-07-16T09:52:24.132188Z","shell.execute_reply.started":"2022-07-16T09:52:24.102187Z","shell.execute_reply":"2022-07-16T09:52:24.131415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_train_df = final_train_df.drop('Id',axis=1)\nfinal_train_df","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:24.133144Z","iopub.execute_input":"2022-07-16T09:52:24.133912Z","iopub.status.idle":"2022-07-16T09:52:24.168719Z","shell.execute_reply.started":"2022-07-16T09:52:24.133881Z","shell.execute_reply":"2022-07-16T09:52:24.167830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_train_df.corr()['SalePrice'].sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:24.170067Z","iopub.execute_input":"2022-07-16T09:52:24.171163Z","iopub.status.idle":"2022-07-16T09:52:24.471560Z","shell.execute_reply.started":"2022-07-16T09:52:24.171117Z","shell.execute_reply":"2022-07-16T09:52:24.470499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Spliting data into Train, Test and Validation","metadata":{}},{"cell_type":"code","source":"x = final_train_df.drop(\"SalePrice\", axis=1)\ny = final_train_df['SalePrice']","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:24.472881Z","iopub.execute_input":"2022-07-16T09:52:24.473665Z","iopub.status.idle":"2022-07-16T09:52:24.480290Z","shell.execute_reply.started":"2022-07-16T09:52:24.473632Z","shell.execute_reply":"2022-07-16T09:52:24.479027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.20, random_state=45)\nx_test, x_val, y_test, y_val = train_test_split(x_test, y_test, test_size=0.50, random_state=45)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:24.481902Z","iopub.execute_input":"2022-07-16T09:52:24.482298Z","iopub.status.idle":"2022-07-16T09:52:24.496645Z","shell.execute_reply.started":"2022-07-16T09:52:24.482265Z","shell.execute_reply":"2022-07-16T09:52:24.495803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler = StandardScaler()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:24.497940Z","iopub.execute_input":"2022-07-16T09:52:24.498798Z","iopub.status.idle":"2022-07-16T09:52:24.502503Z","shell.execute_reply.started":"2022-07-16T09:52:24.498761Z","shell.execute_reply":"2022-07-16T09:52:24.501799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaled_x_train = scaler.fit_transform(x_train)\nscaled_x_test = scaler.transform(x_test)\nscaled_x_val = scaler.transform(x_val)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:24.514609Z","iopub.execute_input":"2022-07-16T09:52:24.515513Z","iopub.status.idle":"2022-07-16T09:52:24.544121Z","shell.execute_reply.started":"2022-07-16T09:52:24.515476Z","shell.execute_reply":"2022-07-16T09:52:24.543005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# I am selecting Elastic Net model for this project","metadata":{}},{"cell_type":"code","source":"base_elastic_model = ElasticNet()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:24.545329Z","iopub.execute_input":"2022-07-16T09:52:24.546247Z","iopub.status.idle":"2022-07-16T09:52:24.551265Z","shell.execute_reply.started":"2022-07-16T09:52:24.546198Z","shell.execute_reply":"2022-07-16T09:52:24.549947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"param_grid = {'alpha':[250,300,400,450,475,500,525,550],\n              'l1_ratio':[0.95,0.96,0.97,0.98,0.99, 1,1.10,1.15,1.25,1.5]}","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:24.552684Z","iopub.execute_input":"2022-07-16T09:52:24.553626Z","iopub.status.idle":"2022-07-16T09:52:24.564999Z","shell.execute_reply.started":"2022-07-16T09:52:24.553583Z","shell.execute_reply":"2022-07-16T09:52:24.564131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grid_model = GridSearchCV(estimator=base_elastic_model,\n                          param_grid=param_grid,\n                          scoring='neg_mean_squared_error',\n                          cv=5,\n                          verbose=2,\n                          )","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:24.566325Z","iopub.execute_input":"2022-07-16T09:52:24.567282Z","iopub.status.idle":"2022-07-16T09:52:24.576811Z","shell.execute_reply.started":"2022-07-16T09:52:24.567236Z","shell.execute_reply":"2022-07-16T09:52:24.575717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grid_model.fit(scaled_x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:24.578412Z","iopub.execute_input":"2022-07-16T09:52:24.578854Z","iopub.status.idle":"2022-07-16T09:52:31.846520Z","shell.execute_reply.started":"2022-07-16T09:52:24.578811Z","shell.execute_reply":"2022-07-16T09:52:31.844922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grid_model.best_params_","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:31.848295Z","iopub.execute_input":"2022-07-16T09:52:31.848786Z","iopub.status.idle":"2022-07-16T09:52:31.858494Z","shell.execute_reply.started":"2022-07-16T09:52:31.848739Z","shell.execute_reply":"2022-07-16T09:52:31.857043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = grid_model.predict(scaled_x_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:31.860265Z","iopub.execute_input":"2022-07-16T09:52:31.860749Z","iopub.status.idle":"2022-07-16T09:52:31.870423Z","shell.execute_reply.started":"2022-07-16T09:52:31.860696Z","shell.execute_reply":"2022-07-16T09:52:31.869115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mae = mean_absolute_error(y_test,y_pred)\nmse = mean_squared_error(y_test,y_pred)\nrmse = np.sqrt(mse)\n\nprint(mae)\nprint(mse)\nprint(rmse)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:31.872175Z","iopub.execute_input":"2022-07-16T09:52:31.873555Z","iopub.status.idle":"2022-07-16T09:52:31.894827Z","shell.execute_reply.started":"2022-07-16T09:52:31.873504Z","shell.execute_reply":"2022-07-16T09:52:31.892976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_pred = grid_model.predict(scaled_x_val)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:31.896802Z","iopub.execute_input":"2022-07-16T09:52:31.900045Z","iopub.status.idle":"2022-07-16T09:52:31.913516Z","shell.execute_reply.started":"2022-07-16T09:52:31.899999Z","shell.execute_reply":"2022-07-16T09:52:31.911586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mae = mean_absolute_error(y_val,val_pred)\nmse = mean_squared_error(y_val,val_pred)\nrmse = np.sqrt(mse)\n\nprint(mae)\nprint(mse)\nprint(rmse)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:31.915651Z","iopub.execute_input":"2022-07-16T09:52:31.919044Z","iopub.status.idle":"2022-07-16T09:52:31.929595Z","shell.execute_reply.started":"2022-07-16T09:52:31.918991Z","shell.execute_reply":"2022-07-16T09:52:31.928118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r2_score(y_test,y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:31.932200Z","iopub.execute_input":"2022-07-16T09:52:31.933277Z","iopub.status.idle":"2022-07-16T09:52:31.943032Z","shell.execute_reply.started":"2022-07-16T09:52:31.933222Z","shell.execute_reply":"2022-07-16T09:52:31.941716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r2_score(y_val,val_pred)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:31.945310Z","iopub.execute_input":"2022-07-16T09:52:31.946506Z","iopub.status.idle":"2022-07-16T09:52:31.954592Z","shell.execute_reply.started":"2022-07-16T09:52:31.946454Z","shell.execute_reply":"2022-07-16T09:52:31.953656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Final Model","metadata":{}},{"cell_type":"code","source":"final_model = ElasticNet(alpha=500,l1_ratio=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:31.956235Z","iopub.execute_input":"2022-07-16T09:52:31.956918Z","iopub.status.idle":"2022-07-16T09:52:31.961334Z","shell.execute_reply.started":"2022-07-16T09:52:31.956884Z","shell.execute_reply":"2022-07-16T09:52:31.960580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = final_test_df['Id']\nfinal_test_df = final_test_df.drop([\"Id\",\"SalePrice\"],axis=1)\nscaled_final_test_df = scaler.transform(final_test_df)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:31.962597Z","iopub.execute_input":"2022-07-16T09:52:31.963434Z","iopub.status.idle":"2022-07-16T09:52:31.986190Z","shell.execute_reply.started":"2022-07-16T09:52:31.963404Z","shell.execute_reply":"2022-07-16T09:52:31.984882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_model.fit(scaled_x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:31.988511Z","iopub.execute_input":"2022-07-16T09:52:31.989483Z","iopub.status.idle":"2022-07-16T09:52:32.065250Z","shell.execute_reply.started":"2022-07-16T09:52:31.989428Z","shell.execute_reply":"2022-07-16T09:52:32.064018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_prediction = final_model.predict(scaled_final_test_df)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:32.067420Z","iopub.execute_input":"2022-07-16T09:52:32.068322Z","iopub.status.idle":"2022-07-16T09:52:32.098836Z","shell.execute_reply.started":"2022-07-16T09:52:32.068272Z","shell.execute_reply":"2022-07-16T09:52:32.090871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_submission = pd.DataFrame(columns=['Id','SalePrice'])\nmy_submission['Id'] = submission\nmy_submission['SalePrice'] = final_prediction\nmy_submission = my_submission.reset_index(drop=True)\nmy_submission.to_csv(\"submission.csv\", index = False)\nmy_submission","metadata":{"execution":{"iopub.status.busy":"2022-07-16T09:52:32.104032Z","iopub.execute_input":"2022-07-16T09:52:32.108635Z","iopub.status.idle":"2022-07-16T09:52:32.194904Z","shell.execute_reply.started":"2022-07-16T09:52:32.108565Z","shell.execute_reply":"2022-07-16T09:52:32.193661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# THE END\n# THANK YOU","metadata":{}}]}