{"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 warnings\nwarnings.filterwarnings('ignore')\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.metrics import f1_score,recall_score,precision_score,accuracy_score\nfrom sklearn.metrics import classification_report,confusion_matrix\nfrom sklearn.preprocessing import OrdinalEncoder,StandardScaler\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.decomposition import PCA\nfrom sklearn.metrics import r2_score,mean_squared_error,mean_absolute_percentage_error\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.ensemble import RandomForestRegressor,GradientBoostingRegressor\npd.set_option('display.max_columns',100)\npd.set_option('display.max_rows',100)\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-03T22:54:22.087329Z","iopub.execute_input":"2022-08-03T22:54:22.087738Z","iopub.status.idle":"2022-08-03T22:54:22.099659Z","shell.execute_reply.started":"2022-08-03T22:54:22.087703Z","shell.execute_reply":"2022-08-03T22:54:22.098193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Read the csv files into Pandas dataframes**","metadata":{}},{"cell_type":"code","source":"train_data=pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv')\ntest_data=pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv')\nprint(train_data.head())","metadata":{"execution":{"iopub.status.busy":"2022-08-03T22:54:22.115735Z","iopub.execute_input":"2022-08-03T22:54:22.116199Z","iopub.status.idle":"2022-08-03T22:54:22.198176Z","shell.execute_reply.started":"2022-08-03T22:54:22.116162Z","shell.execute_reply":"2022-08-03T22:54:22.196749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Details about the Data**","metadata":{}},{"cell_type":"code","source":"train_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T22:54:22.200086Z","iopub.execute_input":"2022-08-03T22:54:22.200444Z","iopub.status.idle":"2022-08-03T22:54:22.222894Z","shell.execute_reply.started":"2022-08-03T22:54:22.200411Z","shell.execute_reply":"2022-08-03T22:54:22.221675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Describe the data**","metadata":{}},{"cell_type":"code","source":"train_data.describe()\n","metadata":{"execution":{"iopub.status.busy":"2022-08-03T22:54:22.224537Z","iopub.execute_input":"2022-08-03T22:54:22.225369Z","iopub.status.idle":"2022-08-03T22:54:22.351480Z","shell.execute_reply.started":"2022-08-03T22:54:22.225327Z","shell.execute_reply":"2022-08-03T22:54:22.350076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Data PreProcessing**","metadata":{}},{"cell_type":"code","source":"# variance on train data\ntrain_data.var()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T22:54:22.354116Z","iopub.execute_input":"2022-08-03T22:54:22.354472Z","iopub.status.idle":"2022-08-03T22:54:22.377207Z","shell.execute_reply.started":"2022-08-03T22:54:22.354441Z","shell.execute_reply":"2022-08-03T22:54:22.375914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**There is no need to remove any features as none of them have zero variance**","metadata":{}},{"cell_type":"code","source":"#check for missing data\ntrain_data.isnull().sum()[train_data.isnull().sum()>0].sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T22:54:22.378801Z","iopub.execute_input":"2022-08-03T22:54:22.379138Z","iopub.status.idle":"2022-08-03T22:54:22.399167Z","shell.execute_reply.started":"2022-08-03T22:54:22.379109Z","shell.execute_reply":"2022-08-03T22:54:22.397853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**we can observe that PoolQC,MiscFeature,Alley and Fence features has almost all null values so we can remove those features**","metadata":{}},{"cell_type":"code","source":"train_data.drop(columns=['Id','PoolQC','MiscFeature','Alley','Fence'],axis=1,inplace=True)\ntest_data.drop(columns=['Id','PoolQC','MiscFeature','Alley','Fence'],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T22:54:22.400823Z","iopub.execute_input":"2022-08-03T22:54:22.401310Z","iopub.status.idle":"2022-08-03T22:54:22.413964Z","shell.execute_reply.started":"2022-08-03T22:54:22.401265Z","shell.execute_reply":"2022-08-03T22:54:22.413004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Handle the missing data**","metadata":{}},{"cell_type":"code","source":"# check categorical columns with null values for train data\ncategorical_data=train_data.select_dtypes(include='object').columns\nd1=[]\nfor x in train_data.isnull().sum()[train_data.isnull().sum()>0].index:\n    if x in categorical_data:\n        d1.append(x)\nprint(d1)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T22:54:22.415293Z","iopub.execute_input":"2022-08-03T22:54:22.416405Z","iopub.status.idle":"2022-08-03T22:54:22.437251Z","shell.execute_reply.started":"2022-08-03T22:54:22.416366Z","shell.execute_reply":"2022-08-03T22:54:22.435807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Handle null values of categorical column of train data\nfor i in d1:\n    train_data[i].fillna(method='ffill',inplace=True)\ntrain_data[d1].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T22:54:22.438676Z","iopub.execute_input":"2022-08-03T22:54:22.439860Z","iopub.status.idle":"2022-08-03T22:54:22.459063Z","shell.execute_reply.started":"2022-08-03T22:54:22.439817Z","shell.execute_reply":"2022-08-03T22:54:22.457673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.dropna(subset=['FireplaceQu'],axis=0,inplace=True)\ntrain_data[d1].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T22:54:22.462457Z","iopub.execute_input":"2022-08-03T22:54:22.463796Z","iopub.status.idle":"2022-08-03T22:54:22.483193Z","shell.execute_reply.started":"2022-08-03T22:54:22.463735Z","shell.execute_reply":"2022-08-03T22:54:22.482058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Handle null values of numerical columns of train data\nfor j in train_data.isnull().sum()[train_data.isnull().sum()>0].index:\n    train_data[j].fillna(train_data.groupby('MSZoning')[j].transform('median'),inplace=True)\ntrain_data.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T22:54:22.485035Z","iopub.execute_input":"2022-08-03T22:54:22.486031Z","iopub.status.idle":"2022-08-03T22:54:22.513767Z","shell.execute_reply.started":"2022-08-03T22:54:22.485957Z","shell.execute_reply":"2022-08-03T22:54:22.512798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Similarly Preprocessing for Test data**","metadata":{}},{"cell_type":"code","source":"# check categorical columns with null values for test data\nd2=[]\nfor x in test_data.isnull().sum()[test_data.isnull().sum()>0].index:\n    if x in categorical_data:\n        d2.append(x)\nprint(d2)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-03T22:54:22.514915Z","iopub.execute_input":"2022-08-03T22:54:22.515922Z","iopub.status.idle":"2022-08-03T22:54:22.532394Z","shell.execute_reply.started":"2022-08-03T22:54:22.515874Z","shell.execute_reply":"2022-08-03T22:54:22.531143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Handle null values of categorical column of test data\nfor i in d2:\n    test_data[i].fillna(method='ffill',inplace=True)\ntest_data[d2].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T22:54:22.533801Z","iopub.execute_input":"2022-08-03T22:54:22.534144Z","iopub.status.idle":"2022-08-03T22:54:22.557743Z","shell.execute_reply.started":"2022-08-03T22:54:22.534114Z","shell.execute_reply":"2022-08-03T22:54:22.556341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data['FireplaceQu'].fillna('Gd',inplace=True)\ntest_data[d2].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T22:54:22.561089Z","iopub.execute_input":"2022-08-03T22:54:22.561868Z","iopub.status.idle":"2022-08-03T22:54:22.576500Z","shell.execute_reply.started":"2022-08-03T22:54:22.561817Z","shell.execute_reply":"2022-08-03T22:54:22.575196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Handle null values of numerical columns of test data\nfor j in test_data.isnull().sum()[test_data.isnull().sum()>0].index:\n    test_data[j].fillna(test_data.groupby('MSZoning')[j].transform('median'),inplace=True)\ntest_data.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T22:54:22.577945Z","iopub.execute_input":"2022-08-03T22:54:22.579444Z","iopub.status.idle":"2022-08-03T22:54:22.620374Z","shell.execute_reply.started":"2022-08-03T22:54:22.579394Z","shell.execute_reply":"2022-08-03T22:54:22.619386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#check outliers\nplt.figure(figsize=(15,15))\nfor i,j in zip(range(1,38),train_data.select_dtypes(include=['int64','float64']).columns):\n    plt.subplot(8,5,i)\n    sns.boxplot(train_data[j])\n    plt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T22:54:22.621668Z","iopub.execute_input":"2022-08-03T22:54:22.622015Z","iopub.status.idle":"2022-08-03T22:54:32.370149Z","shell.execute_reply.started":"2022-08-03T22:54:22.621962Z","shell.execute_reply":"2022-08-03T22:54:32.368975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# remove outleirs\ntrain_data=train_data[(train_data['MSSubClass']<=150)&(train_data['LotFrontage']<=300)&(train_data['LotArea']<=100000)\n          &(train_data['OverallQual']>=2)&(train_data['BsmtFinSF1']<=4000)&(train_data['TotalBsmtSF']<=4000)\n          &(train_data['2ndFlrSF']<=1800)&(train_data['BsmtFullBath']<=2)&\n          (train_data['Fireplaces']<=2)&(train_data['GarageCars']<=3)]","metadata":{"execution":{"iopub.status.busy":"2022-08-03T22:54:32.371327Z","iopub.execute_input":"2022-08-03T22:54:32.372076Z","iopub.status.idle":"2022-08-03T22:54:32.383317Z","shell.execute_reply.started":"2022-08-03T22:54:32.372041Z","shell.execute_reply":"2022-08-03T22:54:32.382152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Label Encoding the categorical data\ncategory_cols1=train_data.select_dtypes(include='object').columns\ntrain_data1=pd.get_dummies(train_data,columns=category_cols1,prefix=category_cols1)\ntest_data1=pd.get_dummies(test_data,columns=category_cols1,prefix=category_cols1)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T22:54:32.384833Z","iopub.execute_input":"2022-08-03T22:54:32.385743Z","iopub.status.idle":"2022-08-03T22:54:32.475716Z","shell.execute_reply.started":"2022-08-03T22:54:32.385706Z","shell.execute_reply":"2022-08-03T22:54:32.474675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Visulaize how the features are correlated**","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(27,15))\nsns.heatmap(train_data.corr())\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T22:54:32.477506Z","iopub.execute_input":"2022-08-03T22:54:32.477828Z","iopub.status.idle":"2022-08-03T22:54:33.482242Z","shell.execute_reply.started":"2022-08-03T22:54:32.477799Z","shell.execute_reply":"2022-08-03T22:54:33.480774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Split dataset - 80% for training set, 20% for testing set the classifier. Use random state as 2022**","metadata":{}},{"cell_type":"code","source":"X=train_data1.drop('SalePrice',axis=1).values\nY=train_data1['SalePrice'].values","metadata":{"execution":{"iopub.status.busy":"2022-08-03T22:54:33.486385Z","iopub.execute_input":"2022-08-03T22:54:33.487186Z","iopub.status.idle":"2022-08-03T22:54:33.495960Z","shell.execute_reply.started":"2022-08-03T22:54:33.487144Z","shell.execute_reply":"2022-08-03T22:54:33.495018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train,x_test,y_train,y_test=train_test_split(X,Y,test_size=0.2,random_state=42) ","metadata":{"execution":{"iopub.status.busy":"2022-08-03T22:54:33.497240Z","iopub.execute_input":"2022-08-03T22:54:33.498292Z","iopub.status.idle":"2022-08-03T22:54:33.509198Z","shell.execute_reply.started":"2022-08-03T22:54:33.498148Z","shell.execute_reply":"2022-08-03T22:54:33.508210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#scaling\nscale=StandardScaler()\nx_train1= scale.fit_transform(x_train)\nx_test1 = scale.transform(x_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T22:54:33.510598Z","iopub.execute_input":"2022-08-03T22:54:33.511245Z","iopub.status.idle":"2022-08-03T22:54:33.524545Z","shell.execute_reply.started":"2022-08-03T22:54:33.511192Z","shell.execute_reply":"2022-08-03T22:54:33.523248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#dimensionality reduction\nsklearn_pca = PCA(n_components=0.95)\nsklearn_pca.fit(x_train1)\nx_train2= sklearn_pca.transform(x_train1)\nx_test2=sklearn_pca.transform(x_test1)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T22:55:26.680246Z","iopub.execute_input":"2022-08-03T22:55:26.680692Z","iopub.status.idle":"2022-08-03T22:55:26.781660Z","shell.execute_reply.started":"2022-08-03T22:55:26.680654Z","shell.execute_reply":"2022-08-03T22:55:26.780001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Build the model**","metadata":{}},{"cell_type":"code","source":"model1= LinearRegression().fit(x_train2, y_train)\nmodel2= RandomForestRegressor(random_state=42).fit(x_train2, y_train)\nmodel3= GradientBoostingRegressor(random_state=42).fit(x_train2, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T23:03:33.324089Z","iopub.execute_input":"2022-08-03T23:03:33.324494Z","iopub.status.idle":"2022-08-03T23:03:47.070651Z","shell.execute_reply.started":"2022-08-03T23:03:33.324462Z","shell.execute_reply":"2022-08-03T23:03:47.069095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"models = [model1, model2, model3]\nr_m_s_e= [mean_squared_error(y_test, mod.predict(x_test2))**0.5 for mod in models]\nmean_absolute_percent_error= [mean_absolute_percentage_error(y_test, mod.predict(x_test2)) for mod in models]\nr2_Score = [r2_score(y_test, mod.predict(x_test2))*100 for mod in models]","metadata":{"execution":{"iopub.status.busy":"2022-08-03T23:16:41.261543Z","iopub.execute_input":"2022-08-03T23:16:41.262028Z","iopub.status.idle":"2022-08-03T23:16:41.378992Z","shell.execute_reply.started":"2022-08-03T23:16:41.261963Z","shell.execute_reply":"2022-08-03T23:16:41.377379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Models = ['Linear Regression','Random Forest','Gradient Boosting']\ndf_data= pd.DataFrame({'Models':Models,'Accuracy':r2_Score,'RMSE':r_m_s_e,'Mean_absolute_error':mean_absolute_percent_error})\ndf_data","metadata":{"execution":{"iopub.status.busy":"2022-08-03T23:17:14.314128Z","iopub.execute_input":"2022-08-03T23:17:14.314562Z","iopub.status.idle":"2022-08-03T23:17:14.329208Z","shell.execute_reply.started":"2022-08-03T23:17:14.314527Z","shell.execute_reply":"2022-08-03T23:17:14.328101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# including columns which are not in test data after encoding\nl1=[]\nfor i in train_data1.drop(columns='SalePrice',axis=1).columns:\n    if i not in test_data1.columns:\n        l1.append(i)\nl1","metadata":{"execution":{"iopub.status.busy":"2022-08-03T23:25:01.616818Z","iopub.execute_input":"2022-08-03T23:25:01.617298Z","iopub.status.idle":"2022-08-03T23:25:01.627629Z","shell.execute_reply.started":"2022-08-03T23:25:01.617206Z","shell.execute_reply":"2022-08-03T23:25:01.626711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# adding the columns to the test data that was encoded to balance\nfor i in l1:\n    test_data1[i]=0","metadata":{"execution":{"iopub.status.busy":"2022-08-03T23:25:28.929112Z","iopub.execute_input":"2022-08-03T23:25:28.929490Z","iopub.status.idle":"2022-08-03T23:25:28.944789Z","shell.execute_reply.started":"2022-08-03T23:25:28.929451Z","shell.execute_reply":"2022-08-03T23:25:28.943533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# excluding the columns which are not in train data after encoding\nl2=[]\nfor i in test_data1.columns:\n    if i not in train_data1.drop(columns='SalePrice',axis=1).columns:\n        l2.append(i)\nl2","metadata":{"execution":{"iopub.status.busy":"2022-08-03T23:26:42.062966Z","iopub.execute_input":"2022-08-03T23:26:42.063356Z","iopub.status.idle":"2022-08-03T23:26:42.263297Z","shell.execute_reply.started":"2022-08-03T23:26:42.063325Z","shell.execute_reply":"2022-08-03T23:26:42.261950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dropping extra columns from test data which was encoded\ntest_data1.drop(columns=l2,axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T23:27:12.219554Z","iopub.execute_input":"2022-08-03T23:27:12.219945Z","iopub.status.idle":"2022-08-03T23:27:12.229045Z","shell.execute_reply.started":"2022-08-03T23:27:12.219913Z","shell.execute_reply":"2022-08-03T23:27:12.227540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# scaling test data\ntest_data1=scale.transform(test_data1)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T23:28:03.264848Z","iopub.execute_input":"2022-08-03T23:28:03.265271Z","iopub.status.idle":"2022-08-03T23:28:03.277705Z","shell.execute_reply.started":"2022-08-03T23:28:03.265237Z","shell.execute_reply":"2022-08-03T23:28:03.276347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# feature extraction for test data\ntest_data2=sklearn_pca.transform(test_data1)\ntest_data2.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-03T23:28:06.108254Z","iopub.execute_input":"2022-08-03T23:28:06.108642Z","iopub.status.idle":"2022-08-03T23:28:06.127064Z","shell.execute_reply.started":"2022-08-03T23:28:06.108610Z","shell.execute_reply":"2022-08-03T23:28:06.125505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_price=model3.predict(test_data2)\npredicted_price","metadata":{"execution":{"iopub.status.busy":"2022-08-03T23:29:23.164654Z","iopub.execute_input":"2022-08-03T23:29:23.165030Z","iopub.status.idle":"2022-08-03T23:29:23.176714Z","shell.execute_reply.started":"2022-08-03T23:29:23.164999Z","shell.execute_reply":"2022-08-03T23:29:23.175495Z"},"trusted":true},"execution_count":null,"outputs":[]}]}