{"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)\nimport seaborn as sns\nimport matplotlib.pyplot as plt\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\n#Linear Regression Models from sklearn\nfrom sklearn.linear_model import LinearRegression,SGDRegressor,ElasticNet,BayesianRidge\nfrom lightgbm import LGBMRegressor\nfrom xgboost.sklearn import XGBRegressor\nfrom catboost import CatBoostRegressor\nfrom sklearn.kernel_ridge import KernelRidge\nfrom sklearn.ensemble import GradientBoostingRegressor\nfrom sklearn.svm import SVR\nfrom sklearn.model_selection import KFold,cross_val_score\n\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\npd.set_option('max_rows',1000)\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":"2022-06-24T19:54:41.138122Z","iopub.execute_input":"2022-06-24T19:54:41.138877Z","iopub.status.idle":"2022-06-24T19:54:43.670734Z","shell.execute_reply.started":"2022-06-24T19:54:41.138728Z","shell.execute_reply":"2022-06-24T19:54:43.669690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv')\nxtest=pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv')\nsample=pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-06-24T19:54:43.673266Z","iopub.execute_input":"2022-06-24T19:54:43.673768Z","iopub.status.idle":"2022-06-24T19:54:43.760097Z","shell.execute_reply.started":"2022-06-24T19:54:43.673720Z","shell.execute_reply":"2022-06-24T19:54:43.758567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=X['SalePrice']\nids=xtest['Id']\n","metadata":{"execution":{"iopub.status.busy":"2022-06-24T19:54:43.761933Z","iopub.execute_input":"2022-06-24T19:54:43.762849Z","iopub.status.idle":"2022-06-24T19:54:43.771012Z","shell.execute_reply.started":"2022-06-24T19:54:43.762812Z","shell.execute_reply":"2022-06-24T19:54:43.769942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-06-24T19:54:43.773562Z","iopub.execute_input":"2022-06-24T19:54:43.774241Z","iopub.status.idle":"2022-06-24T19:54:43.800291Z","shell.execute_reply.started":"2022-06-24T19:54:43.774204Z","shell.execute_reply":"2022-06-24T19:54:43.798835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(40,40))\ncorr=X.corr()\ndataplot = sns.heatmap(corr, cmap=\"YlGnBu\", annot=True)\n\n# displaying heatmap\n#figure = dataplot.get_figure()    \n#figure.savefig('corr_heatmap.png', dpi=400)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-06-24T19:54:43.801886Z","iopub.execute_input":"2022-06-24T19:54:43.802224Z","iopub.status.idle":"2022-06-24T19:54:50.280491Z","shell.execute_reply.started":"2022-06-24T19:54:43.802195Z","shell.execute_reply":"2022-06-24T19:54:50.278825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features=corr[corr['SalePrice']>0.3].index.tolist()\nfeatures=features[:-1]\nfeatures","metadata":{"execution":{"iopub.status.busy":"2022-06-24T19:54:50.281805Z","iopub.execute_input":"2022-06-24T19:54:50.282224Z","iopub.status.idle":"2022-06-24T19:54:50.296015Z","shell.execute_reply.started":"2022-06-24T19:54:50.282186Z","shell.execute_reply":"2022-06-24T19:54:50.294743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtrain=X[features]\nxtest=xtest[features]\nxtrain.isnull().sum()\n","metadata":{"execution":{"iopub.status.busy":"2022-06-24T19:54:50.298152Z","iopub.execute_input":"2022-06-24T19:54:50.299234Z","iopub.status.idle":"2022-06-24T19:54:50.328781Z","shell.execute_reply.started":"2022-06-24T19:54:50.299177Z","shell.execute_reply":"2022-06-24T19:54:50.327400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#LotFrontage null values to be replaced by mean, \n#as it has less outliers so we can impute accordingly\nplt.hist(xtrain['LotFrontage'],bins=20)","metadata":{"execution":{"iopub.status.busy":"2022-06-24T19:54:50.331160Z","iopub.execute_input":"2022-06-24T19:54:50.331664Z","iopub.status.idle":"2022-06-24T19:54:50.746008Z","shell.execute_reply.started":"2022-06-24T19:54:50.331608Z","shell.execute_reply":"2022-06-24T19:54:50.744857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtrain.loc[:]['LotFrontage'].fillna(xtrain['LotFrontage'].mean(),inplace=True)\nxtest.loc[:]['LotFrontage'].fillna(xtrain['LotFrontage'].mean(),inplace=True)\nxtrain.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-06-24T19:54:50.749932Z","iopub.execute_input":"2022-06-24T19:54:50.750319Z","iopub.status.idle":"2022-06-24T19:54:50.764997Z","shell.execute_reply.started":"2022-06-24T19:54:50.750285Z","shell.execute_reply":"2022-06-24T19:54:50.763446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Garage year built to be replaced by median as it has more outliers\nplt.hist(xtrain['GarageYrBlt'])","metadata":{"execution":{"iopub.status.busy":"2022-06-24T19:54:50.770011Z","iopub.execute_input":"2022-06-24T19:54:50.770743Z","iopub.status.idle":"2022-06-24T19:54:50.977075Z","shell.execute_reply.started":"2022-06-24T19:54:50.770687Z","shell.execute_reply":"2022-06-24T19:54:50.975882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtrain.loc[:]['GarageYrBlt'].fillna(xtrain['GarageYrBlt'].median(),inplace=True)\nxtest.loc[:]['GarageYrBlt'].fillna(xtrain['GarageYrBlt'].median(),inplace=True)\nxtrain.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-06-24T19:54:50.978355Z","iopub.execute_input":"2022-06-24T19:54:50.978716Z","iopub.status.idle":"2022-06-24T19:54:50.993347Z","shell.execute_reply.started":"2022-06-24T19:54:50.978685Z","shell.execute_reply":"2022-06-24T19:54:50.992536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtest.fillna(0,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-06-24T19:54:50.995221Z","iopub.execute_input":"2022-06-24T19:54:50.995974Z","iopub.status.idle":"2022-06-24T19:54:51.001954Z","shell.execute_reply.started":"2022-06-24T19:54:50.995928Z","shell.execute_reply":"2022-06-24T19:54:51.000834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Replacing the missing values with mode\nplt.hist(xtrain['MasVnrArea'],bins=20)\n","metadata":{"execution":{"iopub.status.busy":"2022-06-24T19:54:51.003994Z","iopub.execute_input":"2022-06-24T19:54:51.004872Z","iopub.status.idle":"2022-06-24T19:54:51.233712Z","shell.execute_reply.started":"2022-06-24T19:54:51.004819Z","shell.execute_reply":"2022-06-24T19:54:51.232788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtrain.loc[:]['MasVnrArea'].fillna(xtrain['MasVnrArea'].mode()[0],inplace=True)\nxtest.loc[:]['MasVnrArea'].fillna(xtrain['MasVnrArea'].mode()[0],inplace=True)\nxtrain.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-06-24T19:54:51.234801Z","iopub.execute_input":"2022-06-24T19:54:51.235687Z","iopub.status.idle":"2022-06-24T19:54:51.252581Z","shell.execute_reply.started":"2022-06-24T19:54:51.235652Z","shell.execute_reply":"2022-06-24T19:54:51.251182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cgb=CatBoostRegressor()\ncgb.fit(xtrain,y)","metadata":{"execution":{"iopub.status.busy":"2022-06-24T19:54:51.266919Z","iopub.execute_input":"2022-06-24T19:54:51.267518Z","iopub.status.idle":"2022-06-24T19:54:51.280035Z","shell.execute_reply.started":"2022-06-24T19:54:51.267473Z","shell.execute_reply":"2022-06-24T19:54:51.278828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.mean(cross_val_score(cgb, xtrain, y, cv=5, n_jobs=-1))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtest.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-06-24T19:54:51.585131Z","iopub.status.idle":"2022-06-24T19:54:51.585791Z","shell.execute_reply.started":"2022-06-24T19:54:51.585561Z","shell.execute_reply":"2022-06-24T19:54:51.585583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results=cgb.predict(xtest)\nresults=pd.DataFrame({'Id':ids,'SalePrice':results})\nresults.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-06-24T19:54:51.587347Z","iopub.status.idle":"2022-06-24T19:54:51.588006Z","shell.execute_reply.started":"2022-06-24T19:54:51.587768Z","shell.execute_reply":"2022-06-24T19:54:51.587792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}