{"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":"# <font color='green'>House Price Predictions</font> \n* **Part 1 - Data Preprocessing**\n   1. Importing libraries\n   2. Importing the dataset\n   3. Dataset information\n   4. Dropping unnecessary columns\n      - \"Train\" \n      - \"Test\" \n   5. Taking care of misssing data\n      - \"Train\" Numerical\n      - \"Train\" Categorical\n      - \"Test\" Numerical\n      - \"Test\" Categorical\n      - Updated info()\n   6. Encoding categorical data\n      - \"Train\"\n      - \"Test\"\n      - Updated head()\n   7. Spliting the Train & Test datasets\n   8. Feature Scaling  \n   9. Dimensionality reduction\n* **Part 2 - Training the Regression model**\n   1. RandomForest \n   2. Other algorithms\n   3. Accuracy score  \n* **Part 3 - Creating a submission.csv**","metadata":{"id":"lCzeVlGrZ0DO"}},{"cell_type":"markdown","source":"# <font color='blue'>Part 1 - Data Preprocessing</font>","metadata":{}},{"cell_type":"markdown","source":"# Importing libraries","metadata":{"id":"PxG0SPZjZrg5"}},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport pandas as pd\nfrom matplotlib.pyplot import figure","metadata":{"id":"RtWuyiaKZwjr","execution":{"iopub.status.busy":"2022-08-09T17:32:35.132648Z","iopub.execute_input":"2022-08-09T17:32:35.133147Z","iopub.status.idle":"2022-08-09T17:32:35.138617Z","shell.execute_reply.started":"2022-08-09T17:32:35.133107Z","shell.execute_reply":"2022-08-09T17:32:35.137717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Importing the dataset","metadata":{"id":"5anEW3k1Z6xw"}},{"cell_type":"code","source":"train_df = pd.read_csv('../input/home-data-for-ml-course/train.csv')\ntest_df = pd.read_csv('../input/home-data-for-ml-course/test.csv')","metadata":{"id":"7bgk15s0aCvm","execution":{"iopub.status.busy":"2022-08-09T17:32:35.140533Z","iopub.execute_input":"2022-08-09T17:32:35.141184Z","iopub.status.idle":"2022-08-09T17:32:35.198153Z","shell.execute_reply.started":"2022-08-09T17:32:35.141146Z","shell.execute_reply":"2022-08-09T17:32:35.197293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dataset information","metadata":{"id":"uDRQuaHCaQwN"}},{"cell_type":"code","source":"# data type and missing values of each column\ntrain_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T17:32:35.199676Z","iopub.execute_input":"2022-08-09T17:32:35.200568Z","iopub.status.idle":"2022-08-09T17:32:35.236093Z","shell.execute_reply.started":"2022-08-09T17:32:35.200532Z","shell.execute_reply":"2022-08-09T17:32:35.234917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T17:32:35.237646Z","iopub.execute_input":"2022-08-09T17:32:35.238493Z","iopub.status.idle":"2022-08-09T17:32:35.261766Z","shell.execute_reply.started":"2022-08-09T17:32:35.238450Z","shell.execute_reply":"2022-08-09T17:32:35.260891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Description of both datasets\ntrain_df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T17:32:35.264192Z","iopub.execute_input":"2022-08-09T17:32:35.264944Z","iopub.status.idle":"2022-08-09T17:32:35.386652Z","shell.execute_reply.started":"2022-08-09T17:32:35.264879Z","shell.execute_reply":"2022-08-09T17:32:35.385572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T17:32:35.388106Z","iopub.execute_input":"2022-08-09T17:32:35.388457Z","iopub.status.idle":"2022-08-09T17:32:35.494016Z","shell.execute_reply.started":"2022-08-09T17:32:35.388395Z","shell.execute_reply":"2022-08-09T17:32:35.492743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 1st 5 rows of every column for overview\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T17:32:35.495571Z","iopub.execute_input":"2022-08-09T17:32:35.496069Z","iopub.status.idle":"2022-08-09T17:32:35.523621Z","shell.execute_reply.started":"2022-08-09T17:32:35.496022Z","shell.execute_reply":"2022-08-09T17:32:35.522289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T17:32:35.524876Z","iopub.execute_input":"2022-08-09T17:32:35.525378Z","iopub.status.idle":"2022-08-09T17:32:35.554515Z","shell.execute_reply.started":"2022-08-09T17:32:35.525347Z","shell.execute_reply":"2022-08-09T17:32:35.553423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.distplot(train_df['SalePrice'])","metadata":{"execution":{"iopub.status.busy":"2022-08-09T17:32:35.555764Z","iopub.execute_input":"2022-08-09T17:32:35.556773Z","iopub.status.idle":"2022-08-09T17:32:35.899517Z","shell.execute_reply.started":"2022-08-09T17:32:35.556740Z","shell.execute_reply":"2022-08-09T17:32:35.898592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.rcParams['figure.figsize']=35,35\ng = heatmap = sns.heatmap(train_df.corr(), vmin=-1, vmax=1, annot=True, cmap='BrBG',fmt = \".1f\")\nheatmap.set_title('Correlation Heatmap', fontdict={'fontsize':10}, pad=12);","metadata":{"execution":{"iopub.status.busy":"2022-08-09T17:32:35.900810Z","iopub.execute_input":"2022-08-09T17:32:35.901565Z","iopub.status.idle":"2022-08-09T17:32:41.792898Z","shell.execute_reply.started":"2022-08-09T17:32:35.901527Z","shell.execute_reply":"2022-08-09T17:32:41.791717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.barplot(x='YearBuilt', y='SalePrice', data=train_df)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T17:32:41.798431Z","iopub.execute_input":"2022-08-09T17:32:41.798898Z","iopub.status.idle":"2022-08-09T17:32:45.851304Z","shell.execute_reply.started":"2022-08-09T17:32:41.798861Z","shell.execute_reply":"2022-08-09T17:32:45.850033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dropping unnecessary columns","metadata":{"id":"ir0SAQ1qcvfB"}},{"cell_type":"markdown","source":"### \"Train\"","metadata":{"id":"7t0LRUhEdFCe"}},{"cell_type":"code","source":"train_df=train_df.drop(\"Id\",axis=1)\ntrain_df=train_df.drop(\"Alley\",axis=1)\ntrain_df=train_df.drop(\"PoolQC\",axis=1)\ntrain_df=train_df.drop(\"Fence\",axis=1)\ntrain_df=train_df.drop(\"MiscFeature\",axis=1)","metadata":{"id":"6iMuJPUTc9Gp","execution":{"iopub.status.busy":"2022-08-09T17:32:45.852750Z","iopub.execute_input":"2022-08-09T17:32:45.853119Z","iopub.status.idle":"2022-08-09T17:32:45.870014Z","shell.execute_reply.started":"2022-08-09T17:32:45.853087Z","shell.execute_reply":"2022-08-09T17:32:45.868429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### \"Test\"","metadata":{"id":"A6e61P-YdIVg"}},{"cell_type":"code","source":"test_df=test_df.drop(\"Alley\",axis=1)\ntest_df=test_df.drop(\"PoolQC\",axis=1)\ntest_df=test_df.drop(\"Fence\",axis=1)\ntest_df=test_df.drop(\"MiscFeature\",axis=1)","metadata":{"id":"oIb_yFUVdEXz","execution":{"iopub.status.busy":"2022-08-09T17:32:45.871790Z","iopub.execute_input":"2022-08-09T17:32:45.872202Z","iopub.status.idle":"2022-08-09T17:32:45.884883Z","shell.execute_reply.started":"2022-08-09T17:32:45.872169Z","shell.execute_reply":"2022-08-09T17:32:45.883776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Taking care of misssing data","metadata":{"id":"R51vrPAeazAs"}},{"cell_type":"markdown","source":"### \"Train\" Numerical","metadata":{"id":"Msxy-w2P8hkl"}},{"cell_type":"code","source":"train_df[\"LotFrontage\"] = train_df[\"LotFrontage\"].fillna(train_df[\"LotFrontage\"].mean())\ntrain_df[\"MasVnrArea\"] = train_df[\"MasVnrArea\"].fillna(train_df[\"MasVnrArea\"].mean())\ntrain_df[\"GarageYrBlt\"] = train_df[\"GarageYrBlt\"].fillna(2001)","metadata":{"id":"LlnhqGX2bLgI","execution":{"iopub.status.busy":"2022-08-09T17:32:45.886445Z","iopub.execute_input":"2022-08-09T17:32:45.887098Z","iopub.status.idle":"2022-08-09T17:32:45.899885Z","shell.execute_reply.started":"2022-08-09T17:32:45.887062Z","shell.execute_reply":"2022-08-09T17:32:45.898732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### \"Train\" Categorical ","metadata":{}},{"cell_type":"code","source":"c = (\"GarageType\", \"GarageFinish\", \"GarageQual\", \"GarageCond\", \"BsmtFinType2\", \"BsmtCond\", \"BsmtQual\", \"BsmtExposure\", \"MasVnrType\", \"Electrical\", \"FireplaceQu\", \"BsmtFinType1\")\nfor col in c:\n  if train_df[col].dtype == \"object\":\n    train_df[col] = train_df[col].fillna(train_df[col].mode()[0])\n\n''' OR\nfor col in (\"GarageType\", \"GarageFinish\", \"GarageQual\", \"GarageCond\", \"BsmtFinType2\", \"BsmtCond\", \"BsmtQual\", \"BsmtExposure\", \"MasVnrType\", \"Electrical\", \"FireplaceQu\", \"BsmtFinType1\"):\n  test_df[col] = test_df[col].fillna('None')\n'''","metadata":{"id":"EhcKgdSMg3b7","outputId":"8cf2ea7e-97c1-44c0-e052-7a6b5a4c8bad","execution":{"iopub.status.busy":"2022-08-09T17:32:45.901957Z","iopub.execute_input":"2022-08-09T17:32:45.902370Z","iopub.status.idle":"2022-08-09T17:32:45.922969Z","shell.execute_reply.started":"2022-08-09T17:32:45.902338Z","shell.execute_reply":"2022-08-09T17:32:45.921591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### \"Test\" Numerical","metadata":{"id":"ZFTz5vRKAq-q"}},{"cell_type":"code","source":"test_df[\"LotFrontage\"] = test_df[\"LotFrontage\"].fillna(test_df[\"LotFrontage\"].mean())\ntest_df[\"MasVnrArea\"] = test_df[\"MasVnrArea\"].fillna(test_df[\"MasVnrArea\"].mean())\ntest_df[\"GarageYrBlt\"] = test_df[\"GarageYrBlt\"].fillna(2001)\ntest_df[\"GarageCars\"] = test_df[\"GarageCars\"].fillna(0)\ntest_df[\"GarageArea\"] = test_df[\"GarageArea\"].fillna(test_df[\"GarageArea\"].mean())\ntest_df[\"BsmtFullBath\"] = test_df[\"BsmtFullBath\"].fillna(0)\ntest_df[\"BsmtHalfBath\"] = test_df[\"BsmtHalfBath\"].fillna(0)\ntest_df[\"BsmtFinSF1\"] = test_df[\"BsmtFinSF1\"].fillna(test_df[\"BsmtFinSF1\"].mean())\ntest_df[\"BsmtFinSF2\"] = test_df[\"BsmtFinSF2\"].fillna(test_df[\"BsmtFinSF2\"].mean())\ntest_df[\"TotalBsmtSF\"] = test_df[\"TotalBsmtSF\"].fillna(test_df[\"TotalBsmtSF\"].mean())\ntest_df[\"BsmtUnfSF\"] = test_df[\"BsmtUnfSF\"].fillna(test_df[\"BsmtUnfSF\"].mean())\n","metadata":{"id":"gGurIcFa8uk0","execution":{"iopub.status.busy":"2022-08-09T17:32:45.924656Z","iopub.execute_input":"2022-08-09T17:32:45.926255Z","iopub.status.idle":"2022-08-09T17:32:45.942429Z","shell.execute_reply.started":"2022-08-09T17:32:45.926205Z","shell.execute_reply":"2022-08-09T17:32:45.941217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### \"Test\" Categorical","metadata":{}},{"cell_type":"code","source":"c = (\"GarageType\", \"GarageFinish\", \"GarageQual\", \"GarageCond\", \"BsmtFinType2\", \"BsmtCond\", \"BsmtQual\", \"BsmtExposure\", \"MasVnrType\", \"Electrical\",\"MSZoning\",\"Utilities\",\"Exterior1st\",\"Exterior2nd\",\"KitchenQual\",\"Functional\",\"FireplaceQu\",\"SaleType\", \"BsmtFinType1\")\nfor col in c:\n  if test_df[col].dtype == \"object\":\n    test_df[col] = test_df[col].fillna(test_df[col].mode()[0])\n\n''' OR\nfor col in (\"GarageType\", \"GarageFinish\", \"GarageQual\", \"GarageCond\", \"BsmtFinType2\", \"BsmtCond\", \"BsmtQual\", \"BsmtExposure\", \"MasVnrType\", \"Electrical\",\"MSZoning\",\"Utilities\",\"Exterior1st\",\"Exterior2nd\",\"KitchenQual\",\"Functional\",\"FireplaceQu\",\"SaleType\", \"BsmtFinType1\"):\n  test_df[col] = test_df[col].fillna('None')\n'''","metadata":{"id":"b8s_rzArGyMP","outputId":"c57e0618-ca26-4a08-e680-4b5e56c13227","execution":{"iopub.status.busy":"2022-08-09T17:32:45.944157Z","iopub.execute_input":"2022-08-09T17:32:45.944866Z","iopub.status.idle":"2022-08-09T17:32:45.972553Z","shell.execute_reply.started":"2022-08-09T17:32:45.944819Z","shell.execute_reply":"2022-08-09T17:32:45.971630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Updated info()","metadata":{"id":"j5wgDMkeDONF"}},{"cell_type":"code","source":"# All the missing values are filled\ntrain_df.info()","metadata":{"id":"fy2_ZbsOBZLx","outputId":"4b204e31-ab50-40f4-adb1-bb47548d017b","execution":{"iopub.status.busy":"2022-08-09T17:32:45.975424Z","iopub.execute_input":"2022-08-09T17:32:45.976196Z","iopub.status.idle":"2022-08-09T17:32:45.999966Z","shell.execute_reply.started":"2022-08-09T17:32:45.976151Z","shell.execute_reply":"2022-08-09T17:32:45.998619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T17:32:46.001729Z","iopub.execute_input":"2022-08-09T17:32:46.002484Z","iopub.status.idle":"2022-08-09T17:32:46.026024Z","shell.execute_reply.started":"2022-08-09T17:32:46.002433Z","shell.execute_reply":"2022-08-09T17:32:46.024748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Plotting the categorical data","metadata":{}},{"cell_type":"code","source":"categorical_cols_df = train_df.select_dtypes(include='object')\ncols = categorical_cols_df.columns\nfig = plt.figure(figsize = (30,60))\nfig.subplots_adjust(hspace=0.3)\nlen(cols)\nfor i in range(1,38):\n    plt.subplot(19, 2, i)\n    order = train_df.groupby(cols[i])['SalePrice'].mean().sort_values(ascending=True).index.values\n    sns.boxplot(x=cols[i], y='SalePrice', data= train_df, order = order)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T17:32:46.028127Z","iopub.execute_input":"2022-08-09T17:32:46.028974Z","iopub.status.idle":"2022-08-09T17:32:53.303168Z","shell.execute_reply.started":"2022-08-09T17:32:46.028904Z","shell.execute_reply":"2022-08-09T17:32:53.301760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Encoding categorical data","metadata":{"id":"p7BEIeoUd-5S"}},{"cell_type":"code","source":"categorical_cols_df = train_df.select_dtypes(include='object')\ncols = categorical_cols_df.columns\ncols\nfor i in cols:\n    order = train_df.groupby(i)['SalePrice'].mean().sort_values(ascending=True).index.values\n    train_df[i].replace(to_replace=order, value=list(range(0,len(order))), inplace = True)\n    test_df[i].replace(to_replace=order, value=list(range(0,len(order))), inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T17:32:53.305288Z","iopub.execute_input":"2022-08-09T17:32:53.305743Z","iopub.status.idle":"2022-08-09T17:32:53.529231Z","shell.execute_reply.started":"2022-08-09T17:32:53.305699Z","shell.execute_reply":"2022-08-09T17:32:53.528001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T17:32:53.530506Z","iopub.execute_input":"2022-08-09T17:32:53.532826Z","iopub.status.idle":"2022-08-09T17:32:53.565059Z","shell.execute_reply.started":"2022-08-09T17:32:53.532773Z","shell.execute_reply":"2022-08-09T17:32:53.563774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Updated head()","metadata":{"id":"OM9ahxwdEUr8"}},{"cell_type":"code","source":"# All the categorical data is encoded with numbers\ntrain_df.head()","metadata":{"id":"k53CSzefrQcR","outputId":"24bd1226-88e7-4cf4-96b1-ee493723838f","execution":{"iopub.status.busy":"2022-08-09T17:32:53.566710Z","iopub.execute_input":"2022-08-09T17:32:53.567485Z","iopub.status.idle":"2022-08-09T17:32:53.598685Z","shell.execute_reply.started":"2022-08-09T17:32:53.567432Z","shell.execute_reply":"2022-08-09T17:32:53.597244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T17:32:53.600491Z","iopub.execute_input":"2022-08-09T17:32:53.602255Z","iopub.status.idle":"2022-08-09T17:32:53.625589Z","shell.execute_reply.started":"2022-08-09T17:32:53.602204Z","shell.execute_reply":"2022-08-09T17:32:53.624220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.isnull().sum()[test_df.isnull().sum()>0].sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T17:32:53.627146Z","iopub.execute_input":"2022-08-09T17:32:53.630354Z","iopub.status.idle":"2022-08-09T17:32:53.657044Z","shell.execute_reply.started":"2022-08-09T17:32:53.630284Z","shell.execute_reply":"2022-08-09T17:32:53.655299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T17:32:53.658653Z","iopub.execute_input":"2022-08-09T17:32:53.659658Z","iopub.status.idle":"2022-08-09T17:32:53.683640Z","shell.execute_reply.started":"2022-08-09T17:32:53.659600Z","shell.execute_reply":"2022-08-09T17:32:53.682375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Spliting the Train & Test datasets","metadata":{"id":"x6oq43_8Hfzs"}},{"cell_type":"code","source":"X_train = train_df.drop(\"SalePrice\", axis=1)\nY_train = train_df[\"SalePrice\"]\nX_test  = test_df.drop(\"Id\", axis=1).copy()\n''' OR\nX_train = train_df[:, 0:-1]\nY_train = train_df[:, -1]\nX_test  = test_df[:, 1:]\n'''","metadata":{"id":"mJvO4rsDHmdP","outputId":"654f49bc-040a-43f6-bfa7-3f0aaaa83ff9","execution":{"iopub.status.busy":"2022-08-09T17:32:53.685359Z","iopub.execute_input":"2022-08-09T17:32:53.687267Z","iopub.status.idle":"2022-08-09T17:32:53.700963Z","shell.execute_reply.started":"2022-08-09T17:32:53.687213Z","shell.execute_reply":"2022-08-09T17:32:53.700023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Feature Scaling","metadata":{"id":"22rwP91OCc-s"}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nsc = StandardScaler()\nX_train = sc.fit_transform(X_train)\nX_test = sc.transform(X_test)","metadata":{"id":"rV09GGBcCjQu","execution":{"iopub.status.busy":"2022-08-09T17:32:53.702285Z","iopub.execute_input":"2022-08-09T17:32:53.703120Z","iopub.status.idle":"2022-08-09T17:32:53.796619Z","shell.execute_reply.started":"2022-08-09T17:32:53.703085Z","shell.execute_reply":"2022-08-09T17:32:53.795441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train)","metadata":{"id":"FFAaorvl1pGU","outputId":"a36df660-30c8-4f0f-f14c-f5f2fe34b80c","execution":{"iopub.status.busy":"2022-08-09T17:32:53.807836Z","iopub.execute_input":"2022-08-09T17:32:53.810213Z","iopub.status.idle":"2022-08-09T17:32:53.816195Z","shell.execute_reply.started":"2022-08-09T17:32:53.810171Z","shell.execute_reply":"2022-08-09T17:32:53.814988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(Y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T17:32:53.820339Z","iopub.execute_input":"2022-08-09T17:32:53.821004Z","iopub.status.idle":"2022-08-09T17:32:53.834382Z","shell.execute_reply.started":"2022-08-09T17:32:53.820969Z","shell.execute_reply":"2022-08-09T17:32:53.832989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dimensionality Reduction","metadata":{"id":"XzFfhF9KZP6P"}},{"cell_type":"code","source":"# Principle Component Analysis\nfrom sklearn.decomposition import PCA\npca = PCA(n_components = 10)\nX_train = pca.fit_transform(X_train)\nX_test = pca.transform(X_test)","metadata":{"id":"6fPPTTfbmzLt","execution":{"iopub.status.busy":"2022-08-09T17:32:53.836190Z","iopub.execute_input":"2022-08-09T17:32:53.836955Z","iopub.status.idle":"2022-08-09T17:32:54.063077Z","shell.execute_reply.started":"2022-08-09T17:32:53.836887Z","shell.execute_reply":"2022-08-09T17:32:54.061459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <font color='blue'>Part 2 - Training the Regression model on the Training set</font>","metadata":{"id":"RIw76-OZlTBo"}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor\nscore_RF_list = []\nfor i in range(10,250,10):\n    RF=RandomForestRegressor(n_estimators = i, random_state = 0)\n    RF.fit(X_train,Y_train)\n    score_RF = round(RF.score(X_train, Y_train) * 100, 2)\n    score_RF_list.append(score_RF)\n    \nscore_RF_list   ","metadata":{"execution":{"iopub.status.busy":"2022-08-09T17:32:54.065444Z","iopub.execute_input":"2022-08-09T17:32:54.066326Z","iopub.status.idle":"2022-08-09T17:33:24.585289Z","shell.execute_reply.started":"2022-08-09T17:32:54.066261Z","shell.execute_reply":"2022-08-09T17:33:24.584101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_axis = [x for x in range(10,250,10)]\nsns.lineplot(x=x_axis, y=score_RF_list)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T17:33:24.587014Z","iopub.execute_input":"2022-08-09T17:33:24.587717Z","iopub.status.idle":"2022-08-09T17:33:25.187034Z","shell.execute_reply.started":"2022-08-09T17:33:24.587671Z","shell.execute_reply":"2022-08-09T17:33:25.185716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nregressor = RandomForestRegressor(n_estimators = 50, random_state = 0)\nregressor.fit(X_train, Y_train)\nY_pred = regressor.predict(X_test)","metadata":{"id":"78U3C0HFlSlI","execution":{"iopub.status.busy":"2022-08-09T17:33:25.188603Z","iopub.execute_input":"2022-08-09T17:33:25.189260Z","iopub.status.idle":"2022-08-09T17:33:25.708390Z","shell.execute_reply.started":"2022-08-09T17:33:25.189215Z","shell.execute_reply":"2022-08-09T17:33:25.707294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Other Algorithms ","metadata":{}},{"cell_type":"code","source":"''' OR\nfrom xgboost import XGBRegressor\nregressor = XGBRegressor()\nregressor.fit(X_train, Y_train)\nY_pred = regressor.predict(X_test)\n'''","metadata":{"id":"vfbb6A4dKmL_","outputId":"b2ee47b9-7496-4715-aa01-da30d62c7b90","execution":{"iopub.status.busy":"2022-08-09T17:33:25.709862Z","iopub.execute_input":"2022-08-09T17:33:25.710324Z","iopub.status.idle":"2022-08-09T17:33:25.718871Z","shell.execute_reply.started":"2022-08-09T17:33:25.710290Z","shell.execute_reply":"2022-08-09T17:33:25.717580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Accuracy score","metadata":{"id":"tHf4C6GaFd5x"}},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\nregressor.score(X_train, Y_train)\nregressor = round(regressor.score(X_train, Y_train) * 100, 2)\nregressor","metadata":{"id":"bB7Uy9gKFcke","outputId":"49e3e751-1db0-4487-eec0-2914e20a04d6","execution":{"iopub.status.busy":"2022-08-09T17:33:25.720263Z","iopub.execute_input":"2022-08-09T17:33:25.720814Z","iopub.status.idle":"2022-08-09T17:33:25.770817Z","shell.execute_reply.started":"2022-08-09T17:33:25.720780Z","shell.execute_reply":"2022-08-09T17:33:25.769704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <font color='blue'>Part 3 - Creating a submission.csv</font>","metadata":{"id":"kz5LHHcUCTgS"}},{"cell_type":"code","source":"submission = pd.DataFrame({\n        \"Id\": test_df[\"Id\"],\n        \"SalePrice\": Y_pred\n    })","metadata":{"id":"cwCFScsqm9qk","execution":{"iopub.status.busy":"2022-08-09T17:33:25.772109Z","iopub.execute_input":"2022-08-09T17:33:25.772439Z","iopub.status.idle":"2022-08-09T17:33:25.778769Z","shell.execute_reply.started":"2022-08-09T17:33:25.772409Z","shell.execute_reply":"2022-08-09T17:33:25.777419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)\nsubmission","metadata":{"id":"lIJk61yYnUAr","execution":{"iopub.status.busy":"2022-08-09T17:33:25.780172Z","iopub.execute_input":"2022-08-09T17:33:25.780763Z","iopub.status.idle":"2022-08-09T17:33:25.802723Z","shell.execute_reply.started":"2022-08-09T17:33:25.780720Z","shell.execute_reply":"2022-08-09T17:33:25.801879Z"},"trusted":true},"execution_count":null,"outputs":[]}]}