{"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":"markdown","source":"# Importing Libraries and loading the data","metadata":{}},{"cell_type":"code","source":"#importing libraries\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\n#Loading data\ndf = pd.read_csv(\"../input/house-prices-advanced-regression-techniques/train.csv\")\nfinal_test = pd.read_csv(\"../input/house-prices-advanced-regression-techniques/test.csv\")\ntest_ID = final_test[\"Id\"]\n\n#dropping ID column from both train and test data\ndf = df.drop(columns=\"Id\")\nfinal_test_Id = final_test[\"Id\"]\nfinal_test = final_test.drop(columns=\"Id\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-27T17:26:11.741728Z","iopub.execute_input":"2022-07-27T17:26:11.743047Z","iopub.status.idle":"2022-07-27T17:26:11.807584Z","shell.execute_reply.started":"2022-07-27T17:26:11.743000Z","shell.execute_reply":"2022-07-27T17:26:11.806438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Analysing the data and identifying outliers","metadata":{}},{"cell_type":"code","source":"#finding features with high correlation to SalePrice\nnp.abs(df.corr()[\"SalePrice\"]).sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T17:26:11.809393Z","iopub.execute_input":"2022-07-27T17:26:11.809945Z","iopub.status.idle":"2022-07-27T17:26:11.826441Z","shell.execute_reply.started":"2022-07-27T17:26:11.809911Z","shell.execute_reply":"2022-07-27T17:26:11.825142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Plotting high correlation features vs SalePrice\nfig, axs = plt.subplots(ncols=2,figsize=(17,5))\nsns.scatterplot(data=df,y=\"SalePrice\",x=\"OverallQual\", ax=axs[0])\nsns.scatterplot(data=df,y=\"SalePrice\",x=\"GrLivArea\", ax=axs[1]);","metadata":{"execution":{"iopub.status.busy":"2022-07-27T17:26:11.827827Z","iopub.execute_input":"2022-07-27T17:26:11.828419Z","iopub.status.idle":"2022-07-27T17:26:12.222035Z","shell.execute_reply.started":"2022-07-27T17:26:11.828383Z","shell.execute_reply":"2022-07-27T17:26:12.220680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#There are 2 high quality houses with low saleprice, removing these 2 as outliers\ndf = df[~((df[\"OverallQual\"]==10) & (df[\"SalePrice\"]<200000))]","metadata":{"execution":{"iopub.status.busy":"2022-07-27T17:26:12.224339Z","iopub.execute_input":"2022-07-27T17:26:12.224732Z","iopub.status.idle":"2022-07-27T17:26:12.234636Z","shell.execute_reply.started":"2022-07-27T17:26:12.224699Z","shell.execute_reply":"2022-07-27T17:26:12.233205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dealing with missing data","metadata":{}},{"cell_type":"code","source":"#function for calculating # of missing data\ndef missing_data():\n    df_missing =  df.isnull().sum()\n    df_missing = df_missing[df_missing > 0].sort_values(ascending=False)\n    test_missing = final_test.isnull().sum()\n    test_missing = test_missing[test_missing > 0].sort_values(ascending=False)\n    missing = pd.DataFrame(data=[df_missing,test_missing], index=[\"Train\",\"Test\"]).T\n    return missing","metadata":{"execution":{"iopub.status.busy":"2022-07-27T17:26:12.236310Z","iopub.execute_input":"2022-07-27T17:26:12.236809Z","iopub.status.idle":"2022-07-27T17:26:12.253527Z","shell.execute_reply.started":"2022-07-27T17:26:12.236762Z","shell.execute_reply":"2022-07-27T17:26:12.252171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Visualizing the missing data\nmissing_data().plot(kind=\"bar\",figsize=(15,6));","metadata":{"execution":{"iopub.status.busy":"2022-07-27T17:26:12.255769Z","iopub.execute_input":"2022-07-27T17:26:12.256279Z","iopub.status.idle":"2022-07-27T17:26:12.817877Z","shell.execute_reply.started":"2022-07-27T17:26:12.256236Z","shell.execute_reply":"2022-07-27T17:26:12.816675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#The first 5 features, missing values show houses without those features, filling them with None\nfeatures_cat = [\"PoolQC\", \"MiscFeature\", \"Alley\", \"Fence\", \"FireplaceQu\"]\ndf[features_cat] = df[features_cat].fillna(\"None\")\nfinal_test[features_cat] = final_test[features_cat].fillna(\"None\")","metadata":{"execution":{"iopub.status.busy":"2022-07-27T17:26:12.819419Z","iopub.execute_input":"2022-07-27T17:26:12.819797Z","iopub.status.idle":"2022-07-27T17:26:12.834986Z","shell.execute_reply.started":"2022-07-27T17:26:12.819762Z","shell.execute_reply":"2022-07-27T17:26:12.833695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Missing LotFrontage is imputated based on average LotFrontage of the same neighborhood\ndf[\"LotFrontage\"] = df.groupby(\"Neighborhood\")[\"LotFrontage\"].transform(lambda val: val.fillna(val.mean()))\nfinal_test[\"LotFrontage\"] = final_test.groupby(\"Neighborhood\")[\"LotFrontage\"].transform(lambda val: val.fillna(val.mean()))","metadata":{"execution":{"iopub.status.busy":"2022-07-27T17:26:12.838592Z","iopub.execute_input":"2022-07-27T17:26:12.838969Z","iopub.status.idle":"2022-07-27T17:26:12.868695Z","shell.execute_reply.started":"2022-07-27T17:26:12.838936Z","shell.execute_reply":"2022-07-27T17:26:12.867572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Missing data regarding Garage means there is no Garage, missing year, area and car is filled with 0 and rest with None\nfeatures_cat = [\"GarageType\", \"GarageFinish\", \"GarageQual\", \"GarageCond\"]\ndf[features_cat] = df[features_cat].fillna(\"None\")\nfinal_test[features_cat] = final_test[features_cat].fillna(\"None\")\n\nfeatures_num = [\"GarageYrBlt\", \"GarageArea\", \"GarageCars\"]\ndf[features_num] = df[features_num].fillna(0)\nfinal_test[features_num] = final_test[features_num].fillna(0)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T17:26:12.869950Z","iopub.execute_input":"2022-07-27T17:26:12.870307Z","iopub.status.idle":"2022-07-27T17:26:12.890637Z","shell.execute_reply.started":"2022-07-27T17:26:12.870277Z","shell.execute_reply":"2022-07-27T17:26:12.889596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Filling missing data regarding basement with None or 0\nfeatures_cat = [\"BsmtQual\", \"BsmtCond\", \"BsmtExposure\", \"BsmtFinType1\", \"BsmtFinType2\"]\ndf[features_cat] = df[features_cat].fillna(\"None\")\nfinal_test[features_cat] = final_test[features_cat].fillna(\"None\")\n\nfeatures_num = [\"BsmtFinSF1\", \"BsmtFinSF2\", \"BsmtUnfSF\", \"TotalBsmtSF\", \"BsmtFullBath\", \"BsmtHalfBath\"]\ndf[features_num] = df[features_num].fillna(0)\nfinal_test[features_num] = final_test[features_num].fillna(0)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T17:26:12.892185Z","iopub.execute_input":"2022-07-27T17:26:12.892534Z","iopub.status.idle":"2022-07-27T17:26:12.912794Z","shell.execute_reply.started":"2022-07-27T17:26:12.892503Z","shell.execute_reply":"2022-07-27T17:26:12.911217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Filling missing data regarding Masonry with None or 0\ndf[\"MasVnrType\"] = df[\"MasVnrType\"].fillna(\"None\")\nfinal_test[\"MasVnrType\"] = final_test[\"MasVnrType\"].fillna(\"None\")\n\ndf[\"MasVnrArea\"] = df[\"MasVnrArea\"].fillna(0)\nfinal_test[\"MasVnrArea\"] = final_test[\"MasVnrArea\"].fillna(0)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T17:26:12.914237Z","iopub.execute_input":"2022-07-27T17:26:12.914753Z","iopub.status.idle":"2022-07-27T17:26:12.925220Z","shell.execute_reply.started":"2022-07-27T17:26:12.914705Z","shell.execute_reply":"2022-07-27T17:26:12.924088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Removing the row with missing Electrical Value\ndf = df[~df[\"Electrical\"].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-07-27T17:26:12.927163Z","iopub.execute_input":"2022-07-27T17:26:12.927545Z","iopub.status.idle":"2022-07-27T17:26:12.939323Z","shell.execute_reply.started":"2022-07-27T17:26:12.927512Z","shell.execute_reply":"2022-07-27T17:26:12.937782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#RL seems to be the most common MSZoning value, therefore missing values are filled with RL\nfinal_test[\"MSZoning\"] = final_test[\"MSZoning\"].fillna(final_test[\"MSZoning\"].mode()[0])","metadata":{"execution":{"iopub.status.busy":"2022-07-27T17:26:12.941069Z","iopub.execute_input":"2022-07-27T17:26:12.942250Z","iopub.status.idle":"2022-07-27T17:26:12.953924Z","shell.execute_reply.started":"2022-07-27T17:26:12.942210Z","shell.execute_reply":"2022-07-27T17:26:12.952830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#For Utilities, majority of data points except 1 are AllPub, 1 data point is NoSeWa and 2 are missing, as a result the entire column can be safely removed\ndf = df.drop(columns=\"Utilities\")\nfinal_test = final_test.drop(columns=\"Utilities\")","metadata":{"execution":{"iopub.status.busy":"2022-07-27T17:26:12.955295Z","iopub.execute_input":"2022-07-27T17:26:12.956187Z","iopub.status.idle":"2022-07-27T17:26:12.969279Z","shell.execute_reply.started":"2022-07-27T17:26:12.956148Z","shell.execute_reply":"2022-07-27T17:26:12.968058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Filling missing values for Functional with the most common value - Typ\nfinal_test[\"Functional\"] = final_test[\"Functional\"].fillna(final_test[\"Functional\"].mode()[0])","metadata":{"execution":{"iopub.status.busy":"2022-07-27T17:26:12.970666Z","iopub.execute_input":"2022-07-27T17:26:12.971096Z","iopub.status.idle":"2022-07-27T17:26:12.983692Z","shell.execute_reply.started":"2022-07-27T17:26:12.971059Z","shell.execute_reply":"2022-07-27T17:26:12.982245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Filling the rest of the missing values with the most common one\nfinal_test[\"Exterior1st\"] = final_test[\"Exterior1st\"].fillna(final_test[\"Exterior1st\"].mode()[0])\nfinal_test[\"Exterior2nd\"] = final_test[\"Exterior2nd\"].fillna(final_test[\"Exterior2nd\"].mode()[0])\nfinal_test[\"KitchenQual\"] = final_test[\"KitchenQual\"].fillna(final_test[\"KitchenQual\"].mode()[0])\nfinal_test[\"SaleType\"] = final_test[\"SaleType\"].fillna(final_test[\"SaleType\"].mode()[0])","metadata":{"execution":{"iopub.status.busy":"2022-07-27T17:26:12.985301Z","iopub.execute_input":"2022-07-27T17:26:12.986558Z","iopub.status.idle":"2022-07-27T17:26:13.001434Z","shell.execute_reply.started":"2022-07-27T17:26:12.986504Z","shell.execute_reply":"2022-07-27T17:26:13.000106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#No missing data remaining\nmissing_data()","metadata":{"execution":{"iopub.status.busy":"2022-07-27T17:26:13.002950Z","iopub.execute_input":"2022-07-27T17:26:13.004050Z","iopub.status.idle":"2022-07-27T17:26:13.036913Z","shell.execute_reply.started":"2022-07-27T17:26:13.004000Z","shell.execute_reply":"2022-07-27T17:26:13.036053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data preparation","metadata":{}},{"cell_type":"code","source":"#Converiting some of the numerical data to categorical\nn_train = df.shape[0]\nX = df.drop(columns=\"SalePrice\")\ny_train = df[\"SalePrice\"]\nall_data = pd.concat((X,final_test))","metadata":{"execution":{"iopub.status.busy":"2022-07-27T17:26:13.038341Z","iopub.execute_input":"2022-07-27T17:26:13.038887Z","iopub.status.idle":"2022-07-27T17:26:13.062247Z","shell.execute_reply.started":"2022-07-27T17:26:13.038852Z","shell.execute_reply":"2022-07-27T17:26:13.061045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features = [\"MSSubClass\", \"MoSold\", \"YrSold\"]\nall_data[features] = all_data[features].astype(\"object\")","metadata":{"execution":{"iopub.status.busy":"2022-07-27T17:26:13.063911Z","iopub.execute_input":"2022-07-27T17:26:13.064669Z","iopub.status.idle":"2022-07-27T17:26:13.080969Z","shell.execute_reply.started":"2022-07-27T17:26:13.064620Z","shell.execute_reply":"2022-07-27T17:26:13.079476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Making dummy variables\nall_data = pd.get_dummies(all_data)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T17:26:13.086640Z","iopub.execute_input":"2022-07-27T17:26:13.087075Z","iopub.status.idle":"2022-07-27T17:26:13.155934Z","shell.execute_reply.started":"2022-07-27T17:26:13.087037Z","shell.execute_reply":"2022-07-27T17:26:13.154382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Seperating the traing data\nX_train = all_data[:n_train]\nX_test = all_data[n_train:]","metadata":{"execution":{"iopub.status.busy":"2022-07-27T17:26:13.157456Z","iopub.execute_input":"2022-07-27T17:26:13.158103Z","iopub.status.idle":"2022-07-27T17:26:13.163363Z","shell.execute_reply.started":"2022-07-27T17:26:13.158063Z","shell.execute_reply":"2022-07-27T17:26:13.162331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Training model\nfrom xgboost import XGBRegressor\nfrom sklearn.model_selection import RandomizedSearchCV\nxgb_model = XGBRegressor()\nparam = {\"n_estimators\":[50, 100, 200, 400, 800, 1000, 2000],\n         \"learning_rate\":[ 0.2, 0.1, 0.05, 0.01, 0.001],\n         \"max_depth\":[1,2,3,5,6,7,8,9],\n         \"min_child_weight\":[0.5,1,3,5,8,10],\n         \"gamma\":[50,100,120,150,180,200],\n         \"reg_lambda\":[0,1,5,10]}\nrand_search = RandomizedSearchCV(xgb_model,param)\nrand_search.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T17:26:13.165204Z","iopub.execute_input":"2022-07-27T17:26:13.166469Z","iopub.status.idle":"2022-07-27T17:33:11.987466Z","shell.execute_reply.started":"2022-07-27T17:26:13.166417Z","shell.execute_reply":"2022-07-27T17:33:11.986619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = rand_search.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T17:33:11.988582Z","iopub.execute_input":"2022-07-27T17:33:11.989434Z","iopub.status.idle":"2022-07-27T17:33:12.059008Z","shell.execute_reply.started":"2022-07-27T17:33:11.989396Z","shell.execute_reply":"2022-07-27T17:33:12.058052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Preparing for submission\nsub = pd.DataFrame()\nsub[\"Id\"] = test_ID\nsub[\"SalePrice\"] = prediction","metadata":{"execution":{"iopub.status.busy":"2022-07-27T17:33:12.063807Z","iopub.execute_input":"2022-07-27T17:33:12.064497Z","iopub.status.idle":"2022-07-27T17:33:12.073640Z","shell.execute_reply.started":"2022-07-27T17:33:12.064459Z","shell.execute_reply":"2022-07-27T17:33:12.072476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T17:33:12.075281Z","iopub.execute_input":"2022-07-27T17:33:12.076241Z","iopub.status.idle":"2022-07-27T17:33:12.089142Z","shell.execute_reply.started":"2022-07-27T17:33:12.076182Z","shell.execute_reply":"2022-07-27T17:33:12.087875Z"},"trusted":true},"execution_count":null,"outputs":[]}]}