{"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":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom matplotlib.offsetbox import AnchoredText\nimport seaborn as sns\nsns.set_style('darkgrid')\nfrom scipy.stats import norm, boxcox\nfrom scipy.special import inv_boxcox\nimport warnings\nwarnings.filterwarnings('ignore')\n\n# sklearn\nfrom sklearn.linear_model import LinearRegression, Lasso, Ridge\nfrom sklearn.tree import DecisionTreeRegressor\nfrom sklearn.neighbors import KNeighborsRegressor\nfrom sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor, AdaBoostRegressor\nfrom sklearn.svm import SVR\nfrom sklearn.metrics import accuracy_score, roc_auc_score, mean_squared_error\nfrom sklearn.model_selection import GridSearchCV, train_test_split, KFold\nfrom xgboost import XGBRegressor\n","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:23.733913Z","iopub.execute_input":"2022-08-12T20:36:23.734323Z","iopub.status.idle":"2022-08-12T20:36:23.742698Z","shell.execute_reply.started":"2022-08-12T20:36:23.734293Z","shell.execute_reply":"2022-08-12T20:36:23.741651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### LOAD DATA ","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('../input/house-prices-advanced-regression-techniques/train.csv')\ntest = pd.read_csv('../input/house-prices-advanced-regression-techniques/test.csv')\n\ntrain.drop('Id', axis=1, inplace=True)\nid = test['Id']\ntest.drop('Id', axis=1, inplace=True)\n\nprint(train.shape)\nprint(test.shape)\nprint(train.head())","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:23.764240Z","iopub.execute_input":"2022-08-12T20:36:23.764823Z","iopub.status.idle":"2022-08-12T20:36:23.821390Z","shell.execute_reply.started":"2022-08-12T20:36:23.764783Z","shell.execute_reply":"2022-08-12T20:36:23.820705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### DATA DESCRIPTION","metadata":{}},{"cell_type":"code","source":"with open('../input/house-prices-advanced-regression-techniques/data_description.txt', 'r') as f:\n    data = f.read()\n# print(data)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:23.822740Z","iopub.execute_input":"2022-08-12T20:36:23.823201Z","iopub.status.idle":"2022-08-12T20:36:23.828241Z","shell.execute_reply.started":"2022-08-12T20:36:23.823170Z","shell.execute_reply":"2022-08-12T20:36:23.827081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### CORRELATION DATA ","metadata":{}},{"cell_type":"code","source":"cols = train.corr().nlargest(10, 'SalePrice').index\ncorr_mat = np.corrcoef(train[cols].values.T)\nfig = plt.subplots(figsize=(12, 12))\nsns.heatmap(corr_mat, annot = True, fmt = '.2f', square=True, \n            xticklabels=cols.values, yticklabels=cols.values,\n            mask = np.triu(corr_mat))\nplt.xticks(rotation=60)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:23.829794Z","iopub.execute_input":"2022-08-12T20:36:23.830531Z","iopub.status.idle":"2022-08-12T20:36:24.434352Z","shell.execute_reply.started":"2022-08-12T20:36:23.830481Z","shell.execute_reply":"2022-08-12T20:36:24.433298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### LOOKING FOR OUTLIERS","metadata":{}},{"cell_type":"code","source":"numeric_dtypes = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']\nnumeric = []\nfor col in train:\n    if train[col].dtype in numeric_dtypes:\n        if train[col].value_counts().count() < 6 or col in ['SalePrice']:\n            pass\n        else:\n            numeric.append(col)\nfig, axs = plt.subplots(figsize=(20, 100))\nfor i, feature in enumerate(list(train[numeric]), 1):\n    plt.subplot(len(list(numeric)), 4, i)\n    sns.residplot(x=feature, y='SalePrice', data=train)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:24.436241Z","iopub.execute_input":"2022-08-12T20:36:24.436606Z","iopub.status.idle":"2022-08-12T20:36:29.692853Z","shell.execute_reply.started":"2022-08-12T20:36:24.436566Z","shell.execute_reply":"2022-08-12T20:36:29.691860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### REMOVING OUTLIERS","metadata":{}},{"cell_type":"code","source":"train.drop(train[train['BsmtFinSF1'] > 5000].index, inplace=True)\ntrain.drop(train[(train['OverallQual'] < 5) & (train['SalePrice'] > 200000)].index, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:29.694197Z","iopub.execute_input":"2022-08-12T20:36:29.694535Z","iopub.status.idle":"2022-08-12T20:36:29.707467Z","shell.execute_reply.started":"2022-08-12T20:36:29.694506Z","shell.execute_reply":"2022-08-12T20:36:29.706522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### TARGET FEATURE ","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(10, 10))\nax = sns.distplot(train['SalePrice'], fit=norm)\nax.set_title('SalePrice Distribution', fontsize=18)\nax.set(xlim=(0, 800000))\nplt.ticklabel_format(style='plain', axis='y')\nplt.show()\n\nskewness = train.SalePrice.skew()\nkurtosis = train.SalePrice.kurt()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:29.708566Z","iopub.execute_input":"2022-08-12T20:36:29.709405Z","iopub.status.idle":"2022-08-12T20:36:30.116970Z","shell.execute_reply.started":"2022-08-12T20:36:29.709371Z","shell.execute_reply":"2022-08-12T20:36:30.115980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### SKEWNESS AND KURTOSIS","metadata":{}},{"cell_type":"code","source":"skewness = train.SalePrice.skew()\nkurtosis = train.SalePrice.kurt()\nprint(f'Skewness of target feature: {skewness}')\nprint(f'Kurtosis of target feature: {kurtosis}')","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:30.118323Z","iopub.execute_input":"2022-08-12T20:36:30.118950Z","iopub.status.idle":"2022-08-12T20:36:30.124325Z","shell.execute_reply.started":"2022-08-12T20:36:30.118916Z","shell.execute_reply":"2022-08-12T20:36:30.123714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### CORRECTING SKEWNESS","metadata":{}},{"cell_type":"code","source":"train['SalePrice1'] = np.log1p(train['SalePrice'])\nprint(f'Skewness with log(x+1): {train.SalePrice1.skew()}')\ntrain['SalePrice2'] = np.log(train['SalePrice'])\nprint(f'Skewness with log: {train.SalePrice2.skew()}')\ntrain['SalePrice3'] = np.sqrt(train['SalePrice'])\nprint(f'Skewness with sqrt: {train.SalePrice3.skew()}')\ntrain['SalePrice4'] = boxcox(train['SalePrice'])[0]\nlambda_value = boxcox(train['SalePrice'])[1]\nprint(f'Skewness with boxcox: {train.SalePrice4.skew()}')","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:30.126283Z","iopub.execute_input":"2022-08-12T20:36:30.127065Z","iopub.status.idle":"2022-08-12T20:36:30.147614Z","shell.execute_reply.started":"2022-08-12T20:36:30.127032Z","shell.execute_reply":"2022-08-12T20:36:30.146928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['SalePrice'] = train['SalePrice4']\ntrain.drop(['SalePrice1', 'SalePrice2', 'SalePrice3', 'SalePrice4'], axis=1, inplace=True)\n\nfig, ax = plt.subplots(figsize=(10, 10))\nax = sns.distplot(train['SalePrice'], fit = norm)\nax.set_title('New SalePrice Distribution', fontsize=18)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:30.149013Z","iopub.execute_input":"2022-08-12T20:36:30.149335Z","iopub.status.idle":"2022-08-12T20:36:30.524120Z","shell.execute_reply.started":"2022-08-12T20:36:30.149305Z","shell.execute_reply":"2022-08-12T20:36:30.523154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### NEW SKEWNESS AND KURTOSIS","metadata":{}},{"cell_type":"code","source":"print(f'New skewness of target feature: {train.SalePrice.skew()}')\nprint(f'New kurtosis of target feature: {train.SalePrice.kurt()}')","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:30.525371Z","iopub.execute_input":"2022-08-12T20:36:30.525722Z","iopub.status.idle":"2022-08-12T20:36:30.532329Z","shell.execute_reply.started":"2022-08-12T20:36:30.525692Z","shell.execute_reply":"2022-08-12T20:36:30.531266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### COMBINING SETS","metadata":{}},{"cell_type":"code","source":"train_features = train.drop(['SalePrice'], axis=1)\nall_data = pd.concat([train_features, test]).reset_index(drop=True)\nall_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:30.533394Z","iopub.execute_input":"2022-08-12T20:36:30.533717Z","iopub.status.idle":"2022-08-12T20:36:30.565824Z","shell.execute_reply.started":"2022-08-12T20:36:30.533687Z","shell.execute_reply":"2022-08-12T20:36:30.564803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### MISSING VALUES","metadata":{}},{"cell_type":"code","source":"total = all_data.isnull().sum().sort_values(ascending=False)\npercent = round((total / all_data.isnull().count() * 100).sort_values(ascending=False), 2)\nmissing_data = pd.concat([total, percent], axis=1, keys=['total', 'percent'])\nprint(missing_data[missing_data['total'] > 0])","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:30.567171Z","iopub.execute_input":"2022-08-12T20:36:30.567941Z","iopub.status.idle":"2022-08-12T20:36:30.598878Z","shell.execute_reply.started":"2022-08-12T20:36:30.567887Z","shell.execute_reply":"2022-08-12T20:36:30.597899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### TREATING MISSING DATA","metadata":{}},{"cell_type":"code","source":"all_data['PoolQC'].fillna('No pool', inplace=True)\nall_data['Alley'].fillna('No alley access', inplace=True)\nall_data['Electrical'].fillna('sBrkr', inplace = True)\nall_data['LotFrontage'].fillna(all_data.LotFrontage.mean(), inplace=True)\nall_data['Fence'].fillna('No Fence', inplace=True)\n\nfor column in ['MiscFeature', 'MasVnrType', 'FireplaceQu']:\n    all_data[column].fillna('None', inplace=True)\n\nfor column in ['GarageType', 'GarageQual', 'GarageCond', 'GarageFinish']:\n    all_data[column].fillna('No garage', inplace = True)\n\nfor column in ['BsmtExposure', 'BsmtFinType2', 'BsmtFinType1', 'BsmtCond', 'BsmtQual']:\n    all_data[column].fillna('No Basement', inplace = True)\n\nfor column in ['BsmtHalfBath', 'BsmtFullBath', 'TotalBsmtSF', 'BsmtFinSF1', 'BsmtFinSF2', \n               'GarageCars', 'MasVnrArea', 'GarageArea', 'BsmtUnfSF', 'GarageYrBlt']:\n    all_data[column].fillna(0, inplace=True)\n\nfor column in ['SaleType', 'Functional', 'KitchenQual', 'Exterior2nd',\n               'Exterior1st', 'Utilities', 'MSZoning']:\n    all_data[column].fillna(all_data[column].value_counts().index.tolist()[0], inplace=True)    ","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:30.600423Z","iopub.execute_input":"2022-08-12T20:36:30.600818Z","iopub.status.idle":"2022-08-12T20:36:30.631361Z","shell.execute_reply.started":"2022-08-12T20:36:30.600783Z","shell.execute_reply":"2022-08-12T20:36:30.630413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### CREATING NEW FEATURES","metadata":{}},{"cell_type":"code","source":"all_data['HasPool'] = (all_data['PoolQC'] != 'No pool') * 1\nall_data['HasMiscFeature'] = (all_data['MiscFeature'] != 'None') * 1\nall_data['HasAlleyAccess'] = (all_data['Alley'] != 'No alley access') * 1\nall_data['HasFence'] = (all_data['Fence'] != 'No Fence') * 1\nall_data['HasEnclosedPorch'] = (all_data['EnclosedPorch'] == 0) * 1\nall_data['HasOpenPorch'] = (all_data['OpenPorchSF'] == 0) * 1\nall_data['Has3SsnPorch'] = (all_data['3SsnPorch'] == 0) * 1\nall_data['HasScreenPorch'] = (all_data['ScreenPorch'] == 0) * 1\nall_data['HasFireplace'] = (all_data['FireplaceQu'] != 'None') * 1\nall_data['HasGarage'] = (all_data['GarageType'] != 'No garage') * 1\nall_data['HasBasement'] = (all_data['BsmtExposure'] != 'No Basement') * 1\nall_data['HasWoodDeck'] = (all_data['WoodDeckSF'] == 0) * 1","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:30.632441Z","iopub.execute_input":"2022-08-12T20:36:30.632772Z","iopub.status.idle":"2022-08-12T20:36:30.653179Z","shell.execute_reply.started":"2022-08-12T20:36:30.632722Z","shell.execute_reply":"2022-08-12T20:36:30.652159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### TRANSFORMING FEATURES","metadata":{}},{"cell_type":"code","source":"all_data['MSSubClass'] = all_data['MSSubClass'].astype(str)\nall_data['YrSold'] = all_data['YrSold'].astype(str)\nall_data['MoSold'] = all_data['MoSold'].astype(str)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:30.654361Z","iopub.execute_input":"2022-08-12T20:36:30.655187Z","iopub.status.idle":"2022-08-12T20:36:30.668274Z","shell.execute_reply.started":"2022-08-12T20:36:30.655144Z","shell.execute_reply":"2022-08-12T20:36:30.667144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### CORRECTING SKEWNESS OF NUMERICAL FEATURES ","metadata":{}},{"cell_type":"code","source":"for col in all_data.columns:\n    if all_data[col].dtype in numeric_dtypes:\n        all_data[col] = np.log1p(all_data[col])","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:30.669663Z","iopub.execute_input":"2022-08-12T20:36:30.670205Z","iopub.status.idle":"2022-08-12T20:36:30.707650Z","shell.execute_reply.started":"2022-08-12T20:36:30.670171Z","shell.execute_reply":"2022-08-12T20:36:30.706469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### ENCODING CATEGORICAL VALUES","metadata":{}},{"cell_type":"code","source":"all_data = pd.get_dummies(all_data)\nprint(all_data.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:30.709049Z","iopub.execute_input":"2022-08-12T20:36:30.709399Z","iopub.status.idle":"2022-08-12T20:36:30.775621Z","shell.execute_reply.started":"2022-08-12T20:36:30.709367Z","shell.execute_reply":"2022-08-12T20:36:30.774523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### SPLITING DATA","metadata":{}},{"cell_type":"code","source":"x = all_data.iloc[:len(train), :]\ntest = all_data.iloc[len(train):, :]\ny = train[\"SalePrice\"].values\nprint(x.shape, test.shape, y.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:30.777262Z","iopub.execute_input":"2022-08-12T20:36:30.778042Z","iopub.status.idle":"2022-08-12T20:36:30.785770Z","shell.execute_reply.started":"2022-08-12T20:36:30.777985Z","shell.execute_reply":"2022-08-12T20:36:30.784687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## TRAINING A MODEL","metadata":{}},{"cell_type":"code","source":"x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.25, random_state = 0)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:30.787232Z","iopub.execute_input":"2022-08-12T20:36:30.787783Z","iopub.status.idle":"2022-08-12T20:36:30.803730Z","shell.execute_reply.started":"2022-08-12T20:36:30.787724Z","shell.execute_reply":"2022-08-12T20:36:30.802841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### LINEAR REGRESSION","metadata":{}},{"cell_type":"code","source":"lin = LinearRegression(n_jobs = -1)\nlin.fit(x, y)\ny_pred = lin.predict(x_test)\nrmse_lin = np.sqrt(mean_squared_error(y_test, y_pred))\nlin_score = lin.score(x_test, y_test)\nprint(\"The Root Mean Squared Error for Linear Regression is:\", round(rmse_lin, 3))\nprint(\"Linear Regression Score is:\", round(lin_score * 100, 3), \"%\")","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:30.806762Z","iopub.execute_input":"2022-08-12T20:36:30.807828Z","iopub.status.idle":"2022-08-12T20:36:30.918937Z","shell.execute_reply.started":"2022-08-12T20:36:30.807790Z","shell.execute_reply":"2022-08-12T20:36:30.917647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### RIDGE REGRESSION","metadata":{}},{"cell_type":"code","source":"ridge = Ridge(alpha = 5)\nridge.fit(x, y)\ny_pred = ridge.predict(x_test)\nrmse_ridge = np.sqrt(mean_squared_error(y_test, y_pred))\nridge_score = ridge.score(x_test, y_test)\nprint(\"The Root Mean Squared Error for Ridge Regression is:\", round(rmse_ridge, 3))\nprint(\"Ridge Regression Score is:\", round(ridge_score * 100, 3), \"%\")","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:30.920765Z","iopub.execute_input":"2022-08-12T20:36:30.921611Z","iopub.status.idle":"2022-08-12T20:36:31.035364Z","shell.execute_reply.started":"2022-08-12T20:36:30.921547Z","shell.execute_reply":"2022-08-12T20:36:31.033646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### LASSO REGRESSION","metadata":{}},{"cell_type":"code","source":"lasso = Lasso(alpha = 0.001)\nlasso.fit(x, y)\ny_pred = lasso.predict(x_test)\nrmse_lasso = np.sqrt(mean_squared_error(y_test, y_pred))\nlasso_score = lasso.score(x_test, y_test)\nprint(\"The Root Mean Squared Error for Lasso Regression is:\", round(rmse_lasso, 3))\nprint(\"Lasso Regression Score is:\", round(lasso_score * 100, 3), \"%\")","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:31.037621Z","iopub.execute_input":"2022-08-12T20:36:31.040063Z","iopub.status.idle":"2022-08-12T20:36:31.249003Z","shell.execute_reply.started":"2022-08-12T20:36:31.039995Z","shell.execute_reply":"2022-08-12T20:36:31.247675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### SUPPORT VECTOR REGRESSION","metadata":{}},{"cell_type":"code","source":"svr = SVR(kernel = 'rbf', gamma = 'scale', epsilon = 0.01, C = 1)\nsvr.fit(x, y)\ny_pred = svr.predict(x_test)\nrmse_svr = np.sqrt(mean_squared_error(y_test, y_pred))\nsvr_score = svr.score(x_test, y_test)\nprint(\"The Root Mean Squared Error for Support Vector Regression is:\", round(rmse_svr, 3))\nprint(\"Support Vector Regression Score is:\", round(svr_score * 100, 3), \"%\")","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:31.251240Z","iopub.execute_input":"2022-08-12T20:36:31.253410Z","iopub.status.idle":"2022-08-12T20:36:32.041922Z","shell.execute_reply.started":"2022-08-12T20:36:31.253345Z","shell.execute_reply":"2022-08-12T20:36:32.041135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### DECISION TREE REGRESSION","metadata":{}},{"cell_type":"code","source":"dtr = DecisionTreeRegressor(random_state = 0, max_depth = 6, max_leaf_nodes = 18,\n                            min_samples_leaf = 9, splitter = 'best')\ndtr.fit(x, y)\ny_pred = dtr.predict(x_test)\nrmse_dtr = np.sqrt(mean_squared_error(y_test, y_pred))\ndtr_score = dtr.score(x_test, y_test)\nprint(\"The Root Mean Squared Error for Decision Tree Regression is:\", round(rmse_dtr, 3))\nprint(\"Decision Tree Regression Score is:\", round(dtr_score * 100, 3), \"%\")","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:32.043437Z","iopub.execute_input":"2022-08-12T20:36:32.044111Z","iopub.status.idle":"2022-08-12T20:36:32.089469Z","shell.execute_reply.started":"2022-08-12T20:36:32.044066Z","shell.execute_reply":"2022-08-12T20:36:32.088714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### RANDOM FOREST REGRESSION","metadata":{}},{"cell_type":"code","source":"rfr = RandomForestRegressor(n_estimators = 130, max_depth = 6, min_samples_split = 2)\nrfr.fit(x, y)\ny_pred = rfr.predict(x_test)\nrmse_rfr = np.sqrt(mean_squared_error(y_test, y_pred))\nrfr_score = rfr.score(x_test, y_test)\nprint(\"The Root Mean Squared Error for Random Forest Regression is:\", round(rmse_rfr, 3))\nprint(\"Random Forest Regression Score is:\", round(rfr_score * 100, 3), \"%\")","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:32.090423Z","iopub.execute_input":"2022-08-12T20:36:32.090956Z","iopub.status.idle":"2022-08-12T20:36:34.176714Z","shell.execute_reply.started":"2022-08-12T20:36:32.090921Z","shell.execute_reply":"2022-08-12T20:36:34.175692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### ADABOOST REGRESSOR","metadata":{}},{"cell_type":"code","source":"abr = AdaBoostRegressor(random_state = 0, learning_rate = 0.1, n_estimators = 300)\nabr.fit(x, y)\ny_pred = abr.predict(x_test)\nrmse_abr = np.sqrt(mean_squared_error(y_test, y_pred))\nabr_score = abr.score(x_test, y_test)\nprint(\"The Root Mean Squared Error is:\", round(rmse_abr, 3))\nprint(\"ABR Score is:\", round(abr_score * 100, 3), \"%\")","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:34.177874Z","iopub.execute_input":"2022-08-12T20:36:34.178254Z","iopub.status.idle":"2022-08-12T20:36:39.078907Z","shell.execute_reply.started":"2022-08-12T20:36:34.178224Z","shell.execute_reply":"2022-08-12T20:36:39.077973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### XGBOOST REGRESSOR","metadata":{}},{"cell_type":"code","source":"xgb = XGBRegressor(n_estimators = 990, learning_rate = 0.02)\nxgb.fit(x, y)\ny_pred = xgb.predict(x_test)\nrmse_xgb = np.sqrt(mean_squared_error(y_test, y_pred))\nxgb_score = xgb.score(x_test, y_test) \nprint(\"The Root Mean Squared Error is:\", round(rmse_xgb, 4))\nprint(\"XGB Score is:\", round(xgb_score * 100, 3), \"%\")","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:39.079989Z","iopub.execute_input":"2022-08-12T20:36:39.080383Z","iopub.status.idle":"2022-08-12T20:36:52.107005Z","shell.execute_reply.started":"2022-08-12T20:36:39.080353Z","shell.execute_reply":"2022-08-12T20:36:52.106237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### VISUALIZING RESULTS","metadata":{}},{"cell_type":"code","source":"data = {'Linear Regression': (rmse_lin, lin_score), 'Ridge Regression': (rmse_ridge, ridge_score), \n        'Lasso Regression': (rmse_lasso, lasso_score), 'Support Vector Regression': (rmse_svr, svr_score),\n       'Decision Tree Regression': (rmse_dtr, dtr_score), 'Random Forest Regression': (rmse_rfr, rfr_score),\n       'AdaBoost Regression': (rmse_abr, abr_score), 'XGBoost Regressor': (rmse_xgb, xgb_score)}","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:52.110666Z","iopub.execute_input":"2022-08-12T20:36:52.112916Z","iopub.status.idle":"2022-08-12T20:36:52.120107Z","shell.execute_reply.started":"2022-08-12T20:36:52.112877Z","shell.execute_reply":"2022-08-12T20:36:52.119157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, (ax0, ax1) = plt.subplots(2, 1, figsize=(20, 12))\nmodels = list(data.keys())\nvalues = list(data.values())\nrmse_values = [value[0] for value in values]\nscore_values = [value[1] for value in values]\nax0 = sns.barplot(x = models, y = rmse_values, ax=ax0)\nax0.set_ylim(0, 0.07)\nax0.set_title('Root Mean Squared Error Score',size=20)\nat0 = AnchoredText('Less is better', prop=dict(size=16), loc='upper left')\nax0.add_artist(at0)\nax1 = sns.barplot(x = models, y = score_values, ax=ax1)\nax1.set_ylim(0.75, 1)\nax1.set_title('R^2 Score', size=20)\nat1 = AnchoredText('More is better', prop=dict(size=16), loc='upper left')\nax1.add_artist(at1)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:52.121680Z","iopub.execute_input":"2022-08-12T20:36:52.122138Z","iopub.status.idle":"2022-08-12T20:36:52.595188Z","shell.execute_reply.started":"2022-08-12T20:36:52.122097Z","shell.execute_reply":"2022-08-12T20:36:52.594180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### SUBMIT PREDICTION","metadata":{}},{"cell_type":"code","source":"submission = pd.read_csv(\"../input/house-prices-advanced-regression-techniques/sample_submission.csv\")\nsubmission.shape\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:52.596455Z","iopub.execute_input":"2022-08-12T20:36:52.596793Z","iopub.status.idle":"2022-08-12T20:36:52.610899Z","shell.execute_reply.started":"2022-08-12T20:36:52.596763Z","shell.execute_reply":"2022-08-12T20:36:52.610144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = xgb.predict(test)\nsubmission.iloc[:, 1] = np.floor(inv_boxcox(prediction, lambda_value))\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:52.612154Z","iopub.execute_input":"2022-08-12T20:36:52.612480Z","iopub.status.idle":"2022-08-12T20:36:52.670836Z","shell.execute_reply.started":"2022-08-12T20:36:52.612442Z","shell.execute_reply":"2022-08-12T20:36:52.669821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"q1 = submission['SalePrice'].quantile(0.06)\nq2 = submission['SalePrice'].quantile(0.95)\nsubmission['SalePrice'] = submission['SalePrice'].apply(lambda x: x if x > q1 else x*1.75)\nsubmission['SalePrice'] = submission['SalePrice'].apply(lambda x: x if x < q2 else x*0.4)\nsubmission['SalePrice'] = submission['SalePrice'].apply(lambda x: x if x < 250000 else x-75000)\nsubmission['SalePrice'] = submission['SalePrice'].apply(lambda x: x if x > 135000 else x+55000)\n\nsubmission.to_csv('submission_prediction', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T20:36:52.672190Z","iopub.execute_input":"2022-08-12T20:36:52.672618Z","iopub.status.idle":"2022-08-12T20:36:52.691708Z","shell.execute_reply.started":"2022-08-12T20:36:52.672566Z","shell.execute_reply":"2022-08-12T20:36:52.690938Z"},"trusted":true},"execution_count":null,"outputs":[]}]}