{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# **** HOUSE PRICE PREDICTION","metadata":{}},{"cell_type":"markdown","source":"### TABLE of CONTENTS:\n\n* [Import Libraries](#sec1)\n* [Import CSV](#sec2)\n* [Basic Information](#sec3)\n* [EDA](#sec4)\n* [Missing Values](#sec5)\n* [Removing Outliers ](#sec6)\n* [Removing Multicollinearity and also features with poor correlation](#sec7)\n* [Standard Scaler and Imputation KMeans](#sec8)\n* [Filling Missing Values](#sec9)\n* [Join ](#sec10)\n* [One hot encoding ](#sec11)\n* [Train-Test Split ](#sec12)\n* [Prediction](#sec13)\n\n","metadata":{}},{"cell_type":"markdown","source":"# Import Libraries <a class=\"anchor\" id=\"sec1\"></a>","metadata":{}},{"cell_type":"code","source":"#importing libraries\nimport warnings\nwarnings.filterwarnings('ignore')\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:22:46.997744Z","iopub.execute_input":"2022-07-17T13:22:46.998253Z","iopub.status.idle":"2022-07-17T13:22:47.005647Z","shell.execute_reply.started":"2022-07-17T13:22:46.998217Z","shell.execute_reply":"2022-07-17T13:22:47.004023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('display.max_columns', None)\npd.set_option('display.max_rows', None)\npd.set_option('display.expand_frame_repr', False)\npd.set_option('max_colwidth', -1)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:22:47.267162Z","iopub.execute_input":"2022-07-17T13:22:47.268721Z","iopub.status.idle":"2022-07-17T13:22:47.277817Z","shell.execute_reply.started":"2022-07-17T13:22:47.268661Z","shell.execute_reply":"2022-07-17T13:22:47.276431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Import CSV <a class=\"anchor\" id=\"sec2\"></a>","metadata":{}},{"cell_type":"code","source":"df=pd.read_csv(\"../input/house-prices-advanced-regression-techniques/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:22:47.527118Z","iopub.execute_input":"2022-07-17T13:22:47.528425Z","iopub.status.idle":"2022-07-17T13:22:47.566995Z","shell.execute_reply.started":"2022-07-17T13:22:47.528378Z","shell.execute_reply":"2022-07-17T13:22:47.565947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_t=pd.read_csv(\"../input/house-prices-advanced-regression-techniques/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:22:47.956992Z","iopub.execute_input":"2022-07-17T13:22:47.957487Z","iopub.status.idle":"2022-07-17T13:22:47.991044Z","shell.execute_reply.started":"2022-07-17T13:22:47.957448Z","shell.execute_reply":"2022-07-17T13:22:47.989819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:22:48.226415Z","iopub.execute_input":"2022-07-17T13:22:48.226873Z","iopub.status.idle":"2022-07-17T13:22:48.292951Z","shell.execute_reply.started":"2022-07-17T13:22:48.226835Z","shell.execute_reply":"2022-07-17T13:22:48.291176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Basic Information <a class=\"anchor\" id=\"sec3\"></a>","metadata":{}},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:22:48.517240Z","iopub.execute_input":"2022-07-17T13:22:48.517713Z","iopub.status.idle":"2022-07-17T13:22:48.527123Z","shell.execute_reply.started":"2022-07-17T13:22:48.517677Z","shell.execute_reply":"2022-07-17T13:22:48.525292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_t.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:22:48.766717Z","iopub.execute_input":"2022-07-17T13:22:48.767985Z","iopub.status.idle":"2022-07-17T13:22:48.776618Z","shell.execute_reply.started":"2022-07-17T13:22:48.767922Z","shell.execute_reply":"2022-07-17T13:22:48.775252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:22:48.976695Z","iopub.execute_input":"2022-07-17T13:22:48.977353Z","iopub.status.idle":"2022-07-17T13:22:48.989104Z","shell.execute_reply.started":"2022-07-17T13:22:48.977302Z","shell.execute_reply":"2022-07-17T13:22:48.987768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:22:49.097084Z","iopub.execute_input":"2022-07-17T13:22:49.097531Z","iopub.status.idle":"2022-07-17T13:22:49.228658Z","shell.execute_reply.started":"2022-07-17T13:22:49.097498Z","shell.execute_reply":"2022-07-17T13:22:49.227718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:22:49.230167Z","iopub.execute_input":"2022-07-17T13:22:49.231771Z","iopub.status.idle":"2022-07-17T13:22:49.262619Z","shell.execute_reply.started":"2022-07-17T13:22:49.231724Z","shell.execute_reply":"2022-07-17T13:22:49.261739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.select_dtypes(include=['int64']).nunique().sort_values(ascending=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:22:49.304166Z","iopub.execute_input":"2022-07-17T13:22:49.305976Z","iopub.status.idle":"2022-07-17T13:22:49.327911Z","shell.execute_reply.started":"2022-07-17T13:22:49.305904Z","shell.execute_reply":"2022-07-17T13:22:49.326612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.select_dtypes(include=['float64']).nunique().sort_values(ascending=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:22:49.356576Z","iopub.execute_input":"2022-07-17T13:22:49.358084Z","iopub.status.idle":"2022-07-17T13:22:49.371577Z","shell.execute_reply.started":"2022-07-17T13:22:49.358041Z","shell.execute_reply":"2022-07-17T13:22:49.369881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.select_dtypes(include=['object']).nunique().sort_values(ascending=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:22:49.436324Z","iopub.execute_input":"2022-07-17T13:22:49.437815Z","iopub.status.idle":"2022-07-17T13:22:49.463894Z","shell.execute_reply.started":"2022-07-17T13:22:49.437743Z","shell.execute_reply":"2022-07-17T13:22:49.462561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.select_dtypes(include=['int64']).columns","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:22:49.526335Z","iopub.execute_input":"2022-07-17T13:22:49.526753Z","iopub.status.idle":"2022-07-17T13:22:49.536800Z","shell.execute_reply.started":"2022-07-17T13:22:49.526719Z","shell.execute_reply":"2022-07-17T13:22:49.535629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.select_dtypes(include=['float64']).columns","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:22:49.686346Z","iopub.execute_input":"2022-07-17T13:22:49.687070Z","iopub.status.idle":"2022-07-17T13:22:49.696166Z","shell.execute_reply.started":"2022-07-17T13:22:49.687025Z","shell.execute_reply":"2022-07-17T13:22:49.695176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.select_dtypes(include=['object']).columns","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:22:49.981891Z","iopub.execute_input":"2022-07-17T13:22:49.982477Z","iopub.status.idle":"2022-07-17T13:22:49.993870Z","shell.execute_reply.started":"2022-07-17T13:22:49.982438Z","shell.execute_reply":"2022-07-17T13:22:49.992633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA <a class=\"anchor\" id=\"sec4\"></a>","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(25, 25))\nfor i, col in enumerate(df.select_dtypes(include=['float64']).columns):\n    ax = plt.subplot(11,4, i+1)\n    sns.histplot(data=df, x=col, ax=ax)\nplt.suptitle('Histogram Plots for all continuous variables')\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:22:50.166418Z","iopub.execute_input":"2022-07-17T13:22:50.166844Z","iopub.status.idle":"2022-07-17T13:22:51.242647Z","shell.execute_reply.started":"2022-07-17T13:22:50.166809Z","shell.execute_reply":"2022-07-17T13:22:51.241162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(25, 25))\nfor i, col in enumerate(df.select_dtypes(include=['int64']).columns):\n    ax = plt.subplot(11,4, i+1)\n    sns.histplot(data=df, x=col, ax=ax)\nplt.suptitle('Histogram Plots for all continuous variables')\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:22:51.244818Z","iopub.execute_input":"2022-07-17T13:22:51.245199Z","iopub.status.idle":"2022-07-17T13:23:01.679430Z","shell.execute_reply.started":"2022-07-17T13:22:51.245165Z","shell.execute_reply":"2022-07-17T13:23:01.677543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(60, 60))\nfor i, col in enumerate(df.iloc[:,1:].select_dtypes(include=['float64']).columns):\n    ax = plt.subplot(11,3, i+1)\n    sns.boxplot(data=df, x=col, ax=ax)\nplt.suptitle('Boxplot for all continuous variables')\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:23:01.681436Z","iopub.execute_input":"2022-07-17T13:23:01.681952Z","iopub.status.idle":"2022-07-17T13:23:02.554387Z","shell.execute_reply.started":"2022-07-17T13:23:01.681906Z","shell.execute_reply":"2022-07-17T13:23:02.553328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(40, 40))\nfor i, col in enumerate(df.iloc[:,1:].select_dtypes(include=['int64']).columns):\n    ax = plt.subplot(15,3, i+1)\n    sns.boxplot(data=df, x=col, ax=ax)\nplt.suptitle('Boxplot for all continuous variables')\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:23:02.556720Z","iopub.execute_input":"2022-07-17T13:23:02.557583Z","iopub.status.idle":"2022-07-17T13:23:08.835158Z","shell.execute_reply.started":"2022-07-17T13:23:02.557548Z","shell.execute_reply":"2022-07-17T13:23:08.833742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, ax=plt.subplots(figsize=(16,10))\nfig=sns.boxplot(x='SaleType',y='SalePrice',data=df)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:23:08.836660Z","iopub.execute_input":"2022-07-17T13:23:08.837398Z","iopub.status.idle":"2022-07-17T13:23:09.203770Z","shell.execute_reply.started":"2022-07-17T13:23:08.837359Z","shell.execute_reply":"2022-07-17T13:23:09.202571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, ax=plt.subplots(figsize=(12,10))\nfig=sns.boxplot(x='OverallQual',y='SalePrice',data=df)\nfig.axis(ymin=0,ymax=800000);\nxt=plt.xticks(rotation=45)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:23:09.206322Z","iopub.execute_input":"2022-07-17T13:23:09.207192Z","iopub.status.idle":"2022-07-17T13:23:09.597645Z","shell.execute_reply.started":"2022-07-17T13:23:09.207140Z","shell.execute_reply":"2022-07-17T13:23:09.596041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set(rc={'figure.figsize':(12,8)})\nsns.distplot(df['SalePrice'],kde=True,bins=25)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:23:09.599468Z","iopub.execute_input":"2022-07-17T13:23:09.600823Z","iopub.status.idle":"2022-07-17T13:23:09.943432Z","shell.execute_reply.started":"2022-07-17T13:23:09.600743Z","shell.execute_reply":"2022-07-17T13:23:09.941960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['SalePrice'].describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:23:09.945310Z","iopub.execute_input":"2022-07-17T13:23:09.945648Z","iopub.status.idle":"2022-07-17T13:23:09.956633Z","shell.execute_reply.started":"2022-07-17T13:23:09.945617Z","shell.execute_reply":"2022-07-17T13:23:09.955464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(50,50))\nsns.heatmap(df.corr(),annot=True)\nplt.title('Correlation Plot')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:23:09.958770Z","iopub.execute_input":"2022-07-17T13:23:09.959215Z","iopub.status.idle":"2022-07-17T13:23:17.414940Z","shell.execute_reply.started":"2022-07-17T13:23:09.959181Z","shell.execute_reply":"2022-07-17T13:23:17.412957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numeric_features=df.select_dtypes(include=['float64','int64'])\nnumeric_features.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:23:17.421283Z","iopub.execute_input":"2022-07-17T13:23:17.423191Z","iopub.status.idle":"2022-07-17T13:23:17.436755Z","shell.execute_reply.started":"2022-07-17T13:23:17.423122Z","shell.execute_reply":"2022-07-17T13:23:17.435238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"correlation=numeric_features.corr()\ncorrelation['SalePrice'].sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:23:17.438758Z","iopub.execute_input":"2022-07-17T13:23:17.440054Z","iopub.status.idle":"2022-07-17T13:23:17.458753Z","shell.execute_reply.started":"2022-07-17T13:23:17.440006Z","shell.execute_reply":"2022-07-17T13:23:17.457360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"k=12\ncols=correlation.nlargest(k,'SalePrice')['SalePrice'].index\ncols\ncm=np.corrcoef(df[cols].values.T)\nf,ax=plt.subplots(figsize=(14,12))\nsns.heatmap(cm,annot=True,xticklabels=cols.values,yticklabels=cols.values)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:23:17.460135Z","iopub.execute_input":"2022-07-17T13:23:17.460478Z","iopub.status.idle":"2022-07-17T13:23:18.333358Z","shell.execute_reply.started":"2022-07-17T13:23:17.460448Z","shell.execute_reply":"2022-07-17T13:23:18.331998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.scatterplot(x='GarageCars',y='SalePrice',data=df)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:23:18.334867Z","iopub.execute_input":"2022-07-17T13:23:18.335182Z","iopub.status.idle":"2022-07-17T13:23:18.653646Z","shell.execute_reply.started":"2022-07-17T13:23:18.335152Z","shell.execute_reply":"2022-07-17T13:23:18.652316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(40, 40))\nfor i, col in enumerate(df.iloc[:,1:].select_dtypes(include=['int64','float64']).columns):\n    ax = plt.subplot(35,2, i+1)\n    sns.boxplot(data=df, x=col,y=col, ax=ax)\nplt.suptitle('Scatterplot for all continuous variables')\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:23:18.655628Z","iopub.execute_input":"2022-07-17T13:23:18.656422Z","iopub.status.idle":"2022-07-17T13:28:29.587770Z","shell.execute_reply.started":"2022-07-17T13:23:18.656371Z","shell.execute_reply":"2022-07-17T13:28:29.585891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#scatter plots between the most correlated variables\nfig, ((ax1,ax2),(ax3,ax4),(ax5,ax6))=plt.subplots(nrows=3,ncols=2,figsize=(14,10))\n\nsns.regplot(x='OverallQual',y='SalePrice',data=df,scatter=True,fit_reg=True,ax=ax1,color='red')\n\nsns.regplot(x='GrLivArea',y='SalePrice',data=df,scatter=True,fit_reg=True,ax=ax2,color='green')\n\nsns.regplot(x='GarageArea',y='SalePrice',data=df,scatter=True,fit_reg=True,ax=ax3,color='yellow')\n\nsns.regplot(x='FullBath',y='SalePrice',data=df,scatter=True,fit_reg=True,ax=ax4,color='violet')\n\nsns.regplot(x='YearBuilt',y='SalePrice',data=df,scatter=True,fit_reg=True,ax=ax5,color='pink')\n\nsns.regplot(x='WoodDeckSF',y='SalePrice',data=df,scatter=True,fit_reg=True,ax=ax6)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:29.589590Z","iopub.execute_input":"2022-07-17T13:28:29.590412Z","iopub.status.idle":"2022-07-17T13:28:31.841775Z","shell.execute_reply.started":"2022-07-17T13:28:29.590362Z","shell.execute_reply":"2022-07-17T13:28:31.840896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Missing Values <a class=\"anchor\" id=\"sec5\"></a>","metadata":{}},{"cell_type":"code","source":"df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:31.843108Z","iopub.execute_input":"2022-07-17T13:28:31.844016Z","iopub.status.idle":"2022-07-17T13:28:31.863518Z","shell.execute_reply.started":"2022-07-17T13:28:31.843979Z","shell.execute_reply":"2022-07-17T13:28:31.862284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.isnull().sum()*100/1460","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:31.864985Z","iopub.execute_input":"2022-07-17T13:28:31.865576Z","iopub.status.idle":"2022-07-17T13:28:31.885742Z","shell.execute_reply.started":"2022-07-17T13:28:31.865527Z","shell.execute_reply":"2022-07-17T13:28:31.884389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing=df.isnull().sum()\nmissing=missing[missing>0]\nmissing.sort_values(inplace=True)\nmissing.plot.bar()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:31.887265Z","iopub.execute_input":"2022-07-17T13:28:31.887592Z","iopub.status.idle":"2022-07-17T13:28:32.268398Z","shell.execute_reply.started":"2022-07-17T13:28:31.887562Z","shell.execute_reply":"2022-07-17T13:28:32.267500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop(['Alley', 'MiscFeature','Fence','PoolQC','FireplaceQu'], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.269627Z","iopub.execute_input":"2022-07-17T13:28:32.270561Z","iopub.status.idle":"2022-07-17T13:28:32.278629Z","shell.execute_reply.started":"2022-07-17T13:28:32.270511Z","shell.execute_reply":"2022-07-17T13:28:32.277419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We have deleated those columns which had more than 45% of missing values.","metadata":{}},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.280633Z","iopub.execute_input":"2022-07-17T13:28:32.281435Z","iopub.status.idle":"2022-07-17T13:28:32.292110Z","shell.execute_reply.started":"2022-07-17T13:28:32.281391Z","shell.execute_reply":"2022-07-17T13:28:32.290744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_t.drop(['Alley', 'MiscFeature','Fence','PoolQC','FireplaceQu'], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.294092Z","iopub.execute_input":"2022-07-17T13:28:32.294930Z","iopub.status.idle":"2022-07-17T13:28:32.304912Z","shell.execute_reply.started":"2022-07-17T13:28:32.294875Z","shell.execute_reply":"2022-07-17T13:28:32.303129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_t.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.307169Z","iopub.execute_input":"2022-07-17T13:28:32.307983Z","iopub.status.idle":"2022-07-17T13:28:32.315694Z","shell.execute_reply.started":"2022-07-17T13:28:32.307932Z","shell.execute_reply":"2022-07-17T13:28:32.314686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Removing Outliers <a class=\"anchor\" id=\"sec6\"></a>","metadata":{}},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.317157Z","iopub.execute_input":"2022-07-17T13:28:32.318237Z","iopub.status.idle":"2022-07-17T13:28:32.327987Z","shell.execute_reply.started":"2022-07-17T13:28:32.318197Z","shell.execute_reply":"2022-07-17T13:28:32.326799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"first_quartile = df['SalePrice'].quantile(.25)\nthird_quartile= df['SalePrice'].quantile(.75)\nIQR= third_quartile - first_quartile","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.329389Z","iopub.execute_input":"2022-07-17T13:28:32.330525Z","iopub.status.idle":"2022-07-17T13:28:32.339558Z","shell.execute_reply.started":"2022-07-17T13:28:32.330489Z","shell.execute_reply":"2022-07-17T13:28:32.338432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_boundary = third_quartile + 3*IQR","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.340950Z","iopub.execute_input":"2022-07-17T13:28:32.342116Z","iopub.status.idle":"2022-07-17T13:28:32.350615Z","shell.execute_reply.started":"2022-07-17T13:28:32.342072Z","shell.execute_reply":"2022-07-17T13:28:32.349191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop(df[df['SalePrice']>new_boundary].index,axis=0,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.353248Z","iopub.execute_input":"2022-07-17T13:28:32.353818Z","iopub.status.idle":"2022-07-17T13:28:32.363471Z","shell.execute_reply.started":"2022-07-17T13:28:32.353728Z","shell.execute_reply":"2022-07-17T13:28:32.362267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.365065Z","iopub.execute_input":"2022-07-17T13:28:32.365455Z","iopub.status.idle":"2022-07-17T13:28:32.374868Z","shell.execute_reply.started":"2022-07-17T13:28:32.365418Z","shell.execute_reply":"2022-07-17T13:28:32.373661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxplot(x=df['SalePrice'])","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.388764Z","iopub.execute_input":"2022-07-17T13:28:32.389333Z","iopub.status.idle":"2022-07-17T13:28:32.602352Z","shell.execute_reply.started":"2022-07-17T13:28:32.389292Z","shell.execute_reply":"2022-07-17T13:28:32.601059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Removing Multicollinearity and also features with poor correlation <a class=\"anchor\" id=\"sec7\"></a>","metadata":{}},{"cell_type":"code","source":"correlation=numeric_features.corr()\nc=correlation['SalePrice']\nc[c<.3]","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.604443Z","iopub.execute_input":"2022-07-17T13:28:32.605509Z","iopub.status.idle":"2022-07-17T13:28:32.626054Z","shell.execute_reply.started":"2022-07-17T13:28:32.605458Z","shell.execute_reply":"2022-07-17T13:28:32.624869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c[c<.3].index","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.628314Z","iopub.execute_input":"2022-07-17T13:28:32.628656Z","iopub.status.idle":"2022-07-17T13:28:32.637396Z","shell.execute_reply.started":"2022-07-17T13:28:32.628624Z","shell.execute_reply":"2022-07-17T13:28:32.635864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# with the following function we can select highly correlated features\n# it will remove the first feature that is correlated with anything other feature\n\ndef correlation(dataset,threshold): \n    col_corr=set() # Set of all the names of correlated columns\n    corr_matrix = dataset.corr()\n    for i in range(len(corr_matrix.columns)):\n        for j in range(i):\n            if corr_matrix.iloc[i,j] > threshold: #we are interested in absolute coeff values\n                colname= corr_matrix.columns[i] # getting the name of column\n                col_corr.add(colname)\n    return col_corr","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.639149Z","iopub.execute_input":"2022-07-17T13:28:32.639567Z","iopub.status.idle":"2022-07-17T13:28:32.648829Z","shell.execute_reply.started":"2022-07-17T13:28:32.639532Z","shell.execute_reply":"2022-07-17T13:28:32.647211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr_features = correlation(df,0.7)\nlen(set(corr_features))","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.651152Z","iopub.execute_input":"2022-07-17T13:28:32.651650Z","iopub.status.idle":"2022-07-17T13:28:32.715061Z","shell.execute_reply.started":"2022-07-17T13:28:32.651602Z","shell.execute_reply":"2022-07-17T13:28:32.713866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr_features","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.716617Z","iopub.execute_input":"2022-07-17T13:28:32.716985Z","iopub.status.idle":"2022-07-17T13:28:32.724296Z","shell.execute_reply.started":"2022-07-17T13:28:32.716950Z","shell.execute_reply":"2022-07-17T13:28:32.723005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols_to_remove=['Id', 'MSSubClass', 'LotArea', 'OverallCond', 'BsmtFinSF2', 'BsmtUnfSF',\n       'LowQualFinSF', 'BsmtFullBath', 'BsmtHalfBath', 'HalfBath',\n       'BedroomAbvGr', 'KitchenAbvGr', 'EnclosedPorch', '3SsnPorch',\n       'ScreenPorch', 'PoolArea', 'MiscVal', 'MoSold', 'YrSold','1stFlrSF', 'GarageArea', 'GarageYrBlt', 'TotRmsAbvGrd']","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.726117Z","iopub.execute_input":"2022-07-17T13:28:32.726911Z","iopub.status.idle":"2022-07-17T13:28:32.734316Z","shell.execute_reply.started":"2022-07-17T13:28:32.726864Z","shell.execute_reply":"2022-07-17T13:28:32.733016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop(cols_to_remove,axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.736181Z","iopub.execute_input":"2022-07-17T13:28:32.736625Z","iopub.status.idle":"2022-07-17T13:28:32.745882Z","shell.execute_reply.started":"2022-07-17T13:28:32.736579Z","shell.execute_reply":"2022-07-17T13:28:32.744749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_t.drop(cols_to_remove,axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.748062Z","iopub.execute_input":"2022-07-17T13:28:32.749121Z","iopub.status.idle":"2022-07-17T13:28:32.757557Z","shell.execute_reply.started":"2022-07-17T13:28:32.749077Z","shell.execute_reply":"2022-07-17T13:28:32.755942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.759510Z","iopub.execute_input":"2022-07-17T13:28:32.760689Z","iopub.status.idle":"2022-07-17T13:28:32.806548Z","shell.execute_reply.started":"2022-07-17T13:28:32.760647Z","shell.execute_reply":"2022-07-17T13:28:32.805669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.808297Z","iopub.execute_input":"2022-07-17T13:28:32.809064Z","iopub.status.idle":"2022-07-17T13:28:32.815859Z","shell.execute_reply.started":"2022-07-17T13:28:32.809028Z","shell.execute_reply":"2022-07-17T13:28:32.814841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_t.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.817429Z","iopub.execute_input":"2022-07-17T13:28:32.818649Z","iopub.status.idle":"2022-07-17T13:28:32.826421Z","shell.execute_reply.started":"2022-07-17T13:28:32.818585Z","shell.execute_reply":"2022-07-17T13:28:32.825242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.827979Z","iopub.execute_input":"2022-07-17T13:28:32.828991Z","iopub.status.idle":"2022-07-17T13:28:32.854605Z","shell.execute_reply.started":"2022-07-17T13:28:32.828955Z","shell.execute_reply":"2022-07-17T13:28:32.853187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Standard Scaler and Imputation KMeans <a class=\"anchor\" id=\"sec8\"></a>","metadata":{}},{"cell_type":"code","source":"#\n# Scale the dataset; \n#\nfrom sklearn.preprocessing import StandardScaler\nsc = StandardScaler()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.856357Z","iopub.execute_input":"2022-07-17T13:28:32.857491Z","iopub.status.idle":"2022-07-17T13:28:32.863444Z","shell.execute_reply.started":"2022-07-17T13:28:32.857438Z","shell.execute_reply":"2022-07-17T13:28:32.862156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1=df.select_dtypes(include=['float64','int64'])","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.865406Z","iopub.execute_input":"2022-07-17T13:28:32.866228Z","iopub.status.idle":"2022-07-17T13:28:32.878106Z","shell.execute_reply.started":"2022-07-17T13:28:32.866181Z","shell.execute_reply":"2022-07-17T13:28:32.876886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.879945Z","iopub.execute_input":"2022-07-17T13:28:32.880738Z","iopub.status.idle":"2022-07-17T13:28:32.903420Z","shell.execute_reply.started":"2022-07-17T13:28:32.880688Z","shell.execute_reply":"2022-07-17T13:28:32.902242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.905268Z","iopub.execute_input":"2022-07-17T13:28:32.905604Z","iopub.status.idle":"2022-07-17T13:28:32.916823Z","shell.execute_reply.started":"2022-07-17T13:28:32.905574Z","shell.execute_reply":"2022-07-17T13:28:32.915860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sc.fit(df1)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.918651Z","iopub.execute_input":"2022-07-17T13:28:32.919584Z","iopub.status.idle":"2022-07-17T13:28:32.929712Z","shell.execute_reply.started":"2022-07-17T13:28:32.919545Z","shell.execute_reply":"2022-07-17T13:28:32.928538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_std = sc.transform(df1)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.932331Z","iopub.execute_input":"2022-07-17T13:28:32.933186Z","iopub.status.idle":"2022-07-17T13:28:32.941943Z","shell.execute_reply.started":"2022-07-17T13:28:32.933140Z","shell.execute_reply":"2022-07-17T13:28:32.940584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.cluster import KMeans","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.943632Z","iopub.execute_input":"2022-07-17T13:28:32.944811Z","iopub.status.idle":"2022-07-17T13:28:32.952133Z","shell.execute_reply.started":"2022-07-17T13:28:32.944717Z","shell.execute_reply":"2022-07-17T13:28:32.950948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.experimental import enable_iterative_imputer ","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.953665Z","iopub.execute_input":"2022-07-17T13:28:32.954883Z","iopub.status.idle":"2022-07-17T13:28:32.963100Z","shell.execute_reply.started":"2022-07-17T13:28:32.954845Z","shell.execute_reply":"2022-07-17T13:28:32.962112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.impute import IterativeImputer","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.964711Z","iopub.execute_input":"2022-07-17T13:28:32.965858Z","iopub.status.idle":"2022-07-17T13:28:32.973999Z","shell.execute_reply.started":"2022-07-17T13:28:32.965695Z","shell.execute_reply":"2022-07-17T13:28:32.972811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imp=IterativeImputer(estimator=KMeans(),missing_values=np.nan)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.975836Z","iopub.execute_input":"2022-07-17T13:28:32.976552Z","iopub.status.idle":"2022-07-17T13:28:32.985920Z","shell.execute_reply.started":"2022-07-17T13:28:32.976506Z","shell.execute_reply":"2022-07-17T13:28:32.984380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imp.fit_transform(df1)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:32.988085Z","iopub.execute_input":"2022-07-17T13:28:32.988867Z","iopub.status.idle":"2022-07-17T13:28:54.548551Z","shell.execute_reply.started":"2022-07-17T13:28:32.988819Z","shell.execute_reply":"2022-07-17T13:28:54.547572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.select_dtypes(include=['float64','int64']).isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:54.550427Z","iopub.execute_input":"2022-07-17T13:28:54.551065Z","iopub.status.idle":"2022-07-17T13:28:54.564176Z","shell.execute_reply.started":"2022-07-17T13:28:54.551019Z","shell.execute_reply":"2022-07-17T13:28:54.562967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Filling Missing Values <a class=\"anchor\" id=\"sec9\"></a>","metadata":{}},{"cell_type":"code","source":"numcols=df.select_dtypes(include=['float64','int64']).columns\nnumcols","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:54.566260Z","iopub.execute_input":"2022-07-17T13:28:54.567264Z","iopub.status.idle":"2022-07-17T13:28:54.579278Z","shell.execute_reply.started":"2022-07-17T13:28:54.567226Z","shell.execute_reply":"2022-07-17T13:28:54.577883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numcols_t=df_t.select_dtypes(include=['float64','int64']).columns\nnumcols_t","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:54.580831Z","iopub.execute_input":"2022-07-17T13:28:54.582187Z","iopub.status.idle":"2022-07-17T13:28:54.597008Z","shell.execute_reply.started":"2022-07-17T13:28:54.582131Z","shell.execute_reply":"2022-07-17T13:28:54.595854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"catcols=df.select_dtypes(include=['object']).columns\ncatcols","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:54.599763Z","iopub.execute_input":"2022-07-17T13:28:54.600919Z","iopub.status.idle":"2022-07-17T13:28:54.613656Z","shell.execute_reply.started":"2022-07-17T13:28:54.600878Z","shell.execute_reply":"2022-07-17T13:28:54.612156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"catcols_t=df_t.select_dtypes(include=['object']).columns\ncatcols_t","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:54.615482Z","iopub.execute_input":"2022-07-17T13:28:54.616718Z","iopub.status.idle":"2022-07-17T13:28:54.630063Z","shell.execute_reply.started":"2022-07-17T13:28:54.616665Z","shell.execute_reply":"2022-07-17T13:28:54.628838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in numcols:\n    df[i]=df[i].fillna(df[i].median())","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:54.632286Z","iopub.execute_input":"2022-07-17T13:28:54.633122Z","iopub.status.idle":"2022-07-17T13:28:54.649045Z","shell.execute_reply.started":"2022-07-17T13:28:54.633085Z","shell.execute_reply":"2022-07-17T13:28:54.648120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in numcols_t:\n    df_t[i]=df_t[i].fillna(df_t[i].median())","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:54.650487Z","iopub.execute_input":"2022-07-17T13:28:54.651146Z","iopub.status.idle":"2022-07-17T13:28:54.665483Z","shell.execute_reply.started":"2022-07-17T13:28:54.651104Z","shell.execute_reply":"2022-07-17T13:28:54.663735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in catcols:\n    df[i]=df[i].fillna(df[i].mode()[0])","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:54.667439Z","iopub.execute_input":"2022-07-17T13:28:54.667928Z","iopub.status.idle":"2022-07-17T13:28:54.715847Z","shell.execute_reply.started":"2022-07-17T13:28:54.667882Z","shell.execute_reply":"2022-07-17T13:28:54.714757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in catcols_t:\n    df_t[i]=df_t[i].fillna(df_t[i].mode()[0])","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:54.717149Z","iopub.execute_input":"2022-07-17T13:28:54.718189Z","iopub.status.idle":"2022-07-17T13:28:54.767724Z","shell.execute_reply.started":"2022-07-17T13:28:54.718149Z","shell.execute_reply":"2022-07-17T13:28:54.766067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:54.769631Z","iopub.execute_input":"2022-07-17T13:28:54.770458Z","iopub.status.idle":"2022-07-17T13:28:54.787727Z","shell.execute_reply.started":"2022-07-17T13:28:54.770419Z","shell.execute_reply":"2022-07-17T13:28:54.786847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['GarageCond']=df['GarageCond'].fillna(df['GarageCond'].mode()[0])","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:54.789136Z","iopub.execute_input":"2022-07-17T13:28:54.789479Z","iopub.status.idle":"2022-07-17T13:28:54.801654Z","shell.execute_reply.started":"2022-07-17T13:28:54.789448Z","shell.execute_reply":"2022-07-17T13:28:54.799904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:54.803287Z","iopub.execute_input":"2022-07-17T13:28:54.803691Z","iopub.status.idle":"2022-07-17T13:28:54.819233Z","shell.execute_reply.started":"2022-07-17T13:28:54.803655Z","shell.execute_reply":"2022-07-17T13:28:54.817175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_t.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:54.822087Z","iopub.execute_input":"2022-07-17T13:28:54.823490Z","iopub.status.idle":"2022-07-17T13:28:54.831581Z","shell.execute_reply.started":"2022-07-17T13:28:54.823435Z","shell.execute_reply":"2022-07-17T13:28:54.830269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Join <a class=\"anchor\" id=\"sec10\"></a>","metadata":{}},{"cell_type":"code","source":"df_main=pd.concat([df,df_t],axis=0)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:54.833748Z","iopub.execute_input":"2022-07-17T13:28:54.834819Z","iopub.status.idle":"2022-07-17T13:28:54.851922Z","shell.execute_reply.started":"2022-07-17T13:28:54.834757Z","shell.execute_reply":"2022-07-17T13:28:54.850884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_main.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:54.853522Z","iopub.execute_input":"2022-07-17T13:28:54.854030Z","iopub.status.idle":"2022-07-17T13:28:54.912632Z","shell.execute_reply.started":"2022-07-17T13:28:54.853994Z","shell.execute_reply":"2022-07-17T13:28:54.911096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_main.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:54.914543Z","iopub.execute_input":"2022-07-17T13:28:54.915175Z","iopub.status.idle":"2022-07-17T13:28:54.925031Z","shell.execute_reply.started":"2022-07-17T13:28:54.915097Z","shell.execute_reply":"2022-07-17T13:28:54.923656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# One hot encoding <a class=\"anchor\" id=\"sec11\"></a>","metadata":{}},{"cell_type":"code","source":"df_main = pd.get_dummies(df_main)\ndf_main.head()\n","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:28:54.926887Z","iopub.execute_input":"2022-07-17T13:28:54.928303Z","iopub.status.idle":"2022-07-17T13:28:55.138358Z","shell.execute_reply.started":"2022-07-17T13:28:54.928150Z","shell.execute_reply":"2022-07-17T13:28:55.136074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=df_main.iloc[:1448,:]\ntest=df_main.iloc[1448:,:]","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:36:50.247890Z","iopub.execute_input":"2022-07-17T13:36:50.248685Z","iopub.status.idle":"2022-07-17T13:36:50.256383Z","shell.execute_reply.started":"2022-07-17T13:36:50.248646Z","shell.execute_reply":"2022-07-17T13:36:50.254872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:36:50.517024Z","iopub.execute_input":"2022-07-17T13:36:50.517852Z","iopub.status.idle":"2022-07-17T13:36:50.618146Z","shell.execute_reply.started":"2022-07-17T13:36:50.517805Z","shell.execute_reply":"2022-07-17T13:36:50.617230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:36:50.652940Z","iopub.execute_input":"2022-07-17T13:36:50.654317Z","iopub.status.idle":"2022-07-17T13:36:50.662772Z","shell.execute_reply.started":"2022-07-17T13:36:50.654258Z","shell.execute_reply":"2022-07-17T13:36:50.661380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:36:50.967020Z","iopub.execute_input":"2022-07-17T13:36:50.968970Z","iopub.status.idle":"2022-07-17T13:36:50.978076Z","shell.execute_reply.started":"2022-07-17T13:36:50.968905Z","shell.execute_reply":"2022-07-17T13:36:50.976771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:36:51.216566Z","iopub.execute_input":"2022-07-17T13:36:51.216997Z","iopub.status.idle":"2022-07-17T13:36:51.339112Z","shell.execute_reply.started":"2022-07-17T13:36:51.216960Z","shell.execute_reply":"2022-07-17T13:36:51.337031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train-Test Split <a class=\"anchor\" id=\"sec12\"></a>","metadata":{}},{"cell_type":"code","source":"x = train.drop(\"SalePrice\",axis=1)\ny = train[[\"SalePrice\"]]\n\nfrom sklearn.model_selection import train_test_split\nx_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.20,random_state=49)\nprint(\"x_train :\",x_train.shape)\nprint(\"x_test :\",x_test.shape)\nprint(\"y_train :\",y_train.shape)\nprint(\"y_test :\",y_test.shape)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:36:51.426757Z","iopub.execute_input":"2022-07-17T13:36:51.427150Z","iopub.status.idle":"2022-07-17T13:36:51.442447Z","shell.execute_reply.started":"2022-07-17T13:36:51.427120Z","shell.execute_reply":"2022-07-17T13:36:51.441277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prediction <a class=\"anchor\" id=\"sec13\"></a>","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor\nR_model = RandomForestRegressor()\nR_model.fit(x_train,y_train)\npreds = R_model.predict(x_test)\n\npred_data = list(x_test.iloc[1])\nR_model.predict(np.array(pred_data).reshape(1, -1))\n\nfrom sklearn.metrics import r2_score\nr = r2_score(y_test,preds)\nprint(\"R2score when we predict using Randomn forest is \",r)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:36:51.561757Z","iopub.execute_input":"2022-07-17T13:36:51.562822Z","iopub.status.idle":"2022-07-17T13:36:53.600004Z","shell.execute_reply.started":"2022-07-17T13:36:51.562749Z","shell.execute_reply":"2022-07-17T13:36:53.598801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import cross_val_score\n\naccuracies=cross_val_score(R_model,x_train,y_train,cv=10)\naccuracies.mean()*100\n","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:36:53.602142Z","iopub.execute_input":"2022-07-17T13:36:53.603592Z","iopub.status.idle":"2022-07-17T13:37:11.625605Z","shell.execute_reply.started":"2022-07-17T13:36:53.603551Z","shell.execute_reply":"2022-07-17T13:37:11.624002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.scatter(y_test,preds,color=\"green\")\nplt.xlabel(\"Actual Value\")\nplt.ylabel(\"Predicted Value\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:37:11.627777Z","iopub.execute_input":"2022-07-17T13:37:11.628257Z","iopub.status.idle":"2022-07-17T13:37:11.941223Z","shell.execute_reply.started":"2022-07-17T13:37:11.628218Z","shell.execute_reply":"2022-07-17T13:37:11.940156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"R_model.feature_importances_","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:37:11.943762Z","iopub.execute_input":"2022-07-17T13:37:11.945163Z","iopub.status.idle":"2022-07-17T13:37:11.970080Z","shell.execute_reply.started":"2022-07-17T13:37:11.945106Z","shell.execute_reply":"2022-07-17T13:37:11.968944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"R_model.estimators_","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:37:11.971383Z","iopub.execute_input":"2022-07-17T13:37:11.971790Z","iopub.status.idle":"2022-07-17T13:37:12.006910Z","shell.execute_reply.started":"2022-07-17T13:37:11.971748Z","shell.execute_reply":"2022-07-17T13:37:12.005978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=test.drop(\"SalePrice\",axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:37:12.008601Z","iopub.execute_input":"2022-07-17T13:37:12.009087Z","iopub.status.idle":"2022-07-17T13:37:12.018245Z","shell.execute_reply.started":"2022-07-17T13:37:12.009046Z","shell.execute_reply":"2022-07-17T13:37:12.017224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds1=R_model.predict(x)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:37:12.019587Z","iopub.execute_input":"2022-07-17T13:37:12.020234Z","iopub.status.idle":"2022-07-17T13:37:12.076505Z","shell.execute_reply.started":"2022-07-17T13:37:12.020201Z","shell.execute_reply":"2022-07-17T13:37:12.075483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds1","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:37:12.077831Z","iopub.execute_input":"2022-07-17T13:37:12.078380Z","iopub.status.idle":"2022-07-17T13:37:12.085012Z","shell.execute_reply.started":"2022-07-17T13:37:12.078347Z","shell.execute_reply":"2022-07-17T13:37:12.083866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(preds1)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:37:12.086413Z","iopub.execute_input":"2022-07-17T13:37:12.087360Z","iopub.status.idle":"2022-07-17T13:37:12.095095Z","shell.execute_reply.started":"2022-07-17T13:37:12.087226Z","shell.execute_reply":"2022-07-17T13:37:12.094089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub=pd.read_csv(\"../input/house-prices-advanced-regression-techniques/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:37:12.098070Z","iopub.execute_input":"2022-07-17T13:37:12.098959Z","iopub.status.idle":"2022-07-17T13:37:12.116177Z","shell.execute_reply.started":"2022-07-17T13:37:12.098918Z","shell.execute_reply":"2022-07-17T13:37:12.114803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:37:12.117851Z","iopub.execute_input":"2022-07-17T13:37:12.118171Z","iopub.status.idle":"2022-07-17T13:37:12.129243Z","shell.execute_reply.started":"2022-07-17T13:37:12.118141Z","shell.execute_reply":"2022-07-17T13:37:12.128180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:37:12.131669Z","iopub.execute_input":"2022-07-17T13:37:12.132803Z","iopub.status.idle":"2022-07-17T13:37:12.140303Z","shell.execute_reply.started":"2022-07-17T13:37:12.132735Z","shell.execute_reply":"2022-07-17T13:37:12.139409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub['SalePrice']=preds1","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:37:12.141773Z","iopub.execute_input":"2022-07-17T13:37:12.142508Z","iopub.status.idle":"2022-07-17T13:37:12.150429Z","shell.execute_reply.started":"2022-07-17T13:37:12.142471Z","shell.execute_reply":"2022-07-17T13:37:12.149174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub.to_csv('House_kagg.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T13:39:03.357173Z","iopub.execute_input":"2022-07-17T13:39:03.357623Z","iopub.status.idle":"2022-07-17T13:39:03.372135Z","shell.execute_reply.started":"2022-07-17T13:39:03.357581Z","shell.execute_reply":"2022-07-17T13:39:03.371251Z"},"trusted":true},"execution_count":null,"outputs":[]}]}