{"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":"2022-07-14T06:55:58.267774Z","iopub.execute_input":"2022-07-14T06:55:58.268990Z","iopub.status.idle":"2022-07-14T06:55:58.283236Z","shell.execute_reply.started":"2022-07-14T06:55:58.268951Z","shell.execute_reply":"2022-07-14T06:55:58.281952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##importing libraries\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-07-14T06:55:58.284769Z","iopub.execute_input":"2022-07-14T06:55:58.285561Z","iopub.status.idle":"2022-07-14T06:55:58.295111Z","shell.execute_reply.started":"2022-07-14T06:55:58.285526Z","shell.execute_reply":"2022-07-14T06:55:58.294094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2022-07-14T06:55:58.302149Z","iopub.execute_input":"2022-07-14T06:55:58.302609Z","iopub.status.idle":"2022-07-14T06:55:58.308433Z","shell.execute_reply.started":"2022-07-14T06:55:58.302574Z","shell.execute_reply":"2022-07-14T06:55:58.307258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Loading dataset\ntrain=pd.read_csv('../input/house-prices-advanced-regression-techniques/train.csv')\ntest_df=pd.read_csv('../input/house-prices-advanced-regression-techniques/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-14T06:55:58.311173Z","iopub.execute_input":"2022-07-14T06:55:58.311688Z","iopub.status.idle":"2022-07-14T06:55:58.366655Z","shell.execute_reply.started":"2022-07-14T06:55:58.311654Z","shell.execute_reply":"2022-07-14T06:55:58.365050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T06:55:58.368810Z","iopub.execute_input":"2022-07-14T06:55:58.369582Z","iopub.status.idle":"2022-07-14T06:55:58.406771Z","shell.execute_reply.started":"2022-07-14T06:55:58.369535Z","shell.execute_reply":"2022-07-14T06:55:58.405434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T06:55:58.408600Z","iopub.execute_input":"2022-07-14T06:55:58.409499Z","iopub.status.idle":"2022-07-14T06:55:58.437213Z","shell.execute_reply.started":"2022-07-14T06:55:58.409446Z","shell.execute_reply":"2022-07-14T06:55:58.435991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Id = test_df['Id']\nId","metadata":{"execution":{"iopub.status.busy":"2022-07-14T06:55:58.438748Z","iopub.execute_input":"2022-07-14T06:55:58.439945Z","iopub.status.idle":"2022-07-14T06:55:58.449454Z","shell.execute_reply.started":"2022-07-14T06:55:58.439907Z","shell.execute_reply":"2022-07-14T06:55:58.447844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T06:55:58.450699Z","iopub.execute_input":"2022-07-14T06:55:58.451845Z","iopub.status.idle":"2022-07-14T06:55:58.569227Z","shell.execute_reply.started":"2022-07-14T06:55:58.451812Z","shell.execute_reply":"2022-07-14T06:55:58.567773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"###Check for null Values\nnull_columns = train.isnull().sum().sort_values(ascending = False)\nnull_columns.head(25)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-14T06:55:58.570975Z","iopub.execute_input":"2022-07-14T06:55:58.571342Z","iopub.status.idle":"2022-07-14T06:55:58.591156Z","shell.execute_reply.started":"2022-07-14T06:55:58.571310Z","shell.execute_reply":"2022-07-14T06:55:58.589714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-14T06:55:58.592841Z","iopub.execute_input":"2022-07-14T06:55:58.593842Z","iopub.status.idle":"2022-07-14T06:55:58.601397Z","shell.execute_reply.started":"2022-07-14T06:55:58.593793Z","shell.execute_reply":"2022-07-14T06:55:58.600393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-14T06:55:58.602979Z","iopub.execute_input":"2022-07-14T06:55:58.603335Z","iopub.status.idle":"2022-07-14T06:55:58.615739Z","shell.execute_reply.started":"2022-07-14T06:55:58.603304Z","shell.execute_reply":"2022-07-14T06:55:58.614410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#drop columns whose null values are too large(almost equal to no of rows) \ndrop_columns = (null_columns.head(5).index).tolist()\ndrop_columns\ntrain.drop(drop_columns, axis = 1, inplace = True)\ntest_df.drop(drop_columns, axis = 1, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T06:55:58.617504Z","iopub.execute_input":"2022-07-14T06:55:58.618556Z","iopub.status.idle":"2022-07-14T06:55:58.630434Z","shell.execute_reply.started":"2022-07-14T06:55:58.618492Z","shell.execute_reply":"2022-07-14T06:55:58.629304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Replace the columns of null values with it's mean\ntrain['LotFrontage'].fillna(train['LotFrontage'].mean(), inplace = True)\ntrain['GarageYrBlt'].fillna(train['GarageYrBlt'].mean(), inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T06:55:58.631980Z","iopub.execute_input":"2022-07-14T06:55:58.632589Z","iopub.status.idle":"2022-07-14T06:55:58.645471Z","shell.execute_reply.started":"2022-07-14T06:55:58.632554Z","shell.execute_reply":"2022-07-14T06:55:58.644305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Check if any null's are present in the dataset\ntrain.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T06:55:58.649949Z","iopub.execute_input":"2022-07-14T06:55:58.650756Z","iopub.status.idle":"2022-07-14T06:55:58.671117Z","shell.execute_reply.started":"2022-07-14T06:55:58.650718Z","shell.execute_reply":"2022-07-14T06:55:58.670229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##Check for correlation between the target variable and other variables of the data set\ncorr_train = train.corr().sort_values(by='SalePrice', ascending = False).round(2)\nprint(corr_train.SalePrice)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T06:55:58.672173Z","iopub.execute_input":"2022-07-14T06:55:58.672831Z","iopub.status.idle":"2022-07-14T06:55:58.696559Z","shell.execute_reply.started":"2022-07-14T06:55:58.672792Z","shell.execute_reply":"2022-07-14T06:55:58.695611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##Plot heatmap for correlation between variables\nplt.figure(figsize=(15,5))\nsns.heatmap(corr_train.corr())","metadata":{"execution":{"iopub.status.busy":"2022-07-14T06:55:58.697604Z","iopub.execute_input":"2022-07-14T06:55:58.698668Z","iopub.status.idle":"2022-07-14T06:55:59.458491Z","shell.execute_reply.started":"2022-07-14T06:55:58.698631Z","shell.execute_reply":"2022-07-14T06:55:59.457155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#List the features to be considered for model building based on correlation\ntrain_features = ['SalePrice', 'GrLivArea', 'GarageCars', 'GarageArea', 'TotalBsmtSF', '1stFlrSF', 'FullBath', 'TotRmsAbvGrd', 'YearBuilt', 'YearRemodAdd', 'Fireplaces', 'BsmtFinSF1', 'WoodDeckSF', '2ndFlrSF', 'OpenPorchSF', 'HalfBath', 'LotArea', 'BsmtFullBath', 'BsmtUnfSF', 'BedroomAbvGr']\ntest_features = ['GrLivArea', 'GarageCars', 'GarageArea', 'TotalBsmtSF', '1stFlrSF', 'FullBath', 'TotRmsAbvGrd', 'YearBuilt', 'YearRemodAdd', 'Fireplaces', 'BsmtFinSF1', 'WoodDeckSF', '2ndFlrSF', 'OpenPorchSF', 'HalfBath', 'LotArea', 'BsmtFullBath', 'BsmtUnfSF', 'BedroomAbvGr']","metadata":{"execution":{"iopub.status.busy":"2022-07-14T06:55:59.459883Z","iopub.execute_input":"2022-07-14T06:55:59.460225Z","iopub.status.idle":"2022-07-14T06:55:59.467987Z","shell.execute_reply.started":"2022-07-14T06:55:59.460195Z","shell.execute_reply":"2022-07-14T06:55:59.466442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train[train_features]\ntest = test_df[test_features]\ntrain.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T06:55:59.469453Z","iopub.execute_input":"2022-07-14T06:55:59.469789Z","iopub.status.idle":"2022-07-14T06:55:59.493255Z","shell.execute_reply.started":"2022-07-14T06:55:59.469758Z","shell.execute_reply":"2022-07-14T06:55:59.492050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"###Check for Outliers using percentiles\ntrain.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T06:55:59.494641Z","iopub.execute_input":"2022-07-14T06:55:59.495475Z","iopub.status.idle":"2022-07-14T06:55:59.569313Z","shell.execute_reply.started":"2022-07-14T06:55:59.495414Z","shell.execute_reply":"2022-07-14T06:55:59.568177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T06:55:59.570966Z","iopub.execute_input":"2022-07-14T06:55:59.571305Z","iopub.status.idle":"2022-07-14T06:55:59.586845Z","shell.execute_reply.started":"2022-07-14T06:55:59.571275Z","shell.execute_reply":"2022-07-14T06:55:59.585273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"####Check for Outliers using PairPlot againist SalePrice(target variable)\nsns.pairplot(train, x_vars = ['GrLivArea', 'GarageArea', 'LotArea'], y_vars = 'SalePrice', height=8, aspect =1, kind = 'scatter')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T06:55:59.588914Z","iopub.execute_input":"2022-07-14T06:55:59.589702Z","iopub.status.idle":"2022-07-14T06:56:00.282373Z","shell.execute_reply.started":"2022-07-14T06:55:59.589643Z","shell.execute_reply":"2022-07-14T06:56:00.281488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"###Drop outliers(which affects in prediction and model building)\ntrain_df = train.drop(train[train['GrLivArea']>4000].index, 0)\n#rows_2_drop=df_train[df_train['GrLivArea']>4000].index\ntrain_df = train.drop(train[train['GarageArea']>1200].index, 0)\ntrain_df = train.drop(train[train['LotArea']>50000].index, 0)\n###Again draw pairplot\nsns.pairplot(train, x_vars = ['GrLivArea', 'GarageArea', 'LotArea'], y_vars = 'SalePrice', height=8, aspect =1, kind = 'scatter')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T06:56:00.283585Z","iopub.execute_input":"2022-07-14T06:56:00.284118Z","iopub.status.idle":"2022-07-14T06:56:00.971931Z","shell.execute_reply.started":"2022-07-14T06:56:00.284072Z","shell.execute_reply":"2022-07-14T06:56:00.970763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isnull().sum()\ntest['GarageCars'].fillna(test['GarageCars'].mean(), inplace = True)\ntest['GarageArea'].fillna(test['GarageArea'].mean(), inplace = True)\ntest['TotalBsmtSF'].fillna(test['TotalBsmtSF'].mean(), inplace = True)\ntest['BsmtFinSF1'].fillna(test['BsmtFinSF1'].mean(), inplace = True)\ntest['BsmtFullBath'].fillna(test['BsmtFullBath'].mean(), inplace = True)\ntest['BsmtUnfSF'].fillna(test['BsmtUnfSF'].mean(), inplace = True)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-14T06:56:00.973651Z","iopub.execute_input":"2022-07-14T06:56:00.974350Z","iopub.status.idle":"2022-07-14T06:56:00.989476Z","shell.execute_reply.started":"2022-07-14T06:56:00.974307Z","shell.execute_reply":"2022-07-14T06:56:00.987894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T06:56:00.990792Z","iopub.execute_input":"2022-07-14T06:56:00.991692Z","iopub.status.idle":"2022-07-14T06:56:01.007124Z","shell.execute_reply.started":"2022-07-14T06:56:00.991655Z","shell.execute_reply":"2022-07-14T06:56:01.005541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"###Data preprocessing- Data standardization\nfrom sklearn.preprocessing import StandardScaler\nx_train = train.iloc[:,1:]\ny_train = train['SalePrice']\nx_test = test\nsc = StandardScaler()\nx_train_scaled = sc.fit_transform(x_train)\nx_test_scaled = sc.fit_transform(x_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T06:56:01.008664Z","iopub.execute_input":"2022-07-14T06:56:01.009640Z","iopub.status.idle":"2022-07-14T06:56:01.027334Z","shell.execute_reply.started":"2022-07-14T06:56:01.009602Z","shell.execute_reply":"2022-07-14T06:56:01.026041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"###LinearRegression\nfrom sklearn.linear_model import LinearRegression\nlm = LinearRegression()\nlm.fit(x_train_scaled, y_train)\nlm.score(x_train_scaled, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T06:56:01.029057Z","iopub.execute_input":"2022-07-14T06:56:01.029649Z","iopub.status.idle":"2022-07-14T06:56:01.069024Z","shell.execute_reply.started":"2022-07-14T06:56:01.029614Z","shell.execute_reply":"2022-07-14T06:56:01.066694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"####XGBoosT\nfrom xgboost import XGBRegressor\nXGBR = XGBRegressor(n_estimators=100, learning_rate=0.05, n_jobs=5)\nXGBR.fit(x_train_scaled, y_train, early_stopping_rounds=5, eval_set=[(x_train_scaled, y_train)], verbose=False)\nprint(XGBR.score(x_train_scaled,y_train))","metadata":{"execution":{"iopub.status.busy":"2022-07-14T06:56:01.071286Z","iopub.execute_input":"2022-07-14T06:56:01.074576Z","iopub.status.idle":"2022-07-14T06:56:02.464670Z","shell.execute_reply.started":"2022-07-14T06:56:01.074517Z","shell.execute_reply":"2022-07-14T06:56:02.463398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##RandomForest\nfrom sklearn.ensemble import RandomForestClassifier\nclassifier = RandomForestClassifier(n_estimators=100)\nclassifier.fit(x_train_scaled, y_train)\npreds = classifier.predict(x_train_scaled)\nprint(classifier.score(x_train_scaled,y_train))","metadata":{"execution":{"iopub.status.busy":"2022-07-14T06:56:02.466138Z","iopub.execute_input":"2022-07-14T06:56:02.466515Z","iopub.status.idle":"2022-07-14T06:56:08.992930Z","shell.execute_reply.started":"2022-07-14T06:56:02.466482Z","shell.execute_reply":"2022-07-14T06:56:08.991564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = classifier.predict(x_test_scaled)\ndf = pd.DataFrame(columns = ['Id', 'SalePrice'])\ndf['SalePrice'] = predictions\ndf['Id'] = test_df.Id\ndf","metadata":{"execution":{"iopub.status.busy":"2022-07-14T06:56:08.994409Z","iopub.execute_input":"2022-07-14T06:56:08.994749Z","iopub.status.idle":"2022-07-14T06:56:09.530389Z","shell.execute_reply.started":"2022-07-14T06:56:08.994718Z","shell.execute_reply":"2022-07-14T06:56:09.529309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('submission.csv', index=False)\nprint('submission successful')","metadata":{"execution":{"iopub.status.busy":"2022-07-14T06:56:09.533177Z","iopub.execute_input":"2022-07-14T06:56:09.533651Z","iopub.status.idle":"2022-07-14T06:56:09.544832Z","shell.execute_reply.started":"2022-07-14T06:56:09.533604Z","shell.execute_reply":"2022-07-14T06:56:09.543872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}