{"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,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-05T21:27:26.404765Z","iopub.execute_input":"2022-08-05T21:27:26.405195Z","iopub.status.idle":"2022-08-05T21:27:26.417872Z","shell.execute_reply.started":"2022-08-05T21:27:26.405151Z","shell.execute_reply":"2022-08-05T21:27:26.416634Z"},"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-05T21:27:26.424702Z","iopub.execute_input":"2022-08-05T21:27:26.425322Z","iopub.status.idle":"2022-08-05T21:27:26.497502Z","shell.execute_reply.started":"2022-08-05T21:27:26.425288Z","shell.execute_reply":"2022-08-05T21:27:26.496348Z"},"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-05T21:27:26.499651Z","iopub.execute_input":"2022-08-05T21:27:26.502341Z","iopub.status.idle":"2022-08-05T21:27:26.524760Z","shell.execute_reply.started":"2022-08-05T21:27:26.502294Z","shell.execute_reply":"2022-08-05T21:27:26.523611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Describe the data**","metadata":{}},{"cell_type":"code","source":"train_data.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-05T21:41:54.453023Z","iopub.execute_input":"2022-08-05T21:41:54.453445Z","iopub.status.idle":"2022-08-05T21:41:54.569076Z","shell.execute_reply.started":"2022-08-05T21:41:54.453412Z","shell.execute_reply":"2022-08-05T21:41:54.567983Z"},"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-05T21:42:05.405159Z","iopub.execute_input":"2022-08-05T21:42:05.405581Z","iopub.status.idle":"2022-08-05T21:42:05.427015Z","shell.execute_reply.started":"2022-08-05T21:42:05.405547Z","shell.execute_reply":"2022-08-05T21:42:05.425887Z"},"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-05T21:42:12.797490Z","iopub.execute_input":"2022-08-05T21:42:12.797875Z","iopub.status.idle":"2022-08-05T21:42:12.814979Z","shell.execute_reply.started":"2022-08-05T21:42:12.797841Z","shell.execute_reply":"2022-08-05T21:42:12.814053Z"},"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-05T21:27:26.690571Z","iopub.execute_input":"2022-08-05T21:27:26.691017Z","iopub.status.idle":"2022-08-05T21:27:26.700323Z","shell.execute_reply.started":"2022-08-05T21:27:26.690978Z","shell.execute_reply":"2022-08-05T21:27:26.699193Z"},"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-05T21:27:26.701461Z","iopub.execute_input":"2022-08-05T21:27:26.701890Z","iopub.status.idle":"2022-08-05T21:27:26.725794Z","shell.execute_reply.started":"2022-08-05T21:27:26.701849Z","shell.execute_reply":"2022-08-05T21:27:26.724406Z"},"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-05T21:27:26.727309Z","iopub.execute_input":"2022-08-05T21:27:26.728069Z","iopub.status.idle":"2022-08-05T21:27:26.748073Z","shell.execute_reply.started":"2022-08-05T21:27:26.728021Z","shell.execute_reply":"2022-08-05T21:27:26.747069Z"},"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-05T21:27:26.749645Z","iopub.execute_input":"2022-08-05T21:27:26.750598Z","iopub.status.idle":"2022-08-05T21:27:26.765039Z","shell.execute_reply.started":"2022-08-05T21:27:26.750555Z","shell.execute_reply":"2022-08-05T21:27:26.763830Z"},"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-05T21:27:26.768686Z","iopub.execute_input":"2022-08-05T21:27:26.769460Z","iopub.status.idle":"2022-08-05T21:27:26.797965Z","shell.execute_reply.started":"2022-08-05T21:27:26.769426Z","shell.execute_reply":"2022-08-05T21:27:26.796724Z"},"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-05T21:27:26.799674Z","iopub.execute_input":"2022-08-05T21:27:26.800360Z","iopub.status.idle":"2022-08-05T21:27:26.817100Z","shell.execute_reply.started":"2022-08-05T21:27:26.800319Z","shell.execute_reply":"2022-08-05T21:27:26.816250Z"},"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-05T21:27:26.818524Z","iopub.execute_input":"2022-08-05T21:27:26.818864Z","iopub.status.idle":"2022-08-05T21:27:26.838478Z","shell.execute_reply.started":"2022-08-05T21:27:26.818833Z","shell.execute_reply":"2022-08-05T21:27:26.837401Z"},"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-05T21:27:26.839858Z","iopub.execute_input":"2022-08-05T21:27:26.840422Z","iopub.status.idle":"2022-08-05T21:27:26.855538Z","shell.execute_reply.started":"2022-08-05T21:27:26.840386Z","shell.execute_reply":"2022-08-05T21:27:26.854732Z"},"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-05T21:27:26.856871Z","iopub.execute_input":"2022-08-05T21:27:26.857734Z","iopub.status.idle":"2022-08-05T21:27:26.892870Z","shell.execute_reply.started":"2022-08-05T21:27:26.857701Z","shell.execute_reply":"2022-08-05T21:27:26.892105Z"},"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-05T21:27:26.894352Z","iopub.execute_input":"2022-08-05T21:27:26.894885Z","iopub.status.idle":"2022-08-05T21:27:35.972506Z","shell.execute_reply.started":"2022-08-05T21:27:26.894853Z","shell.execute_reply":"2022-08-05T21:27:35.971356Z"},"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-05T21:27:35.973965Z","iopub.execute_input":"2022-08-05T21:27:35.974803Z","iopub.status.idle":"2022-08-05T21:27:35.984383Z","shell.execute_reply.started":"2022-08-05T21:27:35.974768Z","shell.execute_reply":"2022-08-05T21:27:35.983468Z"},"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-05T21:27:35.985597Z","iopub.execute_input":"2022-08-05T21:27:35.985885Z","iopub.status.idle":"2022-08-05T21:27:36.068172Z","shell.execute_reply.started":"2022-08-05T21:27:35.985858Z","shell.execute_reply":"2022-08-05T21:27:36.067054Z"},"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-05T21:27:36.069332Z","iopub.execute_input":"2022-08-05T21:27:36.069635Z","iopub.status.idle":"2022-08-05T21:27:36.946796Z","shell.execute_reply.started":"2022-08-05T21:27:36.069607Z","shell.execute_reply":"2022-08-05T21:27:36.945735Z"},"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-05T21:27:36.948339Z","iopub.execute_input":"2022-08-05T21:27:36.948706Z","iopub.status.idle":"2022-08-05T21:27:36.960574Z","shell.execute_reply.started":"2022-08-05T21:27:36.948664Z","shell.execute_reply":"2022-08-05T21:27:36.959489Z"},"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-05T21:27:36.961970Z","iopub.execute_input":"2022-08-05T21:27:36.962558Z","iopub.status.idle":"2022-08-05T21:27:36.971959Z","shell.execute_reply.started":"2022-08-05T21:27:36.962528Z","shell.execute_reply":"2022-08-05T21:27:36.970960Z"},"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-05T21:27:36.973147Z","iopub.execute_input":"2022-08-05T21:27:36.973501Z","iopub.status.idle":"2022-08-05T21:27:36.984280Z","shell.execute_reply.started":"2022-08-05T21:27:36.973468Z","shell.execute_reply":"2022-08-05T21:27:36.983148Z"},"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-05T21:27:36.985592Z","iopub.execute_input":"2022-08-05T21:27:36.986086Z","iopub.status.idle":"2022-08-05T21:27:37.085693Z","shell.execute_reply.started":"2022-08-05T21:27:36.986054Z","shell.execute_reply":"2022-08-05T21:27:37.084263Z"},"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-05T21:27:37.092596Z","iopub.execute_input":"2022-08-05T21:27:37.097470Z","iopub.status.idle":"2022-08-05T21:27:50.208847Z","shell.execute_reply.started":"2022-08-05T21:27:37.097409Z","shell.execute_reply":"2022-08-05T21:27:50.207926Z"},"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-05T21:27:50.210294Z","iopub.execute_input":"2022-08-05T21:27:50.210636Z","iopub.status.idle":"2022-08-05T21:27:50.326034Z","shell.execute_reply.started":"2022-08-05T21:27:50.210605Z","shell.execute_reply":"2022-08-05T21:27:50.324462Z"},"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-05T21:27:50.332870Z","iopub.execute_input":"2022-08-05T21:27:50.336993Z","iopub.status.idle":"2022-08-05T21:27:50.363460Z","shell.execute_reply.started":"2022-08-05T21:27:50.336910Z","shell.execute_reply":"2022-08-05T21:27:50.362096Z"},"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-05T21:27:50.370325Z","iopub.execute_input":"2022-08-05T21:27:50.373764Z","iopub.status.idle":"2022-08-05T21:27:50.395677Z","shell.execute_reply.started":"2022-08-05T21:27:50.373703Z","shell.execute_reply":"2022-08-05T21:27:50.394184Z"},"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-05T21:27:50.405585Z","iopub.execute_input":"2022-08-05T21:27:50.406472Z","iopub.status.idle":"2022-08-05T21:27:50.418551Z","shell.execute_reply.started":"2022-08-05T21:27:50.406416Z","shell.execute_reply":"2022-08-05T21:27:50.417779Z"},"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-05T21:27:50.420812Z","iopub.execute_input":"2022-08-05T21:27:50.421542Z","iopub.status.idle":"2022-08-05T21:27:50.613071Z","shell.execute_reply.started":"2022-08-05T21:27:50.421505Z","shell.execute_reply":"2022-08-05T21:27:50.612308Z"},"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-05T21:27:50.614071Z","iopub.execute_input":"2022-08-05T21:27:50.614768Z","iopub.status.idle":"2022-08-05T21:27:50.622115Z","shell.execute_reply.started":"2022-08-05T21:27:50.614736Z","shell.execute_reply":"2022-08-05T21:27:50.620946Z"},"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-05T21:27:50.623674Z","iopub.execute_input":"2022-08-05T21:27:50.624469Z","iopub.status.idle":"2022-08-05T21:27:50.642217Z","shell.execute_reply.started":"2022-08-05T21:27:50.624416Z","shell.execute_reply":"2022-08-05T21:27:50.641060Z"},"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-05T21:27:50.643870Z","iopub.execute_input":"2022-08-05T21:27:50.644542Z","iopub.status.idle":"2022-08-05T21:27:50.661022Z","shell.execute_reply.started":"2022-08-05T21:27:50.644510Z","shell.execute_reply":"2022-08-05T21:27:50.659690Z"},"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-05T21:27:50.662967Z","iopub.execute_input":"2022-08-05T21:27:50.669908Z","iopub.status.idle":"2022-08-05T21:27:50.683773Z","shell.execute_reply.started":"2022-08-05T21:27:50.669850Z","shell.execute_reply":"2022-08-05T21:27:50.682272Z"},"trusted":true},"execution_count":null,"outputs":[]}]}