{"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 scipy\nimport numpy as np\nimport pandas as pd\npd.set_option('max_columns', None)\npd.set_option('max_rows', 80)\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nsns.set_style('darkgrid')\n\nfrom sklearn.cluster import KMeans\nfrom catboost import CatBoostRegressor\nfrom sklearn.linear_model import BayesianRidge, Lasso, HuberRegressor, Ridge, OrthogonalMatchingPursuit, SGDRegressor\nfrom lightgbm import LGBMRegressor\nfrom sklearn.ensemble import GradientBoostingRegressor, RandomForestRegressor\nfrom xgboost import XGBRegressor\nfrom sklearn.neighbors import KNeighborsRegressor\nfrom sklearn.ensemble import ExtraTreesRegressor, RandomForestRegressor\nfrom sklearn.tree import DecisionTreeRegressor\nfrom sklearn.svm import SVR\n\nfrom sklearn.impute import KNNImputer\nfrom sklearn.metrics import make_scorer, mean_squared_error\nfrom sklearn.model_selection import train_test_split, KFold, cross_val_score, GridSearchCV\nfrom sklearn.preprocessing import PowerTransformer, FunctionTransformer, StandardScaler, MinMaxScaler\n","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:13.439529Z","iopub.execute_input":"2022-07-16T15:19:13.440034Z","iopub.status.idle":"2022-07-16T15:19:17.104219Z","shell.execute_reply.started":"2022-07-16T15:19:13.439941Z","shell.execute_reply":"2022-07-16T15:19:17.103177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train0 = pd.read_csv('../input/house-prices-advanced-regression-techniques/train.csv')\ntest0 = pd.read_csv('../input/house-prices-advanced-regression-techniques/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:17.105860Z","iopub.execute_input":"2022-07-16T15:19:17.106101Z","iopub.status.idle":"2022-07-16T15:19:17.179029Z","shell.execute_reply.started":"2022-07-16T15:19:17.106069Z","shell.execute_reply":"2022-07-16T15:19:17.178305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" # cleaning","metadata":{}},{"cell_type":"code","source":"target = train0['SalePrice']","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:17.183004Z","iopub.execute_input":"2022-07-16T15:19:17.184503Z","iopub.status.idle":"2022-07-16T15:19:17.194265Z","shell.execute_reply.started":"2022-07-16T15:19:17.184459Z","shell.execute_reply":"2022-07-16T15:19:17.193126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train0.select_dtypes(np.number)\n# According to data description MSSubClass should be a 'str'","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:17.196730Z","iopub.execute_input":"2022-07-16T15:19:17.197752Z","iopub.status.idle":"2022-07-16T15:19:17.254973Z","shell.execute_reply.started":"2022-07-16T15:19:17.197715Z","shell.execute_reply":"2022-07-16T15:19:17.253907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train1 = train0.copy()\ntrain1 = train1.drop(['Id', 'SalePrice'], axis=1)\ntrain1 = pd.concat([train1, test0.drop(['Id'], axis=1)], axis=0)\ndata0 = train1","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:17.256531Z","iopub.execute_input":"2022-07-16T15:19:17.257098Z","iopub.status.idle":"2022-07-16T15:19:17.285167Z","shell.execute_reply.started":"2022-07-16T15:19:17.257028Z","shell.execute_reply":"2022-07-16T15:19:17.284298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train1['MSSubClass'] = train1['MSSubClass'].apply(str)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:17.286442Z","iopub.execute_input":"2022-07-16T15:19:17.286867Z","iopub.status.idle":"2022-07-16T15:19:17.294880Z","shell.execute_reply.started":"2022-07-16T15:19:17.286835Z","shell.execute_reply":"2022-07-16T15:19:17.293921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train1.select_dtypes(np.number)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:17.296373Z","iopub.execute_input":"2022-07-16T15:19:17.297360Z","iopub.status.idle":"2022-07-16T15:19:17.353099Z","shell.execute_reply.started":"2022-07-16T15:19:17.297309Z","shell.execute_reply":"2022-07-16T15:19:17.352139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Categorical values cleaning ","metadata":{"execution":{"iopub.status.busy":"2022-05-12T15:32:34.029075Z","iopub.execute_input":"2022-05-12T15:32:34.029366Z","iopub.status.idle":"2022-05-12T15:32:34.03415Z","shell.execute_reply.started":"2022-05-12T15:32:34.029319Z","shell.execute_reply":"2022-05-12T15:32:34.033175Z"}}},{"cell_type":"code","source":"train1.select_dtypes('object').columns[train1.select_dtypes('object').isna().any()].tolist()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:17.354566Z","iopub.execute_input":"2022-07-16T15:19:17.355308Z","iopub.status.idle":"2022-07-16T15:19:17.383839Z","shell.execute_reply.started":"2022-07-16T15:19:17.355256Z","shell.execute_reply":"2022-07-16T15:19:17.382828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# For cloumns which missing values have some meaning \nfor col in [\n    'Alley',\n    'BsmtQual',\n    'BsmtCond',\n    'BsmtExposure',\n    'BsmtFinType1',\n    'BsmtFinType2',\n    'FireplaceQu',\n    'GarageType',\n    'GarageFinish',\n    'GarageQual',\n    'GarageCond',\n    'PoolQC',\n    'Fence',\n    'MiscFeature'\n]:\n    train1[col] = train1[col].fillna(\"None\")\n\n# For cloumns which missing values dont have meaning  \nfor column in [\n    'MSZoning',\n    'Utilities',\n    'Exterior1st',\n    'Exterior2nd',\n    'MasVnrType',\n    'Electrical',\n    'KitchenQual',\n    'Functional',\n    'SaleType'\n]:\n    train1[column] = train1[column].fillna(train1[column].mode()[0])","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:17.385202Z","iopub.execute_input":"2022-07-16T15:19:17.386128Z","iopub.status.idle":"2022-07-16T15:19:17.416732Z","shell.execute_reply.started":"2022-07-16T15:19:17.386087Z","shell.execute_reply":"2022-07-16T15:19:17.416025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train1.select_dtypes('object').isna().sum().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:17.419701Z","iopub.execute_input":"2022-07-16T15:19:17.420411Z","iopub.status.idle":"2022-07-16T15:19:17.442525Z","shell.execute_reply.started":"2022-07-16T15:19:17.420376Z","shell.execute_reply":"2022-07-16T15:19:17.441479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Numerci value cleaning","metadata":{}},{"cell_type":"code","source":"train2 = train1.copy()\ntrain2.select_dtypes(np.number)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:17.444240Z","iopub.execute_input":"2022-07-16T15:19:17.444791Z","iopub.status.idle":"2022-07-16T15:19:17.498108Z","shell.execute_reply.started":"2022-07-16T15:19:17.444728Z","shell.execute_reply":"2022-07-16T15:19:17.497079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNNImputer()\n\ntrain_num = train2.select_dtypes(np.number)\ntrain_object = train2.select_dtypes('object')\n\ntrain_num_transformed = knn.fit_transform(train_num)\ntrain_num = pd.DataFrame(train_num_transformed, columns=train_num.columns, index=data0.index)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:17.499881Z","iopub.execute_input":"2022-07-16T15:19:17.500449Z","iopub.status.idle":"2022-07-16T15:19:17.880847Z","shell.execute_reply.started":"2022-07-16T15:19:17.500400Z","shell.execute_reply":"2022-07-16T15:19:17.880116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_num.isna().sum().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:17.881928Z","iopub.execute_input":"2022-07-16T15:19:17.882219Z","iopub.status.idle":"2022-07-16T15:19:17.889343Z","shell.execute_reply.started":"2022-07-16T15:19:17.882186Z","shell.execute_reply":"2022-07-16T15:19:17.888647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train3 = pd.concat([train_num, train_object], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:17.890284Z","iopub.execute_input":"2022-07-16T15:19:17.890848Z","iopub.status.idle":"2022-07-16T15:19:17.905105Z","shell.execute_reply.started":"2022-07-16T15:19:17.890816Z","shell.execute_reply":"2022-07-16T15:19:17.904342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train3.isna().sum().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:17.906374Z","iopub.execute_input":"2022-07-16T15:19:17.906745Z","iopub.status.idle":"2022-07-16T15:19:17.939323Z","shell.execute_reply.started":"2022-07-16T15:19:17.906712Z","shell.execute_reply":"2022-07-16T15:19:17.938330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train2.shape, train3.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:17.940540Z","iopub.execute_input":"2022-07-16T15:19:17.941188Z","iopub.status.idle":"2022-07-16T15:19:17.948426Z","shell.execute_reply.started":"2022-07-16T15:19:17.941141Z","shell.execute_reply":"2022-07-16T15:19:17.947423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_num.shape, train_object.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:17.950103Z","iopub.execute_input":"2022-07-16T15:19:17.950664Z","iopub.status.idle":"2022-07-16T15:19:17.965780Z","shell.execute_reply.started":"2022-07-16T15:19:17.950616Z","shell.execute_reply":"2022-07-16T15:19:17.964792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Feature enginiring ","metadata":{}},{"cell_type":"code","source":"train4 = train3.copy()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:17.967767Z","iopub.execute_input":"2022-07-16T15:19:17.968366Z","iopub.status.idle":"2022-07-16T15:19:17.976714Z","shell.execute_reply.started":"2022-07-16T15:19:17.968314Z","shell.execute_reply":"2022-07-16T15:19:17.975957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train4[\"SqFtPerRoom\"] = train4[\"GrLivArea\"] / (train4[\"TotRmsAbvGrd\"] +\n                                                       train4[\"FullBath\"] +\n                                                       train4[\"HalfBath\"] +\n                                                       train4[\"KitchenAbvGr\"])\n\ntrain4['Total_Home_Quality'] = train4['OverallQual'] + train4['OverallCond']\n\ntrain4['Total_Bathrooms'] = (train4['FullBath'] + (0.5 * train4['HalfBath']) +\n                               train4['BsmtFullBath'] + (0.5 * train4['BsmtHalfBath']))\n\ntrain4[\"HighQualSF\"] = train4[\"1stFlrSF\"] + train4[\"2ndFlrSF\"]","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:17.978332Z","iopub.execute_input":"2022-07-16T15:19:17.979251Z","iopub.status.idle":"2022-07-16T15:19:17.997720Z","shell.execute_reply.started":"2022-07-16T15:19:17.979197Z","shell.execute_reply":"2022-07-16T15:19:17.996380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train4['Total_Home_Quality']","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:17.999250Z","iopub.execute_input":"2022-07-16T15:19:17.999550Z","iopub.status.idle":"2022-07-16T15:19:18.010613Z","shell.execute_reply.started":"2022-07-16T15:19:17.999487Z","shell.execute_reply":"2022-07-16T15:19:18.009572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Feature transformation","metadata":{}},{"cell_type":"markdown","source":"# Skew data transformation","metadata":{}},{"cell_type":"code","source":"train5 = train4.copy()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:18.012799Z","iopub.execute_input":"2022-07-16T15:19:18.013413Z","iopub.status.idle":"2022-07-16T15:19:18.021686Z","shell.execute_reply.started":"2022-07-16T15:19:18.013361Z","shell.execute_reply":"2022-07-16T15:19:18.020619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"skew = scipy.stats.skew(train5.select_dtypes(np.number))\nskew_data = pd.DataFrame(skew.reshape(1,-1), columns=train5.select_dtypes(np.number).columns)\nskew_data = skew_data.T\nskew_data = skew_data.rename(columns={0: 'skew'})\nskew_data['skew_abs'] = skew_data.apply(abs)\nskew_data = skew_data[skew_data['skew_abs'] > 0.5]","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:18.023561Z","iopub.execute_input":"2022-07-16T15:19:18.024138Z","iopub.status.idle":"2022-07-16T15:19:18.044746Z","shell.execute_reply.started":"2022-07-16T15:19:18.024090Z","shell.execute_reply":"2022-07-16T15:19:18.043596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"skew_data","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:18.046732Z","iopub.execute_input":"2022-07-16T15:19:18.047297Z","iopub.status.idle":"2022-07-16T15:19:18.064111Z","shell.execute_reply.started":"2022-07-16T15:19:18.047247Z","shell.execute_reply":"2022-07-16T15:19:18.063112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train5[skew_data.index].describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:18.065822Z","iopub.execute_input":"2022-07-16T15:19:18.066399Z","iopub.status.idle":"2022-07-16T15:19:18.188131Z","shell.execute_reply.started":"2022-07-16T15:19:18.066350Z","shell.execute_reply":"2022-07-16T15:19:18.187066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pt = PowerTransformer()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:18.189833Z","iopub.execute_input":"2022-07-16T15:19:18.190368Z","iopub.status.idle":"2022-07-16T15:19:18.195347Z","shell.execute_reply.started":"2022-07-16T15:19:18.190318Z","shell.execute_reply":"2022-07-16T15:19:18.194335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transformed = pt.fit_transform(train5[skew_data.index])","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:18.197194Z","iopub.execute_input":"2022-07-16T15:19:18.197736Z","iopub.status.idle":"2022-07-16T15:19:18.415959Z","shell.execute_reply.started":"2022-07-16T15:19:18.197686Z","shell.execute_reply":"2022-07-16T15:19:18.414936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"skew_fix = pd.DataFrame(transformed, columns=skew_data.index, index=train4.index)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:18.417445Z","iopub.execute_input":"2022-07-16T15:19:18.417804Z","iopub.status.idle":"2022-07-16T15:19:18.423635Z","shell.execute_reply.started":"2022-07-16T15:19:18.417756Z","shell.execute_reply":"2022-07-16T15:19:18.422664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"skew_fix","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:18.431147Z","iopub.execute_input":"2022-07-16T15:19:18.432413Z","iopub.status.idle":"2022-07-16T15:19:18.492599Z","shell.execute_reply.started":"2022-07-16T15:19:18.432356Z","shell.execute_reply":"2022-07-16T15:19:18.491696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"no_skew_columns = train5.drop(skew_data.index, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:18.493975Z","iopub.execute_input":"2022-07-16T15:19:18.494890Z","iopub.status.idle":"2022-07-16T15:19:18.502029Z","shell.execute_reply.started":"2022-07-16T15:19:18.494849Z","shell.execute_reply":"2022-07-16T15:19:18.501010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"no_skew_columns.shape, train5.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:18.503349Z","iopub.execute_input":"2022-07-16T15:19:18.504106Z","iopub.status.idle":"2022-07-16T15:19:18.514969Z","shell.execute_reply.started":"2022-07-16T15:19:18.504021Z","shell.execute_reply":"2022-07-16T15:19:18.514240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train6 = pd.concat([skew_fix, no_skew_columns], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:18.516361Z","iopub.execute_input":"2022-07-16T15:19:18.516731Z","iopub.status.idle":"2022-07-16T15:19:18.527068Z","shell.execute_reply.started":"2022-07-16T15:19:18.516688Z","shell.execute_reply":"2022-07-16T15:19:18.526023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train6","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:18.528272Z","iopub.execute_input":"2022-07-16T15:19:18.528673Z","iopub.status.idle":"2022-07-16T15:19:18.657935Z","shell.execute_reply.started":"2022-07-16T15:19:18.528639Z","shell.execute_reply":"2022-07-16T15:19:18.656960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Cyclical transormation for moth representing data","metadata":{}},{"cell_type":"code","source":"train6['MoSold']","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:18.659348Z","iopub.execute_input":"2022-07-16T15:19:18.660167Z","iopub.status.idle":"2022-07-16T15:19:18.670356Z","shell.execute_reply.started":"2022-07-16T15:19:18.660116Z","shell.execute_reply":"2022-07-16T15:19:18.669303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def cos_transformer():\n    return FunctionTransformer(lambda x: -np.cos(x))","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:18.671804Z","iopub.execute_input":"2022-07-16T15:19:18.672760Z","iopub.status.idle":"2022-07-16T15:19:18.679957Z","shell.execute_reply.started":"2022-07-16T15:19:18.672705Z","shell.execute_reply":"2022-07-16T15:19:18.678984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"month_cos = cos_transformer().fit_transform(train5['MoSold'])","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:18.681530Z","iopub.execute_input":"2022-07-16T15:19:18.682403Z","iopub.status.idle":"2022-07-16T15:19:18.694608Z","shell.execute_reply.started":"2022-07-16T15:19:18.682354Z","shell.execute_reply":"2022-07-16T15:19:18.693272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"month_cos","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:18.695963Z","iopub.execute_input":"2022-07-16T15:19:18.696845Z","iopub.status.idle":"2022-07-16T15:19:18.707283Z","shell.execute_reply.started":"2022-07-16T15:19:18.696795Z","shell.execute_reply":"2022-07-16T15:19:18.706218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train7 = train6.copy()\ntrain7['MoSold'] = month_cos","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:18.708633Z","iopub.execute_input":"2022-07-16T15:19:18.709770Z","iopub.status.idle":"2022-07-16T15:19:18.717666Z","shell.execute_reply.started":"2022-07-16T15:19:18.709719Z","shell.execute_reply":"2022-07-16T15:19:18.716457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train7","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:18.719262Z","iopub.execute_input":"2022-07-16T15:19:18.720299Z","iopub.status.idle":"2022-07-16T15:19:18.826707Z","shell.execute_reply.started":"2022-07-16T15:19:18.720206Z","shell.execute_reply":"2022-07-16T15:19:18.825715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Encode categorical ","metadata":{}},{"cell_type":"code","source":"train8 = train7.copy()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:18.828219Z","iopub.execute_input":"2022-07-16T15:19:18.829078Z","iopub.status.idle":"2022-07-16T15:19:18.834626Z","shell.execute_reply.started":"2022-07-16T15:19:18.829005Z","shell.execute_reply":"2022-07-16T15:19:18.833569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train8 = pd.get_dummies(train7)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:18.836413Z","iopub.execute_input":"2022-07-16T15:19:18.836977Z","iopub.status.idle":"2022-07-16T15:19:18.900823Z","shell.execute_reply.started":"2022-07-16T15:19:18.836925Z","shell.execute_reply":"2022-07-16T15:19:18.899879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train8","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:18.902474Z","iopub.execute_input":"2022-07-16T15:19:18.902932Z","iopub.status.idle":"2022-07-16T15:19:19.122667Z","shell.execute_reply.started":"2022-07-16T15:19:18.902881Z","shell.execute_reply":"2022-07-16T15:19:19.121876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Scaling","metadata":{}},{"cell_type":"code","source":"st = StandardScaler()\ntrain_scaled = st.fit_transform(train8)\ntrain9 = pd.DataFrame(train_scaled, columns=train8.columns, index=train4.index)\ntrain9","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:19.123658Z","iopub.execute_input":"2022-07-16T15:19:19.123911Z","iopub.status.idle":"2022-07-16T15:19:19.513603Z","shell.execute_reply.started":"2022-07-16T15:19:19.123878Z","shell.execute_reply":"2022-07-16T15:19:19.512437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Target transformation","metadata":{}},{"cell_type":"code","source":"\npt_price = PowerTransformer()\nprice_transform = pt_price.fit_transform(target.values.reshape(-1, 1))","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:19.515013Z","iopub.execute_input":"2022-07-16T15:19:19.515324Z","iopub.status.idle":"2022-07-16T15:19:19.528488Z","shell.execute_reply.started":"2022-07-16T15:19:19.515288Z","shell.execute_reply":"2022-07-16T15:19:19.527731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 10))\n\nplt.subplot(1, 2, 1)\nsns.distplot(target, kde=True, fit=scipy.stats.norm)\nplt.title(\"Without PowerTransformer\")\n\nplt.subplot(1, 2, 2)\nsns.distplot(price_transform, kde=True, fit=scipy.stats.norm)\nplt.xlabel(\"Log SalePrice\")\nplt.title(\"With PowerTransformer\")\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:19.529761Z","iopub.execute_input":"2022-07-16T15:19:19.530992Z","iopub.status.idle":"2022-07-16T15:19:20.305247Z","shell.execute_reply.started":"2022-07-16T15:19:19.530940Z","shell.execute_reply":"2022-07-16T15:19:20.304038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# model selection","metadata":{}},{"cell_type":"code","source":"train_final = train9.iloc[:data0.index.max() + 1, :].copy()\ntrain_final['SalePrice'] = price_transform\ntest_final = train9.iloc[data0.index.max() + 1:, :].copy()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:20.306771Z","iopub.execute_input":"2022-07-16T15:19:20.307022Z","iopub.status.idle":"2022-07-16T15:19:20.316935Z","shell.execute_reply.started":"2022-07-16T15:19:20.306989Z","shell.execute_reply":"2022-07-16T15:19:20.315852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Here i am basically removing all the outlier values,\n#you can spot them by plotting saccaterplot with y = SalePrice\ntrain_final = train_final.drop([1298, 691, 1182, 635, 1298, 185, 1268, 608, 583, 190, 218,\n                                304, 747, 1061, 1298, 1190, 581, 691, 1182, 747, 420, 1350,\n                                1190, 1340, 691, 1182, 1298, 523, 1298, 523, 1328, 939, 1174,\n                                417, 1244, 1059, 546, 181, 563, 497, 1263, 1301, 1343, 799,\n                                1147, 104, 868, 852, 966, 1396, 313, 1423, 1328, 691, 1182,\n                                1169])","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:20.317918Z","iopub.execute_input":"2022-07-16T15:19:20.318282Z","iopub.status.idle":"2022-07-16T15:19:20.335183Z","shell.execute_reply.started":"2022-07-16T15:19:20.318248Z","shell.execute_reply":"2022-07-16T15:19:20.334339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train_final.drop(['SalePrice'], axis=1)\ny = train_final['SalePrice']","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:20.336454Z","iopub.execute_input":"2022-07-16T15:19:20.337251Z","iopub.status.idle":"2022-07-16T15:19:20.346747Z","shell.execute_reply.started":"2022-07-16T15:19:20.337191Z","shell.execute_reply":"2022-07-16T15:19:20.345933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_final","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:20.350425Z","iopub.execute_input":"2022-07-16T15:19:20.351361Z","iopub.status.idle":"2022-07-16T15:19:20.722641Z","shell.execute_reply.started":"2022-07-16T15:19:20.351299Z","shell.execute_reply":"2022-07-16T15:19:20.721649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_final","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:20.724029Z","iopub.execute_input":"2022-07-16T15:19:20.724945Z","iopub.status.idle":"2022-07-16T15:19:21.084346Z","shell.execute_reply.started":"2022-07-16T15:19:20.724895Z","shell.execute_reply":"2022-07-16T15:19:21.083593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:21.085338Z","iopub.execute_input":"2022-07-16T15:19:21.086321Z","iopub.status.idle":"2022-07-16T15:19:21.094967Z","shell.execute_reply.started":"2022-07-16T15:19:21.086283Z","shell.execute_reply":"2022-07-16T15:19:21.093889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# model selection","metadata":{}},{"cell_type":"code","source":"models = {\n    \"catboost\": CatBoostRegressor(verbose=0),\n    \"br\": BayesianRidge(),\n    \"lightgbm\": LGBMRegressor(),\n    \"ridge\": Ridge(),\n    \"omp\": OrthogonalMatchingPursuit(normalize=False),\n    \"xg\": XGBRegressor(),\n    \"grb\": GradientBoostingRegressor(),\n    \"rfr\": RandomForestRegressor(),\n    'SGD': SGDRegressor(),\n    \"svr_rbf\": SVR(kernel=\"rbf\"),\n    \"svr_lin\": SVR(kernel=\"linear\"),\n    \"svr_poly\": SVR(kernel=\"poly\")  \n}","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:21.096189Z","iopub.execute_input":"2022-07-16T15:19:21.096703Z","iopub.status.idle":"2022-07-16T15:19:21.112986Z","shell.execute_reply.started":"2022-07-16T15:19:21.096668Z","shell.execute_reply":"2022-07-16T15:19:21.111834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for name, model in models.items():\n    model.fit(X_train, y_train)\n    pred = model.predict(X_test)\n    result = mean_squared_error(y_test, pred, squared=False)\n    print(f'{name}: {np.exp(np.mean(result))}')","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:21.114143Z","iopub.execute_input":"2022-07-16T15:19:21.114707Z","iopub.status.idle":"2022-07-16T15:19:50.803864Z","shell.execute_reply.started":"2022-07-16T15:19:21.114661Z","shell.execute_reply":"2022-07-16T15:19:50.802785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nmodel = OrthogonalMatchingPursuit(normalize=False)\nmodel.fit(X_train, y_train)\n\npred = model.predict(test_final)\nactual_pred = pt_price.inverse_transform(pred.reshape(-1, 1))\nsubmission = pd.concat([test0['Id'], pd.DataFrame(actual_pred, columns=['SalePrice'])], axis=1)\nsubmission.to_csv('./submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T15:19:50.805628Z","iopub.execute_input":"2022-07-16T15:19:50.806265Z","iopub.status.idle":"2022-07-16T15:19:50.858288Z","shell.execute_reply.started":"2022-07-16T15:19:50.806211Z","shell.execute_reply":"2022-07-16T15:19:50.857055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}