{"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":"Problem statement","metadata":{}},{"cell_type":"markdown","source":"Ask a home buyer to describe their dream house, and they probably won't begin with the height of the basement ceiling or the proximity to an east-west railroad. But this playground competition's dataset proves that much more influences price negotiations than the number of bedrooms or a white-picket fence.\n\nWith 79 explanatory variables describing (almost) every aspect of residential homes in Ames, Iowa, this competition challenges you to predict the final price of each home.\n\nAcknowledgments\n\nThe Ames Housing dataset was compiled by Dean De Cock for use in data science education. It's an incredible alternative for data scientists looking for a modernized and expanded version of the often cited Boston Housing dataset.","metadata":{}},{"cell_type":"markdown","source":"Import libraries","metadata":{}},{"cell_type":"code","source":"\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-08T15:36:04.893258Z","iopub.execute_input":"2022-08-08T15:36:04.893965Z","iopub.status.idle":"2022-08-08T15:36:05.929583Z","shell.execute_reply.started":"2022-08-08T15:36:04.893918Z","shell.execute_reply":"2022-08-08T15:36:05.928412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Load and read csv files","metadata":{}},{"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2022-08-08T15:36:05.931954Z","iopub.execute_input":"2022-08-08T15:36:05.932446Z","iopub.status.idle":"2022-08-08T15:36:05.941941Z","shell.execute_reply.started":"2022-08-08T15:36:05.932400Z","shell.execute_reply":"2022-08-08T15:36:05.940724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Load datasets\ntrain=pd.read_csv(\"/kaggle/input/house-prices-advanced-regression-techniques/train.csv\")\npd.set_option('display.max_columns', None)\ntrain","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:05.943611Z","iopub.execute_input":"2022-08-08T15:36:05.944014Z","iopub.status.idle":"2022-08-08T15:36:06.095351Z","shell.execute_reply.started":"2022-08-08T15:36:05.943953Z","shell.execute_reply":"2022-08-08T15:36:06.094406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#Load datasets\ntest=pd.read_csv(\"/kaggle/input/house-prices-advanced-regression-techniques/test.csv\")\ntest\n","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:06.096834Z","iopub.execute_input":"2022-08-08T15:36:06.097183Z","iopub.status.idle":"2022-08-08T15:36:06.225806Z","shell.execute_reply.started":"2022-08-08T15:36:06.097149Z","shell.execute_reply":"2022-08-08T15:36:06.224677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv(\"/kaggle/input/house-prices-advanced-regression-techniques/sample_submission.csv\")\nsubmission","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:06.229459Z","iopub.execute_input":"2022-08-08T15:36:06.229776Z","iopub.status.idle":"2022-08-08T15:36:06.250853Z","shell.execute_reply.started":"2022-08-08T15:36:06.229744Z","shell.execute_reply":"2022-08-08T15:36:06.249720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Analyse SalePrice","metadata":{}},{"cell_type":"code","source":"target = train['SalePrice']\nsns.distplot(train['SalePrice']);\n","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:06.253063Z","iopub.execute_input":"2022-08-08T15:36:06.253379Z","iopub.status.idle":"2022-08-08T15:36:06.544334Z","shell.execute_reply.started":"2022-08-08T15:36:06.253346Z","shell.execute_reply":"2022-08-08T15:36:06.543258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:06.545941Z","iopub.execute_input":"2022-08-08T15:36:06.546285Z","iopub.status.idle":"2022-08-08T15:36:06.558006Z","shell.execute_reply.started":"2022-08-08T15:36:06.546251Z","shell.execute_reply":"2022-08-08T15:36:06.557064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"var = 'OverallQual'\ndata = pd.concat([train['SalePrice'], train[var]], axis=1)\nf, ax = plt.subplots(figsize=(14, 8))\nfig = sns.boxplot(x=var, y=\"SalePrice\", data=data)\nfig.axis(ymin=0, ymax=800000);","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:06.559465Z","iopub.execute_input":"2022-08-08T15:36:06.559793Z","iopub.status.idle":"2022-08-08T15:36:06.851919Z","shell.execute_reply.started":"2022-08-08T15:36:06.559762Z","shell.execute_reply":"2022-08-08T15:36:06.851076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"var = 'OverallCond'\ndata = pd.concat([train['SalePrice'], train[var]], axis=1)\nf, ax = plt.subplots(figsize=(14, 8))\nfig = sns.boxplot(x=var, y=\"SalePrice\", data=data)\nfig.axis(ymin=0, ymax=800000);","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:06.853041Z","iopub.execute_input":"2022-08-08T15:36:06.853480Z","iopub.status.idle":"2022-08-08T15:36:07.130817Z","shell.execute_reply.started":"2022-08-08T15:36:06.853446Z","shell.execute_reply":"2022-08-08T15:36:07.129748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Check for null values on train","metadata":{}},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:07.132340Z","iopub.execute_input":"2022-08-08T15:36:07.132688Z","iopub.status.idle":"2022-08-08T15:36:07.145507Z","shell.execute_reply.started":"2022-08-08T15:36:07.132646Z","shell.execute_reply":"2022-08-08T15:36:07.144608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Remove columns that have null values","metadata":{}},{"cell_type":"code","source":"train.columns[train.isnull().any()]","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:07.146768Z","iopub.execute_input":"2022-08-08T15:36:07.147123Z","iopub.status.idle":"2022-08-08T15:36:07.160081Z","shell.execute_reply.started":"2022-08-08T15:36:07.147080Z","shell.execute_reply":"2022-08-08T15:36:07.159000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Drop columns that have missing values","metadata":{}},{"cell_type":"code","source":"train.drop(['LotFrontage', 'Alley', 'MasVnrType', 'MasVnrArea', 'BsmtQual',\n       'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2',\n       'Electrical', 'FireplaceQu', 'GarageType', 'GarageYrBlt',\n       'GarageFinish', 'GarageQual', 'GarageCond', 'PoolQC', 'Fence',\n       'MiscFeature'], axis=1, inplace=True)\n\ntest.drop(['LotFrontage', 'Alley', 'MasVnrType', 'MasVnrArea', 'BsmtQual',\n       'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2',\n       'Electrical', 'FireplaceQu', 'GarageType', 'GarageYrBlt',\n       'GarageFinish', 'GarageQual', 'GarageCond', 'PoolQC', 'Fence',\n       'MiscFeature'], axis=1, inplace=True)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:07.161538Z","iopub.execute_input":"2022-08-08T15:36:07.161931Z","iopub.status.idle":"2022-08-08T15:36:07.174056Z","shell.execute_reply.started":"2022-08-08T15:36:07.161900Z","shell.execute_reply":"2022-08-08T15:36:07.173176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:07.175347Z","iopub.execute_input":"2022-08-08T15:36:07.175836Z","iopub.status.idle":"2022-08-08T15:36:07.332605Z","shell.execute_reply.started":"2022-08-08T15:36:07.175802Z","shell.execute_reply":"2022-08-08T15:36:07.331540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:07.333902Z","iopub.execute_input":"2022-08-08T15:36:07.334287Z","iopub.status.idle":"2022-08-08T15:36:07.412059Z","shell.execute_reply.started":"2022-08-08T15:36:07.334211Z","shell.execute_reply":"2022-08-08T15:36:07.411280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Drop rows in train that have missing values","metadata":{}},{"cell_type":"code","source":"train = train.dropna()\ntrain","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:07.413113Z","iopub.execute_input":"2022-08-08T15:36:07.413535Z","iopub.status.idle":"2022-08-08T15:36:07.505708Z","shell.execute_reply.started":"2022-08-08T15:36:07.413503Z","shell.execute_reply":"2022-08-08T15:36:07.504742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Locate and delete outliers from train file","metadata":{}},{"cell_type":"code","source":"for x in ['SalePrice']:\n    q75,q25 = np.percentile(train.loc[:,x],[75,25])\n    intr_qr = q75-q25\n \n    max = q75+(2*intr_qr)\n    min = q25-(2*intr_qr)\n \n    train.loc[train[x] < min,x] = np.nan\n    train.loc[train[x] > max,x] = np.nan","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:07.506938Z","iopub.execute_input":"2022-08-08T15:36:07.507427Z","iopub.status.idle":"2022-08-08T15:36:07.518110Z","shell.execute_reply.started":"2022-08-08T15:36:07.507391Z","shell.execute_reply":"2022-08-08T15:36:07.516878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['SalePrice'].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:07.520160Z","iopub.execute_input":"2022-08-08T15:36:07.520696Z","iopub.status.idle":"2022-08-08T15:36:07.534152Z","shell.execute_reply.started":"2022-08-08T15:36:07.520627Z","shell.execute_reply":"2022-08-08T15:36:07.533125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.dropna(axis = 0)\ntrain","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:07.535456Z","iopub.execute_input":"2022-08-08T15:36:07.535917Z","iopub.status.idle":"2022-08-08T15:36:07.615511Z","shell.execute_reply.started":"2022-08-08T15:36:07.535883Z","shell.execute_reply":"2022-08-08T15:36:07.614476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Analyse SalePrice","metadata":{}},{"cell_type":"code","source":"sns.distplot(train['SalePrice']);","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:07.616758Z","iopub.execute_input":"2022-08-08T15:36:07.617368Z","iopub.status.idle":"2022-08-08T15:36:07.802169Z","shell.execute_reply.started":"2022-08-08T15:36:07.617199Z","shell.execute_reply":"2022-08-08T15:36:07.801189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.scatter(train['LotArea'], train['SalePrice'], alpha=0.5, cmap='nipy_spectral')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:07.803401Z","iopub.execute_input":"2022-08-08T15:36:07.803878Z","iopub.status.idle":"2022-08-08T15:36:07.976424Z","shell.execute_reply.started":"2022-08-08T15:36:07.803833Z","shell.execute_reply":"2022-08-08T15:36:07.975104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.scatter(train['YearBuilt'], train['SalePrice'], alpha=0.5, cmap='nipy_spectral')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:07.978076Z","iopub.execute_input":"2022-08-08T15:36:07.978600Z","iopub.status.idle":"2022-08-08T15:36:08.139626Z","shell.execute_reply.started":"2022-08-08T15:36:07.978555Z","shell.execute_reply":"2022-08-08T15:36:08.138569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Append train and test","metadata":{}},{"cell_type":"code","source":"train_copy = train\ncombi = train_copy.drop('SalePrice', axis =1)\ncombi = combi.append(test)\ncombi","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:08.144740Z","iopub.execute_input":"2022-08-08T15:36:08.145134Z","iopub.status.idle":"2022-08-08T15:36:08.232751Z","shell.execute_reply.started":"2022-08-08T15:36:08.145097Z","shell.execute_reply":"2022-08-08T15:36:08.231922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Drop Id","metadata":{}},{"cell_type":"code","source":"combi.drop(['Id'], axis=1, inplace=True)\ncombi","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:08.234103Z","iopub.execute_input":"2022-08-08T15:36:08.234534Z","iopub.status.idle":"2022-08-08T15:36:08.315421Z","shell.execute_reply.started":"2022-08-08T15:36:08.234502Z","shell.execute_reply":"2022-08-08T15:36:08.314555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Check null values","metadata":{}},{"cell_type":"code","source":"combi.columns[combi.isnull().any()]","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:08.316513Z","iopub.execute_input":"2022-08-08T15:36:08.316927Z","iopub.status.idle":"2022-08-08T15:36:08.328031Z","shell.execute_reply.started":"2022-08-08T15:36:08.316896Z","shell.execute_reply":"2022-08-08T15:36:08.327060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combi.drop(['MSZoning', 'Utilities', 'Exterior1st', 'Exterior2nd', 'BsmtFinSF1',\n       'BsmtFinSF2', 'BsmtUnfSF', 'TotalBsmtSF', 'BsmtFullBath',\n       'BsmtHalfBath', 'KitchenQual', 'Functional', 'GarageCars', 'GarageArea',\n       'SaleType'], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:08.329371Z","iopub.execute_input":"2022-08-08T15:36:08.329667Z","iopub.status.idle":"2022-08-08T15:36:08.341126Z","shell.execute_reply.started":"2022-08-08T15:36:08.329630Z","shell.execute_reply":"2022-08-08T15:36:08.339853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combi.isnull().sum().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:08.342878Z","iopub.execute_input":"2022-08-08T15:36:08.343343Z","iopub.status.idle":"2022-08-08T15:36:08.357184Z","shell.execute_reply.started":"2022-08-08T15:36:08.343304Z","shell.execute_reply":"2022-08-08T15:36:08.356304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Encode","metadata":{}},{"cell_type":"code","source":"from sklearn import preprocessing\nfrom sklearn.preprocessing import OrdinalEncoder\n\nenc = OrdinalEncoder()\n\nfor col in combi:\n    if combi[col].dtype==\"object\":\n        combi[col] = enc.fit_transform(combi[col].values.reshape(-1,1))\ncombi","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:08.358408Z","iopub.execute_input":"2022-08-08T15:36:08.358838Z","iopub.status.idle":"2022-08-08T15:36:08.513145Z","shell.execute_reply.started":"2022-08-08T15:36:08.358797Z","shell.execute_reply":"2022-08-08T15:36:08.512301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Heatmap","metadata":{}},{"cell_type":"code","source":"corr = combi.corr()\nf, ax = plt.subplots(figsize=(12, 9))\nsns.heatmap(corr, vmax=.8, square=True);","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:08.514474Z","iopub.execute_input":"2022-08-08T15:36:08.514778Z","iopub.status.idle":"2022-08-08T15:36:09.417742Z","shell.execute_reply.started":"2022-08-08T15:36:08.514746Z","shell.execute_reply":"2022-08-08T15:36:09.416760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:09.419035Z","iopub.execute_input":"2022-08-08T15:36:09.419477Z","iopub.status.idle":"2022-08-08T15:36:09.512857Z","shell.execute_reply.started":"2022-08-08T15:36:09.419444Z","shell.execute_reply":"2022-08-08T15:36:09.512071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Remove columns that have a high correlation","metadata":{}},{"cell_type":"code","source":"columns = np.full((corr.shape[0],), True, dtype=bool)\nfor i in range(corr.shape[0]):\n    for j in range(i+1, corr.shape[0]):\n        if corr.iloc[i,j] >= 0.9:\n            if columns[j]:\n                columns[j] = False\nselected_columns = combi.columns[columns]\ncombi = combi[selected_columns]\ncombi","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:09.513958Z","iopub.execute_input":"2022-08-08T15:36:09.514395Z","iopub.status.idle":"2022-08-08T15:36:09.595685Z","shell.execute_reply.started":"2022-08-08T15:36:09.514364Z","shell.execute_reply":"2022-08-08T15:36:09.594602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Standardise","metadata":{}},{"cell_type":"code","source":"#combi = (combi - np.average(combi)) / (np.std(combi))\n#combi","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:09.597328Z","iopub.execute_input":"2022-08-08T15:36:09.597792Z","iopub.status.idle":"2022-08-08T15:36:09.602306Z","shell.execute_reply.started":"2022-08-08T15:36:09.597745Z","shell.execute_reply":"2022-08-08T15:36:09.601423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Normalise","metadata":{}},{"cell_type":"code","source":"combi = (combi - combi.min()) / (combi.max() - combi.min())\ncombi","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:09.603466Z","iopub.execute_input":"2022-08-08T15:36:09.603928Z","iopub.status.idle":"2022-08-08T15:36:09.700003Z","shell.execute_reply.started":"2022-08-08T15:36:09.603886Z","shell.execute_reply":"2022-08-08T15:36:09.698963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Define X and y","metadata":{}},{"cell_type":"code","source":"y = train['SalePrice']\nX = combi[: len(train)]\nX_test = combi[len(train) :]","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:09.701316Z","iopub.execute_input":"2022-08-08T15:36:09.701604Z","iopub.status.idle":"2022-08-08T15:36:09.707784Z","shell.execute_reply.started":"2022-08-08T15:36:09.701574Z","shell.execute_reply":"2022-08-08T15:36:09.706413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Split X_Train for training and validation","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.1, random_state=42)\nX_train.shape, X_val.shape, y_train.shape,y_val.shape, X_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:09.709548Z","iopub.execute_input":"2022-08-08T15:36:09.710034Z","iopub.status.idle":"2022-08-08T15:36:09.783711Z","shell.execute_reply.started":"2022-08-08T15:36:09.709987Z","shell.execute_reply":"2022-08-08T15:36:09.782597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Select model - Linear Regression","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import LinearRegression\n\nmodel1 = LinearRegression().fit(X_train, y_train)\nprint(model1.score(X_train, y_train))","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:09.785375Z","iopub.execute_input":"2022-08-08T15:36:09.785822Z","iopub.status.idle":"2022-08-08T15:36:09.935126Z","shell.execute_reply.started":"2022-08-08T15:36:09.785775Z","shell.execute_reply":"2022-08-08T15:36:09.933164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Predict on validation set","metadata":{}},{"cell_type":"code","source":"y_pred1 = model1.predict(X_val)\nprint(model1.score(X_val, y_val))","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:09.938148Z","iopub.execute_input":"2022-08-08T15:36:09.938568Z","iopub.status.idle":"2022-08-08T15:36:09.955292Z","shell.execute_reply.started":"2022-08-08T15:36:09.938524Z","shell.execute_reply":"2022-08-08T15:36:09.953623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import mean_squared_error\n\nrmse = mean_squared_error(y_val, y_pred1, squared=False)\nrmse","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:09.957219Z","iopub.execute_input":"2022-08-08T15:36:09.957869Z","iopub.status.idle":"2022-08-08T15:36:09.975271Z","shell.execute_reply.started":"2022-08-08T15:36:09.957816Z","shell.execute_reply":"2022-08-08T15:36:09.974116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.DataFrame({'Actual': y_val, 'Predicted':y_pred1})\ndf","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:09.976905Z","iopub.execute_input":"2022-08-08T15:36:09.977629Z","iopub.status.idle":"2022-08-08T15:36:09.995454Z","shell.execute_reply.started":"2022-08-08T15:36:09.977580Z","shell.execute_reply":"2022-08-08T15:36:09.994275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Graphics","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots()\nax.scatter(y_val, y_pred1, edgecolors=(0, 0, 0))\nax.plot([y.min(), y.max()], [y.min(), y.max()], 'k--', lw=4)\nax.set_xlabel('Measured')\nax.set_ylabel('Predicted')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:09.997222Z","iopub.execute_input":"2022-08-08T15:36:09.997934Z","iopub.status.idle":"2022-08-08T15:36:10.173932Z","shell.execute_reply.started":"2022-08-08T15:36:09.997885Z","shell.execute_reply":"2022-08-08T15:36:10.173106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"KNN","metadata":{}},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsRegressor\n\nmodel2 = KNeighborsRegressor(algorithm='auto', n_neighbors=7,p=1, weights='distance').fit(X_train, y_train)\nprint(model2.score(X_train, y_train))","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:10.175486Z","iopub.execute_input":"2022-08-08T15:36:10.176180Z","iopub.status.idle":"2022-08-08T15:36:10.406395Z","shell.execute_reply.started":"2022-08-08T15:36:10.176134Z","shell.execute_reply":"2022-08-08T15:36:10.402748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Predict on validation","metadata":{}},{"cell_type":"code","source":"y_pred2 = model2.predict(X_val)\nprint(model2.score(X_val, y_val))","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:10.408661Z","iopub.execute_input":"2022-08-08T15:36:10.409471Z","iopub.status.idle":"2022-08-08T15:36:10.460456Z","shell.execute_reply.started":"2022-08-08T15:36:10.409418Z","shell.execute_reply":"2022-08-08T15:36:10.458811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Metrics","metadata":{}},{"cell_type":"code","source":"from sklearn import metrics\n\nprint('Mean Absolute Error:', metrics.mean_absolute_error(y_val, y_pred2))\nprint('Mean Squared Error:', metrics.mean_squared_error(y_val, y_pred2))\nprint('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_val, y_pred2)))","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:10.464129Z","iopub.execute_input":"2022-08-08T15:36:10.465098Z","iopub.status.idle":"2022-08-08T15:36:10.478610Z","shell.execute_reply.started":"2022-08-08T15:36:10.465041Z","shell.execute_reply":"2022-08-08T15:36:10.476987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Compare","metadata":{}},{"cell_type":"code","source":"compare = pd.DataFrame({'actual': y_val.values.ravel(), 'predicted': y_pred2})\ncompare","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:10.482887Z","iopub.execute_input":"2022-08-08T15:36:10.483730Z","iopub.status.idle":"2022-08-08T15:36:10.499822Z","shell.execute_reply.started":"2022-08-08T15:36:10.483676Z","shell.execute_reply":"2022-08-08T15:36:10.498474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Plot predictions","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots()\nax.scatter(y_val, y_pred2, edgecolors=(0, 0, 0))\nax.plot([y.min(), y.max()], [y.min(), y.max()], 'k--', lw=4)\nax.set_xlabel('Measured')\nax.set_ylabel('Predicted')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:10.501518Z","iopub.execute_input":"2022-08-08T15:36:10.502065Z","iopub.status.idle":"2022-08-08T15:36:10.671372Z","shell.execute_reply.started":"2022-08-08T15:36:10.502016Z","shell.execute_reply":"2022-08-08T15:36:10.670401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Extra Trees","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import ExtraTreesRegressor\n\nmodel3 = ExtraTreesRegressor(ccp_alpha=0, criterion='mse', max_features='auto', n_estimators=500, random_state=42).fit(X_train, y_train)\nprint(model3.score(X_train, y_train))","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:10.672620Z","iopub.execute_input":"2022-08-08T15:36:10.673120Z","iopub.status.idle":"2022-08-08T15:36:15.746267Z","shell.execute_reply.started":"2022-08-08T15:36:10.673079Z","shell.execute_reply":"2022-08-08T15:36:15.745173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Predict on validation set","metadata":{}},{"cell_type":"code","source":"y_pred3 = model3.predict(X_val)\nprint(model3.score(X_val, y_val))","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:15.747549Z","iopub.execute_input":"2022-08-08T15:36:15.747851Z","iopub.status.idle":"2022-08-08T15:36:15.890139Z","shell.execute_reply.started":"2022-08-08T15:36:15.747821Z","shell.execute_reply":"2022-08-08T15:36:15.888851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Metrics","metadata":{}},{"cell_type":"code","source":"from sklearn import metrics\n\nprint('Mean Absolute Error:', metrics.mean_absolute_error(y_val, y_pred3))\nprint('Mean Squared Error:', metrics.mean_squared_error(y_val, y_pred3))\nprint('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_val, y_pred3)))","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:15.891826Z","iopub.execute_input":"2022-08-08T15:36:15.892287Z","iopub.status.idle":"2022-08-08T15:36:15.901740Z","shell.execute_reply.started":"2022-08-08T15:36:15.892240Z","shell.execute_reply":"2022-08-08T15:36:15.900106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Compare","metadata":{}},{"cell_type":"code","source":"compare = pd.DataFrame({'actual': y_val.values.ravel(), 'predicted': y_pred3})\ncompare","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:15.903482Z","iopub.execute_input":"2022-08-08T15:36:15.903895Z","iopub.status.idle":"2022-08-08T15:36:15.924464Z","shell.execute_reply.started":"2022-08-08T15:36:15.903851Z","shell.execute_reply":"2022-08-08T15:36:15.923597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Plot predictions","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots()\nax.scatter(y_val, y_pred3, edgecolors=(0, 0, 0))\nax.plot([y.min(), y.max()], [y.min(), y.max()], 'k--', lw=4)\nax.set_xlabel('Measured')\nax.set_ylabel('Predicted')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:15.925617Z","iopub.execute_input":"2022-08-08T15:36:15.926077Z","iopub.status.idle":"2022-08-08T15:36:16.097520Z","shell.execute_reply.started":"2022-08-08T15:36:15.926040Z","shell.execute_reply":"2022-08-08T15:36:16.096537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Voting Regressor","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import VotingRegressor\n\nemodel1 = VotingRegressor(estimators=[('LR', model1), ('KNN', model2), ('ET', model3)]).fit(X_train, y_train)\nprint(emodel1.score(X_train, y_train))","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:16.098688Z","iopub.execute_input":"2022-08-08T15:36:16.099207Z","iopub.status.idle":"2022-08-08T15:36:21.373356Z","shell.execute_reply.started":"2022-08-08T15:36:16.099160Z","shell.execute_reply":"2022-08-08T15:36:21.372236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Predict on validation set","metadata":{}},{"cell_type":"code","source":"y_pred4 = emodel1.predict(X_val)\nprint(emodel1.score(X_val, y_val))","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:21.374637Z","iopub.execute_input":"2022-08-08T15:36:21.374928Z","iopub.status.idle":"2022-08-08T15:36:21.547148Z","shell.execute_reply.started":"2022-08-08T15:36:21.374900Z","shell.execute_reply":"2022-08-08T15:36:21.545781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Metrics","metadata":{}},{"cell_type":"code","source":"from sklearn import metrics\n\nprint('Mean Absolute Error:', metrics.mean_absolute_error(y_val, y_pred4))\nprint('Mean Squared Error:', metrics.mean_squared_error(y_val, y_pred4))\nprint('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_val, y_pred4)))","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:21.548628Z","iopub.execute_input":"2022-08-08T15:36:21.549079Z","iopub.status.idle":"2022-08-08T15:36:21.557842Z","shell.execute_reply.started":"2022-08-08T15:36:21.549040Z","shell.execute_reply":"2022-08-08T15:36:21.556637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Compare","metadata":{}},{"cell_type":"code","source":"compare = pd.DataFrame({'actual': y_val.values.ravel(), 'predicted': y_pred4})\ncompare","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:21.559460Z","iopub.execute_input":"2022-08-08T15:36:21.559889Z","iopub.status.idle":"2022-08-08T15:36:21.578743Z","shell.execute_reply.started":"2022-08-08T15:36:21.559843Z","shell.execute_reply":"2022-08-08T15:36:21.577522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Plot graph","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots()\nax.scatter(y_val, y_pred4, edgecolors=(0, 0, 0))\nax.plot([y.min(), y.max()], [y.min(), y.max()], 'k--', lw=4)\nax.set_xlabel('Measured')\nax.set_ylabel('Predicted')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:21.580305Z","iopub.execute_input":"2022-08-08T15:36:21.580721Z","iopub.status.idle":"2022-08-08T15:36:21.745142Z","shell.execute_reply.started":"2022-08-08T15:36:21.580676Z","shell.execute_reply":"2022-08-08T15:36:21.744098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Predict on test set and submit","metadata":{}},{"cell_type":"code","source":"preds = emodel1.predict(X_test)\npreds = preds.astype(int)\npreds[preds < 0] = 0\npreds","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:21.746289Z","iopub.execute_input":"2022-08-08T15:36:21.746617Z","iopub.status.idle":"2022-08-08T15:36:22.117405Z","shell.execute_reply.started":"2022-08-08T15:36:21.746587Z","shell.execute_reply":"2022-08-08T15:36:22.116420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Submit","metadata":{}},{"cell_type":"code","source":"submission.SalePrice = preds\nsubmission.to_csv('submission.csv', index=False)\nsubmission = pd.read_csv(\"submission.csv\")\nsubmission","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:36:22.119110Z","iopub.execute_input":"2022-08-08T15:36:22.119544Z","iopub.status.idle":"2022-08-08T15:36:22.682489Z","shell.execute_reply.started":"2022-08-08T15:36:22.119498Z","shell.execute_reply":"2022-08-08T15:36:22.681392Z"},"trusted":true},"execution_count":null,"outputs":[]}]}