{"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","execution":{"iopub.status.busy":"2022-07-21T13:41:09.914876Z","iopub.execute_input":"2022-07-21T13:41:09.915371Z","iopub.status.idle":"2022-07-21T13:41:09.950559Z","shell.execute_reply.started":"2022-07-21T13:41:09.915274Z","shell.execute_reply":"2022-07-21T13:41:09.949514Z"},"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.feature_selection import VarianceThreshold\nimport statsmodels.api as sm\nimport warnings\nwarnings.filterwarnings(\"ignore\")\nfrom scipy.stats import chi2_contingency\nfrom sklearn.metrics import confusion_matrix\nfrom sklearn import preprocessing\n","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:09.952580Z","iopub.execute_input":"2022-07-21T13:41:09.952901Z","iopub.status.idle":"2022-07-21T13:41:12.173455Z","shell.execute_reply.started":"2022-07-21T13:41:09.952869Z","shell.execute_reply":"2022-07-21T13:41:12.172556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv('../input/house-prices-advanced-regression-techniques/train.csv')\n","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:12.175011Z","iopub.execute_input":"2022-07-21T13:41:12.175530Z","iopub.status.idle":"2022-07-21T13:41:12.221860Z","shell.execute_reply.started":"2022-07-21T13:41:12.175463Z","shell.execute_reply":"2022-07-21T13:41:12.220124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:12.224762Z","iopub.execute_input":"2022-07-21T13:41:12.226912Z","iopub.status.idle":"2022-07-21T13:41:12.261754Z","shell.execute_reply.started":"2022-07-21T13:41:12.226873Z","shell.execute_reply":"2022-07-21T13:41:12.260960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:12.262973Z","iopub.execute_input":"2022-07-21T13:41:12.263315Z","iopub.status.idle":"2022-07-21T13:41:12.302148Z","shell.execute_reply.started":"2022-07-21T13:41:12.263284Z","shell.execute_reply":"2022-07-21T13:41:12.301001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.shape)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:12.303606Z","iopub.execute_input":"2022-07-21T13:41:12.304457Z","iopub.status.idle":"2022-07-21T13:41:12.310605Z","shell.execute_reply.started":"2022-07-21T13:41:12.304409Z","shell.execute_reply":"2022-07-21T13:41:12.309543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.drop('Id',inplace=True,axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:12.312291Z","iopub.execute_input":"2022-07-21T13:41:12.312994Z","iopub.status.idle":"2022-07-21T13:41:12.324611Z","shell.execute_reply.started":"2022-07-21T13:41:12.312947Z","shell.execute_reply":"2022-07-21T13:41:12.323350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_num = train.select_dtypes(include=[\"number\"])\ndf_train_num.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:12.326342Z","iopub.execute_input":"2022-07-21T13:41:12.327197Z","iopub.status.idle":"2022-07-21T13:41:12.339910Z","shell.execute_reply.started":"2022-07-21T13:41:12.327132Z","shell.execute_reply":"2022-07-21T13:41:12.338583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sel = VarianceThreshold(threshold=0.1)\nsel.fit(df_train_num.iloc[:, :-1])\nprint(\"Number of features removed:\",df_train_num.shape[1]-sum(sel.get_support())-1)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:12.341766Z","iopub.execute_input":"2022-07-21T13:41:12.342570Z","iopub.status.idle":"2022-07-21T13:41:12.356792Z","shell.execute_reply.started":"2022-07-21T13:41:12.342527Z","shell.execute_reply":"2022-07-21T13:41:12.355386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dropped=[x for x in df_train_num.iloc[:,:-1].columns if x not in df_train_num.iloc[:, :-1].columns[sel.get_support()]]\nprint(dropped)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:12.363053Z","iopub.execute_input":"2022-07-21T13:41:12.363780Z","iopub.status.idle":"2022-07-21T13:41:12.380547Z","shell.execute_reply.started":"2022-07-21T13:41:12.363741Z","shell.execute_reply":"2022-07-21T13:41:12.379520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig_ = df_train_num.hist(figsize=(25, 24), bins=60,xlabelsize=10, ylabelsize=10)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:12.381965Z","iopub.execute_input":"2022-07-21T13:41:12.382783Z","iopub.status.idle":"2022-07-21T13:41:21.166809Z","shell.execute_reply.started":"2022-07-21T13:41:12.382745Z","shell.execute_reply":"2022-07-21T13:41:21.165596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_num_corr = df_train_num.corr()[\"SalePrice\"][:-1]","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.168576Z","iopub.execute_input":"2022-07-21T13:41:21.169344Z","iopub.status.idle":"2022-07-21T13:41:21.185975Z","shell.execute_reply.started":"2022-07-21T13:41:21.169296Z","shell.execute_reply":"2022-07-21T13:41:21.184896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_num_corr","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.187237Z","iopub.execute_input":"2022-07-21T13:41:21.188245Z","iopub.status.idle":"2022-07-21T13:41:21.196212Z","shell.execute_reply.started":"2022-07-21T13:41:21.188208Z","shell.execute_reply":"2022-07-21T13:41:21.195122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"high_cor = df_num_corr[abs(df_num_corr) >= 0.5].sort_values(ascending=False)\nprint(f\"{len(high_cor)} strongly correlated variables with SalePrice:\\n{high_cor}\")","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.197415Z","iopub.execute_input":"2022-07-21T13:41:21.197878Z","iopub.status.idle":"2022-07-21T13:41:21.208895Z","shell.execute_reply.started":"2022-07-21T13:41:21.197846Z","shell.execute_reply":"2022-07-21T13:41:21.208140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"low_cor = df_num_corr[abs(df_num_corr) <=0.5].sort_values(ascending=True)\nprint(f\"{len(low_cor)} lightly correlated variables with SalePrice:\\n{low_cor}\")","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.210112Z","iopub.execute_input":"2022-07-21T13:41:21.210606Z","iopub.status.idle":"2022-07-21T13:41:21.225761Z","shell.execute_reply.started":"2022-07-21T13:41:21.210576Z","shell.execute_reply":"2022-07-21T13:41:21.224703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_num.isna().sum(axis=0)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.227081Z","iopub.execute_input":"2022-07-21T13:41:21.227846Z","iopub.status.idle":"2022-07-21T13:41:21.241115Z","shell.execute_reply.started":"2022-07-21T13:41:21.227813Z","shell.execute_reply":"2022-07-21T13:41:21.240032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df_train_num.LotFrontage.nunique())\nprint(df_train_num.MasVnrArea.nunique())\nprint(df_train_num.GarageYrBlt.nunique())","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.242436Z","iopub.execute_input":"2022-07-21T13:41:21.242949Z","iopub.status.idle":"2022-07-21T13:41:21.254585Z","shell.execute_reply.started":"2022-07-21T13:41:21.242918Z","shell.execute_reply":"2022-07-21T13:41:21.253237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_num.isna().sum(axis=0)/len(df_train_num)*100","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.257714Z","iopub.execute_input":"2022-07-21T13:41:21.258646Z","iopub.status.idle":"2022-07-21T13:41:21.268562Z","shell.execute_reply.started":"2022-07-21T13:41:21.258612Z","shell.execute_reply":"2022-07-21T13:41:21.267735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Percentage of missing values","metadata":{}},{"cell_type":"code","source":"from sklearn.impute import KNNImputer\nknn=KNNImputer(n_neighbors=5)\ntrain_imputed = pd.DataFrame(knn.fit_transform(df_train_num))\ntrain_imputed.columns = df_train_num.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.270045Z","iopub.execute_input":"2022-07-21T13:41:21.270393Z","iopub.status.idle":"2022-07-21T13:41:21.381600Z","shell.execute_reply.started":"2022-07-21T13:41:21.270363Z","shell.execute_reply":"2022-07-21T13:41:21.380228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imputed.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.383577Z","iopub.execute_input":"2022-07-21T13:41:21.384389Z","iopub.status.idle":"2022-07-21T13:41:21.426498Z","shell.execute_reply.started":"2022-07-21T13:41:21.384337Z","shell.execute_reply":"2022-07-21T13:41:21.425290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imputed.isna().sum(axis=0)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.427748Z","iopub.execute_input":"2022-07-21T13:41:21.428070Z","iopub.status.idle":"2022-07-21T13:41:21.437442Z","shell.execute_reply.started":"2022-07-21T13:41:21.428040Z","shell.execute_reply":"2022-07-21T13:41:21.436533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_cat = train.select_dtypes(include=[\"object\"])\ndf_train_cat.head(1)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.438771Z","iopub.execute_input":"2022-07-21T13:41:21.439642Z","iopub.status.idle":"2022-07-21T13:41:21.465043Z","shell.execute_reply.started":"2022-07-21T13:41:21.439557Z","shell.execute_reply":"2022-07-21T13:41:21.464240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat=df_train_cat","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.466270Z","iopub.execute_input":"2022-07-21T13:41:21.466744Z","iopub.status.idle":"2022-07-21T13:41:21.470515Z","shell.execute_reply.started":"2022-07-21T13:41:21.466714Z","shell.execute_reply":"2022-07-21T13:41:21.469643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat[\"target\"]=train_imputed[\"SalePrice\"]\ndf_cat.head(0)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.471638Z","iopub.execute_input":"2022-07-21T13:41:21.472349Z","iopub.status.idle":"2022-07-21T13:41:21.495491Z","shell.execute_reply.started":"2022-07-21T13:41:21.472315Z","shell.execute_reply":"2022-07-21T13:41:21.494591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.496766Z","iopub.execute_input":"2022-07-21T13:41:21.497044Z","iopub.status.idle":"2022-07-21T13:41:21.503624Z","shell.execute_reply.started":"2022-07-21T13:41:21.497017Z","shell.execute_reply":"2022-07-21T13:41:21.502641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat.Street.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.504828Z","iopub.execute_input":"2022-07-21T13:41:21.505688Z","iopub.status.idle":"2022-07-21T13:41:21.519465Z","shell.execute_reply.started":"2022-07-21T13:41:21.505653Z","shell.execute_reply":"2022-07-21T13:41:21.518234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat.Utilities.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.520987Z","iopub.execute_input":"2022-07-21T13:41:21.521485Z","iopub.status.idle":"2022-07-21T13:41:21.531775Z","shell.execute_reply.started":"2022-07-21T13:41:21.521439Z","shell.execute_reply":"2022-07-21T13:41:21.531026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat['LandContour'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.543262Z","iopub.execute_input":"2022-07-21T13:41:21.543821Z","iopub.status.idle":"2022-07-21T13:41:21.552880Z","shell.execute_reply.started":"2022-07-21T13:41:21.543784Z","shell.execute_reply":"2022-07-21T13:41:21.551792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat[\"LandSlope\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.554074Z","iopub.execute_input":"2022-07-21T13:41:21.554609Z","iopub.status.idle":"2022-07-21T13:41:21.565278Z","shell.execute_reply.started":"2022-07-21T13:41:21.554575Z","shell.execute_reply":"2022-07-21T13:41:21.564409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat[\"Condition1\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.566302Z","iopub.execute_input":"2022-07-21T13:41:21.566884Z","iopub.status.idle":"2022-07-21T13:41:21.577605Z","shell.execute_reply.started":"2022-07-21T13:41:21.566841Z","shell.execute_reply":"2022-07-21T13:41:21.576454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat[\"Condition2\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.578748Z","iopub.execute_input":"2022-07-21T13:41:21.579321Z","iopub.status.idle":"2022-07-21T13:41:21.588750Z","shell.execute_reply.started":"2022-07-21T13:41:21.579289Z","shell.execute_reply":"2022-07-21T13:41:21.588054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat['BldgType'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.589756Z","iopub.execute_input":"2022-07-21T13:41:21.590303Z","iopub.status.idle":"2022-07-21T13:41:21.604269Z","shell.execute_reply.started":"2022-07-21T13:41:21.590272Z","shell.execute_reply":"2022-07-21T13:41:21.603200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat[\"RoofMatl\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.605310Z","iopub.execute_input":"2022-07-21T13:41:21.606207Z","iopub.status.idle":"2022-07-21T13:41:21.618184Z","shell.execute_reply.started":"2022-07-21T13:41:21.606154Z","shell.execute_reply":"2022-07-21T13:41:21.617154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat[\"CentralAir\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.619212Z","iopub.execute_input":"2022-07-21T13:41:21.620146Z","iopub.status.idle":"2022-07-21T13:41:21.630874Z","shell.execute_reply.started":"2022-07-21T13:41:21.620024Z","shell.execute_reply":"2022-07-21T13:41:21.629865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat[\"GarageQual\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.632285Z","iopub.execute_input":"2022-07-21T13:41:21.632623Z","iopub.status.idle":"2022-07-21T13:41:21.646122Z","shell.execute_reply.started":"2022-07-21T13:41:21.632592Z","shell.execute_reply":"2022-07-21T13:41:21.645337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat[\"GarageCond\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.647258Z","iopub.execute_input":"2022-07-21T13:41:21.647994Z","iopub.status.idle":"2022-07-21T13:41:21.660041Z","shell.execute_reply.started":"2022-07-21T13:41:21.647955Z","shell.execute_reply":"2022-07-21T13:41:21.658812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat[\"SaleType\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.661909Z","iopub.execute_input":"2022-07-21T13:41:21.662655Z","iopub.status.idle":"2022-07-21T13:41:21.672040Z","shell.execute_reply.started":"2022-07-21T13:41:21.662610Z","shell.execute_reply":"2022-07-21T13:41:21.671081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat[\"PavedDrive\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.673768Z","iopub.execute_input":"2022-07-21T13:41:21.674224Z","iopub.status.idle":"2022-07-21T13:41:21.685897Z","shell.execute_reply.started":"2022-07-21T13:41:21.674181Z","shell.execute_reply":"2022-07-21T13:41:21.685011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat[\"ExterCond\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.687332Z","iopub.execute_input":"2022-07-21T13:41:21.688369Z","iopub.status.idle":"2022-07-21T13:41:21.700277Z","shell.execute_reply.started":"2022-07-21T13:41:21.688324Z","shell.execute_reply":"2022-07-21T13:41:21.699099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat[\"GarageCond\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.701867Z","iopub.execute_input":"2022-07-21T13:41:21.702608Z","iopub.status.idle":"2022-07-21T13:41:21.714316Z","shell.execute_reply.started":"2022-07-21T13:41:21.702563Z","shell.execute_reply":"2022-07-21T13:41:21.713140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat[\"MiscFeature\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.715679Z","iopub.execute_input":"2022-07-21T13:41:21.716000Z","iopub.status.idle":"2022-07-21T13:41:21.729025Z","shell.execute_reply.started":"2022-07-21T13:41:21.715969Z","shell.execute_reply":"2022-07-21T13:41:21.728230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat[\"Heating\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.730348Z","iopub.execute_input":"2022-07-21T13:41:21.731371Z","iopub.status.idle":"2022-07-21T13:41:21.741519Z","shell.execute_reply.started":"2022-07-21T13:41:21.731326Z","shell.execute_reply":"2022-07-21T13:41:21.740326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat[\"BsmtFinType2\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.743399Z","iopub.execute_input":"2022-07-21T13:41:21.744155Z","iopub.status.idle":"2022-07-21T13:41:21.755838Z","shell.execute_reply.started":"2022-07-21T13:41:21.744110Z","shell.execute_reply":"2022-07-21T13:41:21.754588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat[\"Functional\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.757186Z","iopub.execute_input":"2022-07-21T13:41:21.760610Z","iopub.status.idle":"2022-07-21T13:41:21.770324Z","shell.execute_reply.started":"2022-07-21T13:41:21.760562Z","shell.execute_reply":"2022-07-21T13:41:21.768993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat[\"Electrical\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.772882Z","iopub.execute_input":"2022-07-21T13:41:21.773931Z","iopub.status.idle":"2022-07-21T13:41:21.782779Z","shell.execute_reply.started":"2022-07-21T13:41:21.773887Z","shell.execute_reply":"2022-07-21T13:41:21.781650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#dropping Mix as we won't be able to label encode with it and also it doesn't contain much scope of information","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.783876Z","iopub.execute_input":"2022-07-21T13:41:21.784618Z","iopub.status.idle":"2022-07-21T13:41:21.789632Z","shell.execute_reply.started":"2022-07-21T13:41:21.784546Z","shell.execute_reply":"2022-07-21T13:41:21.788360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat[df_cat[\"Electrical\"]=='Mix'].index","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.792196Z","iopub.execute_input":"2022-07-21T13:41:21.792786Z","iopub.status.idle":"2022-07-21T13:41:21.801712Z","shell.execute_reply.started":"2022-07-21T13:41:21.792740Z","shell.execute_reply":"2022-07-21T13:41:21.800931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat.drop([398],axis=0,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.802876Z","iopub.execute_input":"2022-07-21T13:41:21.803217Z","iopub.status.idle":"2022-07-21T13:41:21.811178Z","shell.execute_reply.started":"2022-07-21T13:41:21.803179Z","shell.execute_reply":"2022-07-21T13:41:21.810185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat['BsmtCond'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.812507Z","iopub.execute_input":"2022-07-21T13:41:21.812931Z","iopub.status.idle":"2022-07-21T13:41:21.826784Z","shell.execute_reply.started":"2022-07-21T13:41:21.812897Z","shell.execute_reply":"2022-07-21T13:41:21.825894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#dropping all the above features since they are highly imbalanced and have a lot of classes which would lead to higher\n#dimensions for very less gain. Even if we reduce them to binary classes, the imbalance would not reduce and applying\n#techniques like SMOTE for each feature would lead to high correlation in data and is generally not a right approach\n#in high dimensional setup","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.828073Z","iopub.execute_input":"2022-07-21T13:41:21.828735Z","iopub.status.idle":"2022-07-21T13:41:21.835741Z","shell.execute_reply.started":"2022-07-21T13:41:21.828674Z","shell.execute_reply":"2022-07-21T13:41:21.834896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"drop = [\"Street\",\"Utilities\",\"LandContour\",\"Functional\", \"BsmtFinType2\",\"LandSlope\",\"LandContour\",\"Condition1\",\n        \"Condition2\",\"RoofMatl\",\"CentralAir\",\"GarageQual\",\"GarageCond\",\"SaleType\",\"PavedDrive\",\"ExterCond\",\n        \"GarageCond\",\"Heating\",\"MiscFeature\",\"BsmtCond\"]","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.836928Z","iopub.execute_input":"2022-07-21T13:41:21.837425Z","iopub.status.idle":"2022-07-21T13:41:21.850918Z","shell.execute_reply.started":"2022-07-21T13:41:21.837391Z","shell.execute_reply":"2022-07-21T13:41:21.849891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat.drop(drop, axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.852693Z","iopub.execute_input":"2022-07-21T13:41:21.853480Z","iopub.status.idle":"2022-07-21T13:41:21.861782Z","shell.execute_reply.started":"2022-07-21T13:41:21.853412Z","shell.execute_reply":"2022-07-21T13:41:21.861065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.862939Z","iopub.execute_input":"2022-07-21T13:41:21.863550Z","iopub.status.idle":"2022-07-21T13:41:21.875214Z","shell.execute_reply.started":"2022-07-21T13:41:21.863519Z","shell.execute_reply":"2022-07-21T13:41:21.874102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.876634Z","iopub.execute_input":"2022-07-21T13:41:21.876941Z","iopub.status.idle":"2022-07-21T13:41:21.886422Z","shell.execute_reply.started":"2022-07-21T13:41:21.876911Z","shell.execute_reply":"2022-07-21T13:41:21.885196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(df_cat.isna().sum(axis=0)/len(df_cat)*100).sort_values(by=0,ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.888338Z","iopub.execute_input":"2022-07-21T13:41:21.889094Z","iopub.status.idle":"2022-07-21T13:41:21.910485Z","shell.execute_reply.started":"2022-07-21T13:41:21.889050Z","shell.execute_reply":"2022-07-21T13:41:21.909299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat.drop([\"PoolQC\",\"Alley\",\"Fence\",\"FireplaceQu\"],axis=1,inplace=True)\npd.DataFrame(df_cat.isna().sum(axis=0)/len(df_cat)*100).sort_values(by=0,ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.911832Z","iopub.execute_input":"2022-07-21T13:41:21.912118Z","iopub.status.idle":"2022-07-21T13:41:21.931028Z","shell.execute_reply.started":"2022-07-21T13:41:21.912081Z","shell.execute_reply":"2022-07-21T13:41:21.929928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#removed features with very high number of missing values","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.932784Z","iopub.execute_input":"2022-07-21T13:41:21.933084Z","iopub.status.idle":"2022-07-21T13:41:21.937283Z","shell.execute_reply.started":"2022-07-21T13:41:21.933050Z","shell.execute_reply":"2022-07-21T13:41:21.936229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(7, 3, figsize=(25, 50))\n\nfor i, ax in enumerate(fig.axes):\n    if i < len(df_cat.columns) - 1:\n        ax.set_xticklabels(ax.xaxis.get_majorticklabels(), rotation=45)\n        sns.countplot(x=df_cat.columns[i], alpha=0.7, data=df_cat, ax=ax)\n\nfig.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:21.938720Z","iopub.execute_input":"2022-07-21T13:41:21.939780Z","iopub.status.idle":"2022-07-21T13:41:25.400115Z","shell.execute_reply.started":"2022-07-21T13:41:21.939737Z","shell.execute_reply":"2022-07-21T13:41:25.399261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(df_cat.isna().sum(axis=0)).sort_values(by=0)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:25.401152Z","iopub.execute_input":"2022-07-21T13:41:25.402034Z","iopub.status.idle":"2022-07-21T13:41:25.417433Z","shell.execute_reply.started":"2022-07-21T13:41:25.402001Z","shell.execute_reply":"2022-07-21T13:41:25.416231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mod = {\"BsmtQual\": df_cat[\"BsmtQual\"].mode().iloc[0],\n        \"Electrical\": df_cat[\"Electrical\"].mode().iloc[0],\n       \"MasVnrType\": df_cat[\"MasVnrType\"].mode().iloc[0],\n       \"BsmtFinType1\": df_cat[\"BsmtFinType1\"].mode().iloc[0],\n        \"BsmtExposure\": df_cat[\"BsmtExposure\"].mode().iloc[0],\n        \"GarageType\": df_cat[\"GarageType\"].mode().iloc[0],\n        \"GarageFinish\": df_cat[\"GarageFinish\"].mode().iloc[0]}\n\ndf_cat = df_cat.fillna(value=mod)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:25.418839Z","iopub.execute_input":"2022-07-21T13:41:25.419267Z","iopub.status.idle":"2022-07-21T13:41:25.434589Z","shell.execute_reply.started":"2022-07-21T13:41:25.419236Z","shell.execute_reply":"2022-07-21T13:41:25.433482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat[['GarageType','GarageFinish']]","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:25.435900Z","iopub.execute_input":"2022-07-21T13:41:25.436233Z","iopub.status.idle":"2022-07-21T13:41:25.455371Z","shell.execute_reply.started":"2022-07-21T13:41:25.436201Z","shell.execute_reply":"2022-07-21T13:41:25.454099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ct=pd.crosstab(df_cat.GarageType,df_cat.GarageFinish)\nct","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:25.456816Z","iopub.execute_input":"2022-07-21T13:41:25.457249Z","iopub.status.idle":"2022-07-21T13:41:25.485469Z","shell.execute_reply.started":"2022-07-21T13:41:25.457206Z","shell.execute_reply":"2022-07-21T13:41:25.484222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"chi=chi2_contingency(ct)\nprint(\"p value is:\",chi[1])","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:25.486980Z","iopub.execute_input":"2022-07-21T13:41:25.487637Z","iopub.status.idle":"2022-07-21T13:41:25.494520Z","shell.execute_reply.started":"2022-07-21T13:41:25.487592Z","shell.execute_reply":"2022-07-21T13:41:25.493541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#the above test was performed to check if the two features are not correlated(null hypothesis) which is clearly rejected\n#as the p value is very small","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:25.496534Z","iopub.execute_input":"2022-07-21T13:41:25.497302Z","iopub.status.idle":"2022-07-21T13:41:25.507582Z","shell.execute_reply.started":"2022-07-21T13:41:25.497259Z","shell.execute_reply":"2022-07-21T13:41:25.506786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:25.508629Z","iopub.execute_input":"2022-07-21T13:41:25.509366Z","iopub.status.idle":"2022-07-21T13:41:25.520341Z","shell.execute_reply.started":"2022-07-21T13:41:25.509334Z","shell.execute_reply":"2022-07-21T13:41:25.519217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_feat=['LotShape','ExterQual','BsmtQual','BsmtExposure','BsmtFinType1', 'HeatingQC', 'Electrical','KitchenQual','GarageType', 'GarageFinish' ]","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:25.521797Z","iopub.execute_input":"2022-07-21T13:41:25.522148Z","iopub.status.idle":"2022-07-21T13:41:25.528608Z","shell.execute_reply.started":"2022-07-21T13:41:25.522119Z","shell.execute_reply":"2022-07-21T13:41:25.527657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_encoder = preprocessing.LabelEncoder()\nfor i in label_feat:\n    df_cat[i]=label_encoder.fit_transform(df_cat[i])","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:25.529817Z","iopub.execute_input":"2022-07-21T13:41:25.530936Z","iopub.status.idle":"2022-07-21T13:41:25.547269Z","shell.execute_reply.started":"2022-07-21T13:41:25.530889Z","shell.execute_reply":"2022-07-21T13:41:25.546420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"one_feat=[x for x in df_cat.columns if x not in label_feat]","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:25.551070Z","iopub.execute_input":"2022-07-21T13:41:25.551700Z","iopub.status.idle":"2022-07-21T13:41:25.557192Z","shell.execute_reply.started":"2022-07-21T13:41:25.551651Z","shell.execute_reply":"2022-07-21T13:41:25.556208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"one_feat.remove('target')\none_feat","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:25.558828Z","iopub.execute_input":"2022-07-21T13:41:25.559628Z","iopub.status.idle":"2022-07-21T13:41:25.569873Z","shell.execute_reply.started":"2022-07-21T13:41:25.559583Z","shell.execute_reply":"2022-07-21T13:41:25.569031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat_final = pd.get_dummies(df_cat, columns = one_feat)\ndf_cat_final.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:25.571334Z","iopub.execute_input":"2022-07-21T13:41:25.571642Z","iopub.status.idle":"2022-07-21T13:41:25.604287Z","shell.execute_reply.started":"2022-07-21T13:41:25.571614Z","shell.execute_reply":"2022-07-21T13:41:25.603208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imputed.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:25.605838Z","iopub.execute_input":"2022-07-21T13:41:25.606183Z","iopub.status.idle":"2022-07-21T13:41:25.613792Z","shell.execute_reply.started":"2022-07-21T13:41:25.606128Z","shell.execute_reply":"2022-07-21T13:41:25.612691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig_ = train_imputed.hist(figsize=(25, 24), bins=60,xlabelsize=10, ylabelsize=10)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:25.615679Z","iopub.execute_input":"2022-07-21T13:41:25.616521Z","iopub.status.idle":"2022-07-21T13:41:33.761722Z","shell.execute_reply.started":"2022-07-21T13:41:25.616477Z","shell.execute_reply":"2022-07-21T13:41:33.760591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imputed[\"MiscVal\"].describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:33.763438Z","iopub.execute_input":"2022-07-21T13:41:33.764102Z","iopub.status.idle":"2022-07-21T13:41:33.775176Z","shell.execute_reply.started":"2022-07-21T13:41:33.764047Z","shell.execute_reply":"2022-07-21T13:41:33.774245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imputed[\"MiscVal\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:33.776521Z","iopub.execute_input":"2022-07-21T13:41:33.776823Z","iopub.status.idle":"2022-07-21T13:41:33.790616Z","shell.execute_reply.started":"2022-07-21T13:41:33.776796Z","shell.execute_reply":"2022-07-21T13:41:33.789327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# this variable doesn't seem to give high value to our model and is severely skewed with lots of 0 values. \n#It seems it would be better to drop it","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:33.793076Z","iopub.execute_input":"2022-07-21T13:41:33.794153Z","iopub.status.idle":"2022-07-21T13:41:33.797872Z","shell.execute_reply.started":"2022-07-21T13:41:33.794114Z","shell.execute_reply":"2022-07-21T13:41:33.796779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imputed[\"WoodDeckSF\"].describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:33.799944Z","iopub.execute_input":"2022-07-21T13:41:33.800456Z","iopub.status.idle":"2022-07-21T13:41:33.817318Z","shell.execute_reply.started":"2022-07-21T13:41:33.800413Z","shell.execute_reply":"2022-07-21T13:41:33.816312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#(wooddeck area) this variable doesn't seem to give high value to our model and is severely skewed with lots of 0 values.\n#It seems it would be better to drop it","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:33.818796Z","iopub.execute_input":"2022-07-21T13:41:33.819340Z","iopub.status.idle":"2022-07-21T13:41:33.823062Z","shell.execute_reply.started":"2022-07-21T13:41:33.819308Z","shell.execute_reply":"2022-07-21T13:41:33.821956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imputed[\"OpenPorchSF\"].describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:33.824569Z","iopub.execute_input":"2022-07-21T13:41:33.824938Z","iopub.status.idle":"2022-07-21T13:41:33.841883Z","shell.execute_reply.started":"2022-07-21T13:41:33.824902Z","shell.execute_reply":"2022-07-21T13:41:33.840786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imputed[\"OpenPorchSF\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:33.843224Z","iopub.execute_input":"2022-07-21T13:41:33.843988Z","iopub.status.idle":"2022-07-21T13:41:33.853762Z","shell.execute_reply.started":"2022-07-21T13:41:33.843954Z","shell.execute_reply":"2022-07-21T13:41:33.852593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# this variable should be dropped as well(open porch area)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:33.855358Z","iopub.execute_input":"2022-07-21T13:41:33.856008Z","iopub.status.idle":"2022-07-21T13:41:33.862252Z","shell.execute_reply.started":"2022-07-21T13:41:33.855962Z","shell.execute_reply":"2022-07-21T13:41:33.860457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imputed[\"ScreenPorch\"].describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:33.863541Z","iopub.execute_input":"2022-07-21T13:41:33.864424Z","iopub.status.idle":"2022-07-21T13:41:33.877739Z","shell.execute_reply.started":"2022-07-21T13:41:33.864393Z","shell.execute_reply":"2022-07-21T13:41:33.876716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imputed[\"ScreenPorch\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:33.893633Z","iopub.execute_input":"2022-07-21T13:41:33.894340Z","iopub.status.idle":"2022-07-21T13:41:33.905546Z","shell.execute_reply.started":"2022-07-21T13:41:33.894303Z","shell.execute_reply":"2022-07-21T13:41:33.904075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#ADD TO DROP LIST","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:33.907437Z","iopub.execute_input":"2022-07-21T13:41:33.908134Z","iopub.status.idle":"2022-07-21T13:41:33.913566Z","shell.execute_reply.started":"2022-07-21T13:41:33.908089Z","shell.execute_reply":"2022-07-21T13:41:33.912348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imputed[\"EnclosedPorch\"].describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:33.914988Z","iopub.execute_input":"2022-07-21T13:41:33.915320Z","iopub.status.idle":"2022-07-21T13:41:33.930137Z","shell.execute_reply.started":"2022-07-21T13:41:33.915289Z","shell.execute_reply":"2022-07-21T13:41:33.929234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imputed[\"EnclosedPorch\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:33.931522Z","iopub.execute_input":"2022-07-21T13:41:33.932261Z","iopub.status.idle":"2022-07-21T13:41:33.942493Z","shell.execute_reply.started":"2022-07-21T13:41:33.932215Z","shell.execute_reply":"2022-07-21T13:41:33.941395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from statsmodels.stats.weightstats import ztest ","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:33.943873Z","iopub.execute_input":"2022-07-21T13:41:33.944839Z","iopub.status.idle":"2022-07-21T13:41:33.948982Z","shell.execute_reply.started":"2022-07-21T13:41:33.944803Z","shell.execute_reply":"2022-07-21T13:41:33.948235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ztest(train_imputed[\"EnclosedPorch\"],train_imputed[\"ScreenPorch\"], value=0) ","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:33.950496Z","iopub.execute_input":"2022-07-21T13:41:33.951560Z","iopub.status.idle":"2022-07-21T13:41:33.963708Z","shell.execute_reply.started":"2022-07-21T13:41:33.951515Z","shell.execute_reply":"2022-07-21T13:41:33.962447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#above test implies that there is a significant difference between Enclosed porch and Screen porch","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:33.965403Z","iopub.execute_input":"2022-07-21T13:41:33.966299Z","iopub.status.idle":"2022-07-21T13:41:33.970153Z","shell.execute_reply.started":"2022-07-21T13:41:33.966265Z","shell.execute_reply":"2022-07-21T13:41:33.969263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"drop=[\"MiscVal\",\"WoodDeckSF\",\"OpenPorchSF\",\"ScreenPorch\",\"EnclosedPorch\"]","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:33.971300Z","iopub.execute_input":"2022-07-21T13:41:33.971936Z","iopub.status.idle":"2022-07-21T13:41:33.981549Z","shell.execute_reply.started":"2022-07-21T13:41:33.971905Z","shell.execute_reply":"2022-07-21T13:41:33.980569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imputed.drop(drop,axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:33.983220Z","iopub.execute_input":"2022-07-21T13:41:33.983734Z","iopub.status.idle":"2022-07-21T13:41:33.992826Z","shell.execute_reply.started":"2022-07-21T13:41:33.983701Z","shell.execute_reply":"2022-07-21T13:41:33.992118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##FEATURE ENGINEERING","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:33.993898Z","iopub.execute_input":"2022-07-21T13:41:33.994228Z","iopub.status.idle":"2022-07-21T13:41:34.003826Z","shell.execute_reply.started":"2022-07-21T13:41:33.994191Z","shell.execute_reply":"2022-07-21T13:41:34.003037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imputed[\"floor_area\"]=train_imputed['1stFlrSF']+train_imputed['2ndFlrSF']","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:34.004890Z","iopub.execute_input":"2022-07-21T13:41:34.005700Z","iopub.status.idle":"2022-07-21T13:41:34.015187Z","shell.execute_reply.started":"2022-07-21T13:41:34.005664Z","shell.execute_reply":"2022-07-21T13:41:34.014235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imputed.drop(['1stFlrSF', '2ndFlrSF'],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:34.016488Z","iopub.execute_input":"2022-07-21T13:41:34.016990Z","iopub.status.idle":"2022-07-21T13:41:34.032059Z","shell.execute_reply.started":"2022-07-21T13:41:34.016958Z","shell.execute_reply":"2022-07-21T13:41:34.031031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#dropped first floor and second floor area and took there combined area as a new feature","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:34.033302Z","iopub.execute_input":"2022-07-21T13:41:34.033891Z","iopub.status.idle":"2022-07-21T13:41:34.041321Z","shell.execute_reply.started":"2022-07-21T13:41:34.033858Z","shell.execute_reply":"2022-07-21T13:41:34.040507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imputed.YearBuilt","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:34.042546Z","iopub.execute_input":"2022-07-21T13:41:34.043035Z","iopub.status.idle":"2022-07-21T13:41:34.056700Z","shell.execute_reply.started":"2022-07-21T13:41:34.043001Z","shell.execute_reply":"2022-07-21T13:41:34.055704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imputed[\"age_since_built\"]=train_imputed[\"YearBuilt\"].max()-train_imputed[\"YearBuilt\"]","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:34.057657Z","iopub.execute_input":"2022-07-21T13:41:34.058311Z","iopub.status.idle":"2022-07-21T13:41:34.069465Z","shell.execute_reply.started":"2022-07-21T13:41:34.058278Z","shell.execute_reply":"2022-07-21T13:41:34.068372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#taking years since built with most recent built as the base year. Similarly, we do the same for remod","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:34.071227Z","iopub.execute_input":"2022-07-21T13:41:34.072020Z","iopub.status.idle":"2022-07-21T13:41:34.080239Z","shell.execute_reply.started":"2022-07-21T13:41:34.071974Z","shell.execute_reply":"2022-07-21T13:41:34.079068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imputed[\"age_since_remod\"] =train_imputed[\"YearRemodAdd\"].max() - train_imputed[\"YearRemodAdd\"]","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:34.081655Z","iopub.execute_input":"2022-07-21T13:41:34.082378Z","iopub.status.idle":"2022-07-21T13:41:34.092877Z","shell.execute_reply.started":"2022-07-21T13:41:34.082333Z","shell.execute_reply":"2022-07-21T13:41:34.092070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imputed.drop([\"YearBuilt\",\"YearRemodAdd\"],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:34.093949Z","iopub.execute_input":"2022-07-21T13:41:34.094648Z","iopub.status.idle":"2022-07-21T13:41:34.103318Z","shell.execute_reply.started":"2022-07-21T13:41:34.094614Z","shell.execute_reply":"2022-07-21T13:41:34.102490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imputed['age_garage'] =train_imputed['GarageYrBlt'].max() - train_imputed['GarageYrBlt']","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:34.104452Z","iopub.execute_input":"2022-07-21T13:41:34.105264Z","iopub.status.idle":"2022-07-21T13:41:34.114530Z","shell.execute_reply.started":"2022-07-21T13:41:34.105232Z","shell.execute_reply":"2022-07-21T13:41:34.113585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imputed.drop(\"GarageYrBlt\",axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:34.116375Z","iopub.execute_input":"2022-07-21T13:41:34.117016Z","iopub.status.idle":"2022-07-21T13:41:34.124994Z","shell.execute_reply.started":"2022-07-21T13:41:34.116971Z","shell.execute_reply":"2022-07-21T13:41:34.124260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imputed.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:34.126356Z","iopub.execute_input":"2022-07-21T13:41:34.128534Z","iopub.status.idle":"2022-07-21T13:41:34.137438Z","shell.execute_reply.started":"2022-07-21T13:41:34.128498Z","shell.execute_reply":"2022-07-21T13:41:34.136481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imputed['bsmtfnsh']=train_imputed['BsmtFinSF1']+train_imputed['BsmtFinSF2']","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:34.138433Z","iopub.execute_input":"2022-07-21T13:41:34.139023Z","iopub.status.idle":"2022-07-21T13:41:34.147111Z","shell.execute_reply.started":"2022-07-21T13:41:34.138993Z","shell.execute_reply":"2022-07-21T13:41:34.146425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imputed.drop(['BsmtFinSF1','BsmtFinSF2'],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:34.148178Z","iopub.execute_input":"2022-07-21T13:41:34.148872Z","iopub.status.idle":"2022-07-21T13:41:34.158397Z","shell.execute_reply.started":"2022-07-21T13:41:34.148842Z","shell.execute_reply":"2022-07-21T13:41:34.157646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#dropped basement type1 and 2 finished and took their combined feature ","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:34.159561Z","iopub.execute_input":"2022-07-21T13:41:34.160038Z","iopub.status.idle":"2022-07-21T13:41:34.167964Z","shell.execute_reply.started":"2022-07-21T13:41:34.160008Z","shell.execute_reply":"2022-07-21T13:41:34.166945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig_ = train_imputed.hist(figsize=(25, 24), bins=60,xlabelsize=10, ylabelsize=10)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:34.169292Z","iopub.execute_input":"2022-07-21T13:41:34.169613Z","iopub.status.idle":"2022-07-21T13:41:41.130072Z","shell.execute_reply.started":"2022-07-21T13:41:34.169583Z","shell.execute_reply":"2022-07-21T13:41:41.128796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cont= [\"age_since_built\", \"age_since_remod\",'LotFrontage', 'LotArea', \"GrLivArea\",\n       \"GarageArea\",\"age_garage\",\"floor_area\",\"MasVnrArea\",'bsmtfnsh','BsmtUnfSF', 'TotalBsmtSF']\ndf_skew = train_imputed.loc[:, cont]","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:41.131320Z","iopub.execute_input":"2022-07-21T13:41:41.131649Z","iopub.status.idle":"2022-07-21T13:41:41.138596Z","shell.execute_reply.started":"2022-07-21T13:41:41.131618Z","shell.execute_reply":"2022-07-21T13:41:41.137208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_skew_fin=pd.DataFrame(df_skew.skew(axis=0),columns=[\"skewness\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:41.140127Z","iopub.execute_input":"2022-07-21T13:41:41.141193Z","iopub.status.idle":"2022-07-21T13:41:41.150515Z","shell.execute_reply.started":"2022-07-21T13:41:41.141128Z","shell.execute_reply":"2022-07-21T13:41:41.149588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_skew_fin[df_skew_fin.skewness>1]","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:41.151586Z","iopub.execute_input":"2022-07-21T13:41:41.152620Z","iopub.status.idle":"2022-07-21T13:41:41.167681Z","shell.execute_reply.started":"2022-07-21T13:41:41.152570Z","shell.execute_reply":"2022-07-21T13:41:41.166343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features=[\"LotFrontage\",\"LotArea\",\"GrLivArea\",\"floor_area\",\"MasVnrArea\",\"bsmtfnsh\",\"TotalBsmtSF\"]\n","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:41.169511Z","iopub.execute_input":"2022-07-21T13:41:41.170027Z","iopub.status.idle":"2022-07-21T13:41:41.176568Z","shell.execute_reply.started":"2022-07-21T13:41:41.169979Z","shell.execute_reply":"2022-07-21T13:41:41.175441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#we would apply log transformation for skewed data after a few steps","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:41.178274Z","iopub.execute_input":"2022-07-21T13:41:41.178614Z","iopub.status.idle":"2022-07-21T13:41:41.188044Z","shell.execute_reply.started":"2022-07-21T13:41:41.178574Z","shell.execute_reply":"2022-07-21T13:41:41.187238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=train_imputed.join(df_cat_final)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:41.189868Z","iopub.execute_input":"2022-07-21T13:41:41.190321Z","iopub.status.idle":"2022-07-21T13:41:41.204220Z","shell.execute_reply.started":"2022-07-21T13:41:41.190277Z","shell.execute_reply":"2022-07-21T13:41:41.202858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop('target',axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:41.207325Z","iopub.execute_input":"2022-07-21T13:41:41.208405Z","iopub.status.idle":"2022-07-21T13:41:41.215974Z","shell.execute_reply.started":"2022-07-21T13:41:41.208364Z","shell.execute_reply":"2022-07-21T13:41:41.214645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.dropna(axis=0,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:41.218009Z","iopub.execute_input":"2022-07-21T13:41:41.218999Z","iopub.status.idle":"2022-07-21T13:41:41.228026Z","shell.execute_reply.started":"2022-07-21T13:41:41.218948Z","shell.execute_reply":"2022-07-21T13:41:41.226879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from scipy import stats\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:41.230245Z","iopub.execute_input":"2022-07-21T13:41:41.231311Z","iopub.status.idle":"2022-07-21T13:41:41.236811Z","shell.execute_reply.started":"2022-07-21T13:41:41.231261Z","shell.execute_reply":"2022-07-21T13:41:41.235665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#we apply box cox transformation on the saleprice to make its distribution normal","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:41.239283Z","iopub.execute_input":"2022-07-21T13:41:41.240188Z","iopub.status.idle":"2022-07-21T13:41:41.248280Z","shell.execute_reply.started":"2022-07-21T13:41:41.240116Z","shell.execute_reply":"2022-07-21T13:41:41.247121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['SalePricebox'],parameters=stats.boxcox(df['SalePrice']) ","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:41.249787Z","iopub.execute_input":"2022-07-21T13:41:41.250428Z","iopub.status.idle":"2022-07-21T13:41:41.267689Z","shell.execute_reply.started":"2022-07-21T13:41:41.250392Z","shell.execute_reply":"2022-07-21T13:41:41.266381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop([\"SalePrice\"], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:41.269491Z","iopub.execute_input":"2022-07-21T13:41:41.269825Z","iopub.status.idle":"2022-07-21T13:41:41.278705Z","shell.execute_reply.started":"2022-07-21T13:41:41.269794Z","shell.execute_reply":"2022-07-21T13:41:41.277754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df[[i for i in list(df.columns) if i != \"SalePricebox\"]]\nX.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:41.282117Z","iopub.execute_input":"2022-07-21T13:41:41.282628Z","iopub.status.idle":"2022-07-21T13:41:41.293577Z","shell.execute_reply.started":"2022-07-21T13:41:41.282580Z","shell.execute_reply":"2022-07-21T13:41:41.292221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y=df.SalePricebox\nY.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:41.296477Z","iopub.execute_input":"2022-07-21T13:41:41.296960Z","iopub.status.idle":"2022-07-21T13:41:41.304871Z","shell.execute_reply.started":"2022-07-21T13:41:41.296914Z","shell.execute_reply":"2022-07-21T13:41:41.303741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in features:\n    X[i] = np.log((X[i])+1)\n    ","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:41.306188Z","iopub.execute_input":"2022-07-21T13:41:41.307348Z","iopub.status.idle":"2022-07-21T13:41:41.318421Z","shell.execute_reply.started":"2022-07-21T13:41:41.307308Z","shell.execute_reply":"2022-07-21T13:41:41.317421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#log transformed skewed features","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:41.319827Z","iopub.execute_input":"2022-07-21T13:41:41.320755Z","iopub.status.idle":"2022-07-21T13:41:41.325131Z","shell.execute_reply.started":"2022-07-21T13:41:41.320719Z","shell.execute_reply":"2022-07-21T13:41:41.324071Z"},"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.3, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:41.326813Z","iopub.execute_input":"2022-07-21T13:41:41.327141Z","iopub.status.idle":"2022-07-21T13:41:41.341506Z","shell.execute_reply.started":"2022-07-21T13:41:41.327101Z","shell.execute_reply":"2022-07-21T13:41:41.340318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#now we remove outliers on the basis of Cook's Distance","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:41.342753Z","iopub.execute_input":"2022-07-21T13:41:41.343698Z","iopub.status.idle":"2022-07-21T13:41:41.348496Z","shell.execute_reply.started":"2022-07-21T13:41:41.343663Z","shell.execute_reply":"2022-07-21T13:41:41.347618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape\n","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:41.349818Z","iopub.execute_input":"2022-07-21T13:41:41.350391Z","iopub.status.idle":"2022-07-21T13:41:41.360074Z","shell.execute_reply.started":"2022-07-21T13:41:41.350358Z","shell.execute_reply":"2022-07-21T13:41:41.359222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:41.361894Z","iopub.execute_input":"2022-07-21T13:41:41.362613Z","iopub.status.idle":"2022-07-21T13:41:41.369985Z","shell.execute_reply.started":"2022-07-21T13:41:41.362565Z","shell.execute_reply":"2022-07-21T13:41:41.368796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = sm.OLS(y_train, X_train)\nlr = model.fit()\nnp.set_printoptions(suppress=True)\ninfluence = lr.get_influence()\ncooks = influence.cooks_distance\ncook_df = pd.DataFrame({\"Cook_Distance\": cooks[0], \"p_value\": cooks[1]})\ncook_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:41.371248Z","iopub.execute_input":"2022-07-21T13:41:41.371740Z","iopub.status.idle":"2022-07-21T13:41:41.446959Z","shell.execute_reply.started":"2022-07-21T13:41:41.371707Z","shell.execute_reply":"2022-07-21T13:41:41.445756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"influent_observation = cook_df[cook_df[\"p_value\"] < 0.05].index.tolist()\nprint(f\"Influential observations dropped: {influent_observation}\")\n\nX_train = X_train.drop(X_train.index[influent_observation])\n","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:41.448853Z","iopub.execute_input":"2022-07-21T13:41:41.449646Z","iopub.status.idle":"2022-07-21T13:41:41.460807Z","shell.execute_reply.started":"2022-07-21T13:41:41.449593Z","shell.execute_reply":"2022-07-21T13:41:41.459372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:41.462783Z","iopub.execute_input":"2022-07-21T13:41:41.463873Z","iopub.status.idle":"2022-07-21T13:41:41.471976Z","shell.execute_reply.started":"2022-07-21T13:41:41.463822Z","shell.execute_reply":"2022-07-21T13:41:41.470715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = pd.DataFrame(y_train)\ny_train = y_train.drop(y_train.index[influent_observation])\n","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:41.474244Z","iopub.execute_input":"2022-07-21T13:41:41.475224Z","iopub.status.idle":"2022-07-21T13:41:41.489926Z","shell.execute_reply.started":"2022-07-21T13:41:41.475152Z","shell.execute_reply":"2022-07-21T13:41:41.488497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:41.491816Z","iopub.execute_input":"2022-07-21T13:41:41.492614Z","iopub.status.idle":"2022-07-21T13:41:41.510918Z","shell.execute_reply.started":"2022-07-21T13:41:41.492563Z","shell.execute_reply":"2022-07-21T13:41:41.509366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sklearn\nfrom sklearn.metrics import mean_squared_error, r2_score\nfrom sklearn.linear_model import ElasticNet","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:41.513069Z","iopub.execute_input":"2022-07-21T13:41:41.514022Z","iopub.status.idle":"2022-07-21T13:41:41.520638Z","shell.execute_reply.started":"2022-07-21T13:41:41.513923Z","shell.execute_reply":"2022-07-21T13:41:41.519390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from mlxtend.feature_selection import SequentialFeatureSelector as sfs\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.linear_model import Ridge\nfrom sklearn.linear_model import Lasso\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn.linear_model import ElasticNet\nfrom sklearn.ensemble import RandomForestRegressor\nfrom xgboost import XGBRegressor","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:41.522500Z","iopub.execute_input":"2022-07-21T13:41:41.523288Z","iopub.status.idle":"2022-07-21T13:41:41.722705Z","shell.execute_reply.started":"2022-07-21T13:41:41.523235Z","shell.execute_reply":"2022-07-21T13:41:41.721389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# lreg = LinearRegression()\n\n# sfs1 = sfs(lreg, k_features=90, forward=False, verbose=1, scoring='neg_mean_squared_error')\n# sfs1 = sfs1.fit(X_train, y_train)\n# feat_names = list(sfs1.k_feature_names_)\n# print(feat_names)\n# X_train_=X_train[feat_names]\n# X_test_=X_test[feat_names]\n# index=X_test_.isna().index\n# test_=X_test_.join(pd.DataFrame(y_test))\n# test_.columns\n# test_.dropna(axis=0,inplace=True)\n# X_test_=test_[feat_names]\n# y_test_=test_['SalePricebox']\n\n#ERROR","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:41.724229Z","iopub.execute_input":"2022-07-21T13:41:41.724678Z","iopub.status.idle":"2022-07-21T13:41:41.730115Z","shell.execute_reply.started":"2022-07-21T13:41:41.724632Z","shell.execute_reply":"2022-07-21T13:41:41.728996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"par = np.linspace(0, 20, 50).tolist()\nparameter = {\"alpha\": par}\nridge_cv = GridSearchCV(Ridge(),parameter, cv=15,verbose=1)\nridge_cv.fit(X_train, y_train)\nprint(f\"Best hyperparameters: {ridge_cv.best_params_}\")\nprint(f\"Best R² (train): {ridge_cv.best_score_}\")","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:41:41.731466Z","iopub.execute_input":"2022-07-21T13:41:41.731748Z","iopub.status.idle":"2022-07-21T13:42:08.953418Z","shell.execute_reply.started":"2022-07-21T13:41:41.731720Z","shell.execute_reply":"2022-07-21T13:42:08.951930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ridge_best = Ridge(alpha= 9.387755102040817)\nridge_best.fit(X_train, y_train)\ny_pred = ridge_best.predict(X_test)\nprint(\"RIDGE OPTIMIZED\")\nprint(\"MSE: \",mean_squared_error(y_test, y_pred))\nprint(\"R2 value is: \",r2_score(y_test,y_pred))","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:42:08.955594Z","iopub.execute_input":"2022-07-21T13:42:08.956068Z","iopub.status.idle":"2022-07-21T13:42:08.999041Z","shell.execute_reply.started":"2022-07-21T13:42:08.956020Z","shell.execute_reply":"2022-07-21T13:42:08.997745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"par = np.linspace(-1, 1, 50).tolist()\nparameter = {\"alpha\": par}\nlasso_cv = GridSearchCV(Lasso(),parameter, cv=15,verbose=1)\nlasso_cv.fit(X_train, y_train)\nprint(f\"Best hyperparameters: {lasso_cv.best_params_}\")\nprint(f\"Best R² (train): {lasso_cv.best_score_}\")","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:42:09.000921Z","iopub.execute_input":"2022-07-21T13:42:09.002719Z","iopub.status.idle":"2022-07-21T13:43:28.834617Z","shell.execute_reply.started":"2022-07-21T13:42:09.002665Z","shell.execute_reply":"2022-07-21T13:43:28.833308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lasso_best = Lasso(alpha= 0.020408163265306145)\nlasso_best.fit(X_train, y_train)\ny_pred = lasso_best.predict(X_test)\nprint(\"LASSO OPTIMIZED\")\nprint(\"MSE: \",mean_squared_error(y_test, y_pred))\nprint(\"R2 value is: \",r2_score(y_test,y_pred))","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:43:28.840644Z","iopub.execute_input":"2022-07-21T13:43:28.845018Z","iopub.status.idle":"2022-07-21T13:43:28.899538Z","shell.execute_reply.started":"2022-07-21T13:43:28.844960Z","shell.execute_reply":"2022-07-21T13:43:28.898286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grid = dict()\ngrid['alpha'] = np.linspace(-3,3,50)\ngrid['l1_ratio'] = np.linspace(-3,3,50)\nmodel = ElasticNet()\nsearch = GridSearchCV(model, grid,cv=10,n_jobs=-1)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:43:28.905612Z","iopub.execute_input":"2022-07-21T13:43:28.909737Z","iopub.status.idle":"2022-07-21T13:43:28.924542Z","shell.execute_reply.started":"2022-07-21T13:43:28.909676Z","shell.execute_reply":"2022-07-21T13:43:28.923137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"search.fit(X_train, y_train)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:43:28.930978Z","iopub.execute_input":"2022-07-21T13:43:28.934998Z","iopub.status.idle":"2022-07-21T13:45:21.062735Z","shell.execute_reply.started":"2022-07-21T13:43:28.934938Z","shell.execute_reply":"2022-07-21T13:45:21.060865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Best hyperparameters: {search.best_params_}\")\nprint(f\"Best R² (train): {search.best_score_}\")","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:45:21.065727Z","iopub.execute_input":"2022-07-21T13:45:21.068008Z","iopub.status.idle":"2022-07-21T13:45:21.077891Z","shell.execute_reply.started":"2022-07-21T13:45:21.067947Z","shell.execute_reply":"2022-07-21T13:45:21.076751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"e_net = ElasticNet(alpha = 0.06122448979591821,l1_ratio=0.06122448979591821)\ne_net.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:45:21.079983Z","iopub.execute_input":"2022-07-21T13:45:21.081672Z","iopub.status.idle":"2022-07-21T13:45:21.124007Z","shell.execute_reply.started":"2022-07-21T13:45:21.081624Z","shell.execute_reply":"2022-07-21T13:45:21.122721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = e_net.predict(X_test)\nprint(\"ELASTIC NET OPTIMIZED\")\nprint(\"MSE: \",mean_squared_error(y_test, y_pred))\nprint(\"R2 value is: \",r2_score(y_test,y_pred))","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:45:21.126841Z","iopub.execute_input":"2022-07-21T13:45:21.128546Z","iopub.status.idle":"2022-07-21T13:45:21.164229Z","shell.execute_reply.started":"2022-07-21T13:45:21.128498Z","shell.execute_reply":"2022-07-21T13:45:21.162281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tuned_parameters = {\"max_depth\": np.arange(2,5,1),\"learning_rate\": np.linspace(0.01,0.5,10),\n                    \"n_estimators\": np.arange(100,500,100)}\nxgbr_cv = GridSearchCV(estimator=XGBRegressor(),param_grid=tuned_parameters,cv=10,\n                       n_jobs=-1,verbose=1)\nxgbr_cv.fit(X_train, y_train)\nprint(f\"Best hyperparameters: {xgbr_cv.best_params_}\\n\")\nprint(f\"Best R²: {xgbr_cv.best_score_}\")\n","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:59:35.033530Z","iopub.execute_input":"2022-07-21T13:59:35.033940Z","iopub.status.idle":"2022-07-21T13:59:35.040891Z","shell.execute_reply.started":"2022-07-21T13:59:35.033907Z","shell.execute_reply":"2022-07-21T13:59:35.039738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xgbr = XGBRegressor(learning_rate=0.06444,max_depth=4,n_estimators=400)\nxgbr.fit(X_train, y_train)\ny_pred = xgbr.predict(X_test)\nprint(\"XGB REGRESSOR OPTIMIZED\")\nprint(\"MSE: \",mean_squared_error(y_test, y_pred))\nprint(\"R2 value is: \",r2_score(y_test,y_pred))","metadata":{"execution":{"iopub.status.busy":"2022-07-21T13:55:27.055063Z","iopub.status.idle":"2022-07-21T13:55:27.055682Z","shell.execute_reply.started":"2022-07-21T13:55:27.055393Z","shell.execute_reply":"2022-07-21T13:55:27.055420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}