{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-30T09:18:37.751962Z","iopub.execute_input":"2021-07-30T09:18:37.752329Z","iopub.status.idle":"2021-07-30T09:18:37.763547Z","shell.execute_reply.started":"2021-07-30T09:18:37.752298Z","shell.execute_reply":"2021-07-30T09:18:37.762417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"***Importing The Packages***","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2022-08-04T04:30:17.177287Z","iopub.execute_input":"2022-08-04T04:30:17.177962Z","iopub.status.idle":"2022-08-04T04:30:18.206660Z","shell.execute_reply.started":"2022-08-04T04:30:17.177839Z","shell.execute_reply":"2022-08-04T04:30:18.205588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"***Reading The Datasets***","metadata":{}},{"cell_type":"code","source":"dftrain=pd.read_csv(\"../input/house-prices-advanced-regression-techniques/train.csv\")\ndftest=pd.read_csv(\"../input/house-prices-advanced-regression-techniques/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-08-04T04:31:10.909019Z","iopub.execute_input":"2022-08-04T04:31:10.909386Z","iopub.status.idle":"2022-08-04T04:31:10.992807Z","shell.execute_reply.started":"2022-08-04T04:31:10.909353Z","shell.execute_reply":"2022-08-04T04:31:10.991707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftrain.columns","metadata":{"execution":{"iopub.status.busy":"2022-08-04T04:32:57.226699Z","iopub.execute_input":"2022-08-04T04:32:57.227093Z","iopub.status.idle":"2022-08-04T04:32:57.234931Z","shell.execute_reply.started":"2022-08-04T04:32:57.227044Z","shell.execute_reply":"2022-08-04T04:32:57.233615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for _ in dftrain.select_dtypes(exclude = ['float64','int64']).columns:\n    \n    print(_,\":\",dftrain[_].unique())","metadata":{"execution":{"iopub.status.busy":"2022-08-04T04:44:42.213638Z","iopub.execute_input":"2022-08-04T04:44:42.213995Z","iopub.status.idle":"2022-08-04T04:44:42.265914Z","shell.execute_reply.started":"2022-08-04T04:44:42.213963Z","shell.execute_reply":"2022-08-04T04:44:42.264648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"***Understanding the data***","metadata":{}},{"cell_type":"code","source":"dftrain.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-04T04:33:11.817611Z","iopub.execute_input":"2022-08-04T04:33:11.818268Z","iopub.status.idle":"2022-08-04T04:33:11.851804Z","shell.execute_reply.started":"2022-08-04T04:33:11.818228Z","shell.execute_reply":"2022-08-04T04:33:11.850129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftrain.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-04T04:50:51.840582Z","iopub.execute_input":"2022-08-04T04:50:51.840933Z","iopub.status.idle":"2022-08-04T04:50:51.846804Z","shell.execute_reply.started":"2022-08-04T04:50:51.840901Z","shell.execute_reply":"2022-08-04T04:50:51.845967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"null=[]\nfor i in dftrain.columns:\n    if dftrain[i].isnull().sum()>0:\n        null.append(i)\nprint(null)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T05:05:42.277689Z","iopub.execute_input":"2022-08-04T05:05:42.278100Z","iopub.status.idle":"2022-08-04T05:05:42.308162Z","shell.execute_reply.started":"2022-08-04T05:05:42.278062Z","shell.execute_reply":"2022-08-04T05:05:42.307086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for p in null:\n    if dftrain[p].dtypes==\"object\":\n        dftrain[p].fillna('No',inplace=True)\n    else:\n        dftrain[p].fillna(0,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T05:16:38.331648Z","iopub.execute_input":"2022-08-04T05:16:38.332046Z","iopub.status.idle":"2022-08-04T05:16:38.347997Z","shell.execute_reply.started":"2022-08-04T05:16:38.332011Z","shell.execute_reply":"2022-08-04T05:16:38.346435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"null_test=[]\nfor i in dftest.columns:\n    if dftest[i].isnull().sum()>0:\n        null_test.append(i)\nfor p in null_test:\n    if dftest[p].dtypes==\"object\":\n        dftest[p].fillna('No',inplace=True)\n    else:\n        dftest[p].fillna(0,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T05:23:01.676482Z","iopub.execute_input":"2022-08-04T05:23:01.676938Z","iopub.status.idle":"2022-08-04T05:23:01.707975Z","shell.execute_reply.started":"2022-08-04T05:23:01.676894Z","shell.execute_reply":"2022-08-04T05:23:01.706731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftest.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-04T05:23:16.966201Z","iopub.execute_input":"2022-08-04T05:23:16.966702Z","iopub.status.idle":"2022-08-04T05:23:16.991010Z","shell.execute_reply.started":"2022-08-04T05:23:16.966657Z","shell.execute_reply":"2022-08-04T05:23:16.988407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Missing Values**","metadata":{}},{"cell_type":"code","source":"def missing(dftrain):\n    missing_value = dftrain.isnull().sum().sort_values(ascending=False)\n    missing_percentage= (dftrain.isnull().sum()*100/dftrain.isnull().count()).sort_values(ascending=False)\n    missing_total = pd.concat([missing_value,missing_percentage], axis=1, keys=['missing_value','missing_percentage'])\n    return missing_total\nmissing(dftrain)","metadata":{"execution":{"iopub.status.busy":"2021-08-20T19:58:49.434093Z","iopub.execute_input":"2021-08-20T19:58:49.434454Z","iopub.status.idle":"2021-08-20T19:58:49.492721Z","shell.execute_reply.started":"2021-08-20T19:58:49.434424Z","shell.execute_reply":"2021-08-20T19:58:49.491770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftrain.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2021-08-20T20:02:29.140097Z","iopub.execute_input":"2021-08-20T20:02:29.140433Z","iopub.status.idle":"2021-08-20T20:02:29.156595Z","shell.execute_reply.started":"2021-08-20T20:02:29.140404Z","shell.execute_reply":"2021-08-20T20:02:29.155569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in dftrain.columns:\n    if dftrain[col].dtype == 'object':\n        sns.boxplot(x=col, y='SalePrice', data=dftrain)\n        plt.title('Boxplot for {}'.format(col))\n        plt.show();","metadata":{"execution":{"iopub.status.busy":"2021-08-20T20:03:13.397408Z","iopub.execute_input":"2021-08-20T20:03:13.397770Z","iopub.status.idle":"2021-08-20T20:03:22.171981Z","shell.execute_reply.started":"2021-08-20T20:03:13.397737Z","shell.execute_reply":"2021-08-20T20:03:22.171027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in dftrain.columns:\n    if dftrain[col].dtype != 'object':\n        sns.scatterplot(x=col, y='SalePrice', data=dftrain)\n        plt.title('Scatterplot for {}'.format(col))\n        plt.show();","metadata":{"execution":{"iopub.status.busy":"2021-08-20T20:03:46.342910Z","iopub.execute_input":"2021-08-20T20:03:46.343276Z","iopub.status.idle":"2021-08-20T20:03:53.016848Z","shell.execute_reply.started":"2021-08-20T20:03:46.343242Z","shell.execute_reply":"2021-08-20T20:03:53.015809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftrain.info()","metadata":{"execution":{"iopub.status.busy":"2021-08-20T20:04:00.322268Z","iopub.execute_input":"2021-08-20T20:04:00.322639Z","iopub.status.idle":"2021-08-20T20:04:00.345666Z","shell.execute_reply.started":"2021-08-20T20:04:00.322608Z","shell.execute_reply":"2021-08-20T20:04:00.344933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for _ in dftrain.columns:\n    if dftrain[_].dtypes==\"object\":\n        print(_,\":\\n\",dftrain[_].value_counts(),\"\\n\")","metadata":{"execution":{"iopub.status.busy":"2022-08-04T05:47:17.808900Z","iopub.execute_input":"2022-08-04T05:47:17.809315Z","iopub.status.idle":"2022-08-04T05:47:17.905136Z","shell.execute_reply.started":"2022-08-04T05:47:17.809277Z","shell.execute_reply":"2022-08-04T05:47:17.903727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftrain.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T05:52:55.939460Z","iopub.execute_input":"2022-08-04T05:52:55.939838Z","iopub.status.idle":"2022-08-04T05:52:55.970718Z","shell.execute_reply.started":"2022-08-04T05:52:55.939805Z","shell.execute_reply":"2022-08-04T05:52:55.969702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Creating Some Extra column**","metadata":{}},{"cell_type":"code","source":"dftrain[\"GarageYrBltold\"]=dftrain[\"YrSold\"]-dftrain[\"GarageYrBlt\"]\ndftrain[\"YearBuiltold\"]=dftrain[\"YrSold\"]-dftrain[\"YearBuilt\"]\ndftrain[\"YearRemodAddold\"]=dftrain[\"YrSold\"]-dftrain[\"YearRemodAdd\"]","metadata":{"execution":{"iopub.status.busy":"2022-08-04T05:54:07.699070Z","iopub.execute_input":"2022-08-04T05:54:07.699579Z","iopub.status.idle":"2022-08-04T05:54:07.736193Z","shell.execute_reply.started":"2022-08-04T05:54:07.699531Z","shell.execute_reply":"2022-08-04T05:54:07.735161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Dropping The Needless columns**","metadata":{}},{"cell_type":"code","source":"dftrain.drop([\"Id\"],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T06:04:11.640609Z","iopub.execute_input":"2022-08-04T06:04:11.641026Z","iopub.status.idle":"2022-08-04T06:04:11.677195Z","shell.execute_reply.started":"2022-08-04T06:04:11.640991Z","shell.execute_reply":"2022-08-04T06:04:11.675630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftr=pd.get_dummies(dftrain,prefix_sep='_',drop_first=True)\nprint(dftr.shape)\nprint(dftr.columns)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T06:06:56.038174Z","iopub.execute_input":"2022-08-04T06:06:56.038590Z","iopub.status.idle":"2022-08-04T06:06:56.101569Z","shell.execute_reply.started":"2022-08-04T06:06:56.038552Z","shell.execute_reply":"2022-08-04T06:06:56.099992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftest[\"GarageYrBltold\"]=dftest[\"YrSold\"]-dftest[\"GarageYrBlt\"]\ndftest[\"YearBuiltold\"]=dftest[\"YrSold\"]-dftest[\"YearBuilt\"]\ndftest[\"YearRemodAddold\"]=dftest[\"YrSold\"]-dftest[\"YearRemodAdd\"]\ndftest.drop([\"Id\"],axis=1,inplace=True)\ndfte=pd.get_dummies(dftest,prefix_sep='_',drop_first=True)\nprint(dfte.shape)\nprint(dfte.columns)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T06:28:22.649946Z","iopub.execute_input":"2022-08-04T06:28:22.650383Z","iopub.status.idle":"2022-08-04T06:28:22.721804Z","shell.execute_reply.started":"2022-08-04T06:28:22.650303Z","shell.execute_reply":"2022-08-04T06:28:22.720653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfte[\"Utilities_NoSeWa\"]=0\ndfte[\"Condition2_RRAe\"]=0\ndfte[\"Condition2_RRAn\"]=0\ndfte[\"Condition2_RRNn\"]=0\ndfte[\"HouseStyle_2.5Fin\"]=0\ndfte[\"RoofMatl_ClyTile\"]=0\ndfte[\"RoofMatl_Membran\"]=0\ndfte[\"RoofMatl_Metal\"]=0\ndfte[\"RoofMatl_Roll\"]=0\ndfte[\"Exterior1st_ImStucc\"]=0\ndfte[\"Exterior1st_Stone\"]=0\ndfte[\"Exterior2nd_Other\"]=0\ndfte[\"Heating_Floor\"]=0\ndfte[\"Heating_OthW\"]=0\ndfte[\"Electrical_Mix\"]=0\ndfte[\"GarageQual_Ex\"]=0","metadata":{"execution":{"iopub.status.busy":"2022-08-04T06:31:07.710782Z","iopub.execute_input":"2022-08-04T06:31:07.711394Z","iopub.status.idle":"2022-08-04T06:31:07.732681Z","shell.execute_reply.started":"2022-08-04T06:31:07.711352Z","shell.execute_reply":"2022-08-04T06:31:07.731518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfte.head(20)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T06:31:38.679478Z","iopub.execute_input":"2022-08-04T06:31:38.680101Z","iopub.status.idle":"2022-08-04T06:31:38.728090Z","shell.execute_reply.started":"2022-08-04T06:31:38.680059Z","shell.execute_reply":"2022-08-04T06:31:38.726865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_col=[]\nfor _ in dftr.columns:\n    if _==\"SalePrice\":\n        continue\n    else:\n        x_col.append(_)\ny=dftr[\"SalePrice\"]\nX=dftr[x_col]","metadata":{"execution":{"iopub.status.busy":"2022-08-04T07:24:28.670703Z","iopub.execute_input":"2022-08-04T07:24:28.671350Z","iopub.status.idle":"2022-08-04T07:24:28.682680Z","shell.execute_reply.started":"2022-08-04T07:24:28.671310Z","shell.execute_reply":"2022-08-04T07:24:28.681632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Some Important Packages for Advanced Regression**","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor\nfrom sklearn.model_selection import GridSearchCV\nfrom mlxtend.regressor import StackingCVRegressor\nfrom sklearn.linear_model import Ridge, Lasso\nfrom lightgbm import LGBMRegressor\nfrom xgboost import XGBRegressor\n\nridge = Ridge()\nlasso = Lasso()\nrf = RandomForestRegressor()\nlgbm = LGBMRegressor()\nxgb = XGBRegressor()","metadata":{"execution":{"iopub.status.busy":"2021-08-20T20:13:51.048787Z","iopub.execute_input":"2021-08-20T20:13:51.049314Z","iopub.status.idle":"2021-08-20T20:13:52.798683Z","shell.execute_reply.started":"2021-08-20T20:13:51.049279Z","shell.execute_reply":"2021-08-20T20:13:52.797678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stack = StackingCVRegressor(regressors = (ridge, lasso, rf, lgbm, xgb),\n                           meta_regressor = xgb, cv = 10,\n                           use_features_in_secondary = True,\n                           store_train_meta_features = True,\n                           shuffle = False,\n                           random_state = 42)\n\nstack.fit(xtr,ytr)","metadata":{"execution":{"iopub.status.busy":"2021-08-20T20:13:57.388872Z","iopub.execute_input":"2021-08-20T20:13:57.389200Z","iopub.status.idle":"2021-08-20T20:14:43.483842Z","shell.execute_reply.started":"2021-08-20T20:13:57.389172Z","shell.execute_reply":"2021-08-20T20:14:43.482891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred=stack.predict(xte)","metadata":{"execution":{"iopub.status.busy":"2021-08-20T20:14:43.485335Z","iopub.execute_input":"2021-08-20T20:14:43.485633Z","iopub.status.idle":"2021-08-20T20:14:43.650517Z","shell.execute_reply.started":"2021-08-20T20:14:43.485602Z","shell.execute_reply":"2021-08-20T20:14:43.649706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred","metadata":{"execution":{"iopub.status.busy":"2021-08-20T20:15:31.356302Z","iopub.execute_input":"2021-08-20T20:15:31.356652Z","iopub.status.idle":"2021-08-20T20:15:31.363150Z","shell.execute_reply.started":"2021-08-20T20:15:31.356623Z","shell.execute_reply":"2021-08-20T20:15:31.362220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"***Random Forest***","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import GridSearchCV\nmodb=RandomForestRegressor(n_estimators=500,min_samples_split=40,max_features=None)\nmodb=modb.fit(xtr,ytr)\nparamgrid=[{\"min_samples_split\":[2,3,5],\"n_estimators\":[550,600]}]\nsearch=GridSearchCV(modb,paramgrid,scoring=\"neg_mean_squared_error\",cv=3,verbose=True,n_jobs=-1)\nsearch=search.fit(xtr,ytr)","metadata":{"execution":{"iopub.status.busy":"2021-08-20T20:27:22.148083Z","iopub.execute_input":"2021-08-20T20:27:22.148580Z","iopub.status.idle":"2021-08-20T20:29:52.389156Z","shell.execute_reply.started":"2021-08-20T20:27:22.148548Z","shell.execute_reply":"2021-08-20T20:29:52.388238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"search.best_params_","metadata":{"execution":{"iopub.status.busy":"2021-08-20T20:29:52.390985Z","iopub.execute_input":"2021-08-20T20:29:52.391380Z","iopub.status.idle":"2021-08-20T20:29:52.397047Z","shell.execute_reply.started":"2021-08-20T20:29:52.391328Z","shell.execute_reply":"2021-08-20T20:29:52.396212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modb=RandomForestRegressor(n_estimators=550,min_samples_split=2,max_features=None)\nmodb=modb.fit(xtr,ytr)\nrsq=modb.score(xtr,ytr)\nrsq","metadata":{"execution":{"iopub.status.busy":"2021-08-20T20:30:22.404209Z","iopub.execute_input":"2021-08-20T20:30:22.404569Z","iopub.status.idle":"2021-08-20T20:30:40.328967Z","shell.execute_reply.started":"2021-08-20T20:30:22.404536Z","shell.execute_reply":"2021-08-20T20:30:40.327918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred=modb.predict(xte)\npred","metadata":{"execution":{"iopub.status.busy":"2021-08-20T20:30:43.690638Z","iopub.execute_input":"2021-08-20T20:30:43.691060Z","iopub.status.idle":"2021-08-20T20:30:43.855559Z","shell.execute_reply.started":"2021-08-20T20:30:43.691025Z","shell.execute_reply":"2021-08-20T20:30:43.854499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}