{"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-08-11T11:53:07.065082Z","iopub.execute_input":"2022-08-11T11:53:07.065979Z","iopub.status.idle":"2022-08-11T11:53:07.096319Z","shell.execute_reply.started":"2022-08-11T11:53:07.065874Z","shell.execute_reply":"2022-08-11T11:53:07.095394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Training Dataset","metadata":{}},{"cell_type":"code","source":"train_dataset = pd.read_csv(\"/kaggle/input/house-prices-advanced-regression-techniques/train.csv\")\ntrain_dataset.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:53:07.160685Z","iopub.execute_input":"2022-08-11T11:53:07.161461Z","iopub.status.idle":"2022-08-11T11:53:07.234275Z","shell.execute_reply.started":"2022-08-11T11:53:07.161423Z","shell.execute_reply":"2022-08-11T11:53:07.233232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:53:07.235780Z","iopub.execute_input":"2022-08-11T11:53:07.236161Z","iopub.status.idle":"2022-08-11T11:53:07.243458Z","shell.execute_reply.started":"2022-08-11T11:53:07.236116Z","shell.execute_reply":"2022-08-11T11:53:07.242377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:53:07.287156Z","iopub.execute_input":"2022-08-11T11:53:07.287877Z","iopub.status.idle":"2022-08-11T11:53:07.321001Z","shell.execute_reply.started":"2022-08-11T11:53:07.287827Z","shell.execute_reply":"2022-08-11T11:53:07.319806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Check NaN values","metadata":{}},{"cell_type":"code","source":"NaN_1 = train_dataset.isnull().sum()\nprint(NaN_1[NaN_1 > 0])","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:53:07.369345Z","iopub.execute_input":"2022-08-11T11:53:07.370070Z","iopub.status.idle":"2022-08-11T11:53:07.382089Z","shell.execute_reply.started":"2022-08-11T11:53:07.370023Z","shell.execute_reply":"2022-08-11T11:53:07.381147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Drop unuseful columns","metadata":{}},{"cell_type":"code","source":"train_dataset = train_dataset.drop(['Id','Alley','PoolQC','Fence','MiscFeature','FireplaceQu'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:53:07.424703Z","iopub.execute_input":"2022-08-11T11:53:07.425720Z","iopub.status.idle":"2022-08-11T11:53:07.434762Z","shell.execute_reply.started":"2022-08-11T11:53:07.425683Z","shell.execute_reply":"2022-08-11T11:53:07.433574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Fill NaN values","metadata":{}},{"cell_type":"code","source":"selcted_col = np.array(['LotFrontage','MasVnrArea','GarageYrBlt'])\nfor col in selcted_col:\n    train_dataset[col] = train_dataset[col].fillna(train_dataset[col].mean())","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:53:07.487558Z","iopub.execute_input":"2022-08-11T11:53:07.488429Z","iopub.status.idle":"2022-08-11T11:53:07.496866Z","shell.execute_reply.started":"2022-08-11T11:53:07.488390Z","shell.execute_reply":"2022-08-11T11:53:07.495982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"selcted_col = np.array(['BsmtQual','BsmtCond','BsmtExposure','BsmtFinType1','BsmtFinType2','GarageType','GarageFinish','GarageQual','GarageCond','Electrical'])\nfor col in selcted_col:\n    train_dataset[col] =  train_dataset[col].fillna(train_dataset[col].mode()[0])","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:53:07.547460Z","iopub.execute_input":"2022-08-11T11:53:07.548728Z","iopub.status.idle":"2022-08-11T11:53:07.569926Z","shell.execute_reply.started":"2022-08-11T11:53:07.548676Z","shell.execute_reply":"2022-08-11T11:53:07.569013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.array(train_dataset.isnull().sum())","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:53:07.594862Z","iopub.execute_input":"2022-08-11T11:53:07.595303Z","iopub.status.idle":"2022-08-11T11:53:07.606928Z","shell.execute_reply.started":"2022-08-11T11:53:07.595263Z","shell.execute_reply":"2022-08-11T11:53:07.605962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:53:07.674763Z","iopub.execute_input":"2022-08-11T11:53:07.675181Z","iopub.status.idle":"2022-08-11T11:53:07.699921Z","shell.execute_reply.started":"2022-08-11T11:53:07.675146Z","shell.execute_reply":"2022-08-11T11:53:07.699096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Testing Dataset","metadata":{}},{"cell_type":"code","source":"test_dataset = pd.read_csv(\"/kaggle/input/house-prices-advanced-regression-techniques/test.csv\")\ntest_dataset.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:53:07.741511Z","iopub.execute_input":"2022-08-11T11:53:07.742612Z","iopub.status.idle":"2022-08-11T11:53:07.808086Z","shell.execute_reply.started":"2022-08-11T11:53:07.742563Z","shell.execute_reply":"2022-08-11T11:53:07.806884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:53:07.810184Z","iopub.execute_input":"2022-08-11T11:53:07.810875Z","iopub.status.idle":"2022-08-11T11:53:07.818332Z","shell.execute_reply.started":"2022-08-11T11:53:07.810833Z","shell.execute_reply":"2022-08-11T11:53:07.817112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:53:07.842277Z","iopub.execute_input":"2022-08-11T11:53:07.843284Z","iopub.status.idle":"2022-08-11T11:53:07.866014Z","shell.execute_reply.started":"2022-08-11T11:53:07.843237Z","shell.execute_reply":"2022-08-11T11:53:07.864956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Check NaN values","metadata":{}},{"cell_type":"code","source":"NaN_ = test_dataset.isnull().sum()\nprint(NaN_[NaN_ > 0])","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:53:07.908626Z","iopub.execute_input":"2022-08-11T11:53:07.909015Z","iopub.status.idle":"2022-08-11T11:53:07.920950Z","shell.execute_reply.started":"2022-08-11T11:53:07.908983Z","shell.execute_reply":"2022-08-11T11:53:07.919999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Drop unuseful columns","metadata":{}},{"cell_type":"code","source":"test_dataset = test_dataset.drop(['Id','Alley','PoolQC','Fence','MiscFeature','FireplaceQu'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:53:07.957097Z","iopub.execute_input":"2022-08-11T11:53:07.957832Z","iopub.status.idle":"2022-08-11T11:53:07.966572Z","shell.execute_reply.started":"2022-08-11T11:53:07.957795Z","shell.execute_reply":"2022-08-11T11:53:07.965336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Fill NaN values","metadata":{}},{"cell_type":"code","source":"selcted_col3 = np.array(['MSZoning','Utilities','Exterior1st','Exterior2nd','MasVnrType','BsmtQual','BsmtCond','BsmtExposure','BsmtFinType1','BsmtFinType2','KitchenQual','Functional','GarageType','GarageFinish','GarageQual','GarageCond','SaleType'])\nfor col3 in selcted_col3:\n    test_dataset[col3] =  test_dataset[col3].fillna(test_dataset[col3].mode()[0])","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:53:08.004653Z","iopub.execute_input":"2022-08-11T11:53:08.005837Z","iopub.status.idle":"2022-08-11T11:53:08.025732Z","shell.execute_reply.started":"2022-08-11T11:53:08.005793Z","shell.execute_reply":"2022-08-11T11:53:08.024435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"selcted_col4 = np.array(['LotFrontage','MasVnrArea','BsmtFinSF1','TotalBsmtSF','BsmtFullBath','BsmtHalfBath','BsmtFinSF2','BsmtUnfSF','GarageYrBlt','GarageArea','GarageCars'])\nfor column in selcted_col4:\n    test_dataset[column] = test_dataset[column].fillna(test_dataset[column].mean())","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:53:08.051947Z","iopub.execute_input":"2022-08-11T11:53:08.053298Z","iopub.status.idle":"2022-08-11T11:53:08.064603Z","shell.execute_reply.started":"2022-08-11T11:53:08.053250Z","shell.execute_reply":"2022-08-11T11:53:08.063224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.array(test_dataset.isnull().sum())","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:53:08.098925Z","iopub.execute_input":"2022-08-11T11:53:08.099348Z","iopub.status.idle":"2022-08-11T11:53:08.111573Z","shell.execute_reply.started":"2022-08-11T11:53:08.099311Z","shell.execute_reply":"2022-08-11T11:53:08.110423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:53:08.146608Z","iopub.execute_input":"2022-08-11T11:53:08.147646Z","iopub.status.idle":"2022-08-11T11:53:08.166820Z","shell.execute_reply.started":"2022-08-11T11:53:08.147605Z","shell.execute_reply":"2022-08-11T11:53:08.166033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:53:08.203787Z","iopub.execute_input":"2022-08-11T11:53:08.204623Z","iopub.status.idle":"2022-08-11T11:53:08.229637Z","shell.execute_reply.started":"2022-08-11T11:53:08.204586Z","shell.execute_reply":"2022-08-11T11:53:08.228528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Concat both Datasets","metadata":{}},{"cell_type":"code","source":"full_train = pd.concat([train_dataset,test_dataset],axis=0)\nfull_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:53:08.251992Z","iopub.execute_input":"2022-08-11T11:53:08.252420Z","iopub.status.idle":"2022-08-11T11:53:08.270973Z","shell.execute_reply.started":"2022-08-11T11:53:08.252386Z","shell.execute_reply":"2022-08-11T11:53:08.270190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:53:08.350592Z","iopub.execute_input":"2022-08-11T11:53:08.351255Z","iopub.status.idle":"2022-08-11T11:53:08.375539Z","shell.execute_reply.started":"2022-08-11T11:53:08.351216Z","shell.execute_reply":"2022-08-11T11:53:08.374110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Convert categorical variable to numbers","metadata":{}},{"cell_type":"code","source":"categorical_cols = full_train.select_dtypes('object').columns.to_list()\ncategorical_cols","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:53:08.449466Z","iopub.execute_input":"2022-08-11T11:53:08.450215Z","iopub.status.idle":"2022-08-11T11:53:08.469958Z","shell.execute_reply.started":"2022-08-11T11:53:08.450174Z","shell.execute_reply":"2022-08-11T11:53:08.469104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_full = full_train\ni = 0\n\nfor fields in categorical_cols:\n    df1 = pd.get_dummies(full_train[fields],drop_first=True)\n    full_train.drop([fields],axis=1,inplace=True)\n    if i == 0:\n        train_full = df1.copy()\n    else:\n        train_full = pd.concat([train_full,df1],axis=1)\n    i=i+1\n       \n        \nfull_train = pd.concat([full_train,train_full],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:53:08.559469Z","iopub.execute_input":"2022-08-11T11:53:08.560188Z","iopub.status.idle":"2022-08-11T11:53:08.687919Z","shell.execute_reply.started":"2022-08-11T11:53:08.560143Z","shell.execute_reply":"2022-08-11T11:53:08.686684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Remove Duplicate Columns","metadata":{}},{"cell_type":"code","source":"full_train = full_train.loc[:,~full_train.columns.duplicated()]","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:53:08.691910Z","iopub.execute_input":"2022-08-11T11:53:08.692579Z","iopub.status.idle":"2022-08-11T11:53:08.701993Z","shell.execute_reply.started":"2022-08-11T11:53:08.692528Z","shell.execute_reply":"2022-08-11T11:53:08.700794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:53:08.703216Z","iopub.execute_input":"2022-08-11T11:53:08.704354Z","iopub.status.idle":"2022-08-11T11:53:08.727982Z","shell.execute_reply.started":"2022-08-11T11:53:08.704319Z","shell.execute_reply":"2022-08-11T11:53:08.726866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_train_dataset = full_train.iloc[:1459,:]\nnew_test_dataset = full_train.iloc[1460:,:]\n\nnew_train_dataset.shape, new_test_dataset.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:53:08.730279Z","iopub.execute_input":"2022-08-11T11:53:08.730634Z","iopub.status.idle":"2022-08-11T11:53:08.738070Z","shell.execute_reply.started":"2022-08-11T11:53:08.730602Z","shell.execute_reply":"2022-08-11T11:53:08.737338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train Model","metadata":{}},{"cell_type":"code","source":"import xgboost\nmodel = xgboost.XGBRegressor()\n\ny_train = new_train_dataset['SalePrice']\n\nX_train = new_train_dataset.drop(['SalePrice'],axis=1)\n\nmodel.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:53:08.751384Z","iopub.execute_input":"2022-08-11T11:53:08.752171Z","iopub.status.idle":"2022-08-11T11:53:10.481947Z","shell.execute_reply.started":"2022-08-11T11:53:08.752101Z","shell.execute_reply":"2022-08-11T11:53:10.480942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_test_dataset = new_test_dataset.drop('SalePrice', axis=1)\npredictions = model.predict(new_test_dataset)\npredictions","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:53:10.486018Z","iopub.execute_input":"2022-08-11T11:53:10.486366Z","iopub.status.idle":"2022-08-11T11:53:10.510727Z","shell.execute_reply.started":"2022-08-11T11:53:10.486336Z","shell.execute_reply":"2022-08-11T11:53:10.509877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"getSample = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/sample_submission.csv')\noutput = pd.DataFrame({'Id': getSample.Id, 'SalePrice': predictions})\noutput.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:53:10.514734Z","iopub.execute_input":"2022-08-11T11:53:10.516856Z","iopub.status.idle":"2022-08-11T11:53:10.540656Z","shell.execute_reply.started":"2022-08-11T11:53:10.516816Z","shell.execute_reply":"2022-08-11T11:53:10.539581Z"},"trusted":true},"execution_count":null,"outputs":[]}]}