{"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":"\nimport numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt\nimport seaborn as sns\n%matplotlib inline\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.model_selection import KFold\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.metrics import r2_score,mean_squared_error\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-26T17:39:41.334923Z","iopub.execute_input":"2022-07-26T17:39:41.335393Z","iopub.status.idle":"2022-07-26T17:39:41.346570Z","shell.execute_reply.started":"2022-07-26T17:39:41.335353Z","shell.execute_reply":"2022-07-26T17:39:41.345114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/house-prices-advanced-regression-techniques/train.csv')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T17:39:41.617769Z","iopub.execute_input":"2022-07-26T17:39:41.618172Z","iopub.status.idle":"2022-07-26T17:39:41.665495Z","shell.execute_reply.started":"2022-07-26T17:39:41.618137Z","shell.execute_reply":"2022-07-26T17:39:41.664151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe(include='all')","metadata":{"execution":{"iopub.status.busy":"2022-07-26T17:39:41.916113Z","iopub.execute_input":"2022-07-26T17:39:41.916724Z","iopub.status.idle":"2022-07-26T17:39:42.098530Z","shell.execute_reply.started":"2022-07-26T17:39:41.916687Z","shell.execute_reply":"2022-07-26T17:39:42.097459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-07-26T17:39:42.181643Z","iopub.execute_input":"2022-07-26T17:39:42.182040Z","iopub.status.idle":"2022-07-26T17:39:42.190964Z","shell.execute_reply.started":"2022-07-26T17:39:42.182008Z","shell.execute_reply":"2022-07-26T17:39:42.189784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"null_table = pd.DataFrame(train.isnull().sum())\nnull_table.columns = ['#']\nnull_table = null_table[null_table['#']>0]\nnull_table","metadata":{"execution":{"iopub.status.busy":"2022-07-26T17:39:42.466735Z","iopub.execute_input":"2022-07-26T17:39:42.467347Z","iopub.status.idle":"2022-07-26T17:39:42.488328Z","shell.execute_reply.started":"2022-07-26T17:39:42.467314Z","shell.execute_reply":"2022-07-26T17:39:42.487081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.histplot(train.LotFrontage)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T17:39:42.746068Z","iopub.execute_input":"2022-07-26T17:39:42.746479Z","iopub.status.idle":"2022-07-26T17:39:43.076361Z","shell.execute_reply.started":"2022-07-26T17:39:42.746444Z","shell.execute_reply":"2022-07-26T17:39:43.075542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train[train.LotFrontage<200]","metadata":{"execution":{"iopub.status.busy":"2022-07-26T17:39:43.078067Z","iopub.execute_input":"2022-07-26T17:39:43.078409Z","iopub.status.idle":"2022-07-26T17:39:43.084648Z","shell.execute_reply.started":"2022-07-26T17:39:43.078378Z","shell.execute_reply":"2022-07-26T17:39:43.083853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.histplot(train.LotArea)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T17:39:43.334758Z","iopub.execute_input":"2022-07-26T17:39:43.335822Z","iopub.status.idle":"2022-07-26T17:39:44.036367Z","shell.execute_reply.started":"2022-07-26T17:39:43.335776Z","shell.execute_reply":"2022-07-26T17:39:44.035169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train[train.LotArea<30000]\ntrain.LotFrontage = train.LotFrontage.replace({np.NaN,np.mean(train.LotFrontage)})\ntrain.MasVnrType = train.MasVnrType.replace({np.NaN:'None'})\ntrain.MasVnrArea = train.MasVnrArea.replace({np.NaN:0})\ntrain.BsmtFinType2 = train.BsmtFinType2.replace({np.NaN:'Unf'})\ntrain.dropna(subset=['Electrical'],inplace=True)\ntrain.FireplaceQu = train.FireplaceQu.replace({'Na':'Gd'})\ntrain.GarageType = train.GarageType.replace({np.NAN:'Attchd'})\ntrain.GarageYrBlt = train.GarageYrBlt.replace({np.NaN:int(np.mean(train.GarageYrBlt))})\ntrain.GarageFinish.fillna(method = 'ffill',inplace = True)\ntrain.GarageQual.fillna(method = 'ffill',inplace = True)\ntrain.GarageCond.fillna(method = 'ffill',inplace = True)\ndel train['Alley'],train['Id'],train['PoolQC'],train['Fence'],train['MiscFeature']\n\ntrain = train.reset_index(drop = True)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-26T17:39:44.038836Z","iopub.execute_input":"2022-07-26T17:39:44.039409Z","iopub.status.idle":"2022-07-26T17:39:44.069189Z","shell.execute_reply.started":"2022-07-26T17:39:44.039363Z","shell.execute_reply":"2022-07-26T17:39:44.068097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr = train.corr()\nplt.figure(figsize=(20,20))\nsns.heatmap(corr,annot=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T17:39:44.070536Z","iopub.execute_input":"2022-07-26T17:39:44.071250Z","iopub.status.idle":"2022-07-26T17:39:49.530401Z","shell.execute_reply.started":"2022-07-26T17:39:44.071209Z","shell.execute_reply":"2022-07-26T17:39:49.529516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\ny = train.iloc[:,-1].values\ntrain = pd.get_dummies(train,drop_first=True)\n\ndel train['Condition2_RRNn'],train['HouseStyle_2.5Fin'],train['RoofMatl_Roll'],train['Exterior1st_ImStucc'],train['Exterior1st_Stone'],train['Exterior2nd_Other'],train['Heating_OthW'],train['Electrical_Mix'],train['GarageQual_Fa']\nX = train.iloc[:,:-1].values\nsc = StandardScaler()\nX = sc.fit_transform(X)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-26T17:39:49.532259Z","iopub.execute_input":"2022-07-26T17:39:49.532771Z","iopub.status.idle":"2022-07-26T17:39:49.594356Z","shell.execute_reply.started":"2022-07-26T17:39:49.532739Z","shell.execute_reply":"2022-07-26T17:39:49.592921Z"},"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-07-26T17:39:49.596286Z","iopub.execute_input":"2022-07-26T17:39:49.597076Z","iopub.status.idle":"2022-07-26T17:39:49.603682Z","shell.execute_reply.started":"2022-07-26T17:39:49.597038Z","shell.execute_reply":"2022-07-26T17:39:49.602816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rfr = RandomForestRegressor(n_estimators=200)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T17:39:49.605037Z","iopub.execute_input":"2022-07-26T17:39:49.605542Z","iopub.status.idle":"2022-07-26T17:39:49.613931Z","shell.execute_reply.started":"2022-07-26T17:39:49.605511Z","shell.execute_reply":"2022-07-26T17:39:49.612660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rfr.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T17:39:49.615643Z","iopub.execute_input":"2022-07-26T17:39:49.616597Z","iopub.status.idle":"2022-07-26T17:39:52.738378Z","shell.execute_reply.started":"2022-07-26T17:39:49.616544Z","shell.execute_reply":"2022-07-26T17:39:52.737254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = rfr.predict(X_test)\nprint(f'r2_score is equal to: {r2_score(y_test,prediction)}')\nprint(f'mean squared error is equal to: {mean_squared_error(y_test,prediction)}')","metadata":{"execution":{"iopub.status.busy":"2022-07-26T17:39:52.742318Z","iopub.execute_input":"2022-07-26T17:39:52.742839Z","iopub.status.idle":"2022-07-26T17:39:52.776070Z","shell.execute_reply.started":"2022-07-26T17:39:52.742807Z","shell.execute_reply":"2022-07-26T17:39:52.775200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('../input/house-prices-advanced-regression-techniques/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-26T17:39:52.777214Z","iopub.execute_input":"2022-07-26T17:39:52.777683Z","iopub.status.idle":"2022-07-26T17:39:52.804157Z","shell.execute_reply.started":"2022-07-26T17:39:52.777653Z","shell.execute_reply":"2022-07-26T17:39:52.803116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"null_table = pd.DataFrame(test.isnull().sum())\nnull_table.columns = ['#']\nnull_table = null_table[null_table['#']>0]\nnull_table","metadata":{"execution":{"iopub.status.busy":"2022-07-26T17:39:52.805472Z","iopub.execute_input":"2022-07-26T17:39:52.806010Z","iopub.status.idle":"2022-07-26T17:39:52.827475Z","shell.execute_reply.started":"2022-07-26T17:39:52.805980Z","shell.execute_reply":"2022-07-26T17:39:52.826037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.LotFrontage = test.LotFrontage.replace(np.NaN,np.mean(train.LotFrontage))\ntest.MasVnrType = test.MasVnrType.replace({np.NaN:'None'})\ntest.MasVnrArea = test.MasVnrArea.replace(np.NaN,0)\ntest.BsmtFinType2 = test.BsmtFinType2.replace({np.NaN:'Unf'})\ntest.dropna(subset=['Electrical'],inplace=True)\ntest.FireplaceQu = test.FireplaceQu.replace({'Na':'Gd'})\ntest.GarageType = test.GarageType.replace({np.NAN:'Attchd'})\ntest.GarageYrBlt = test.GarageYrBlt.replace({np.NaN:int(np.mean(train.GarageYrBlt))})\ntest.GarageFinish.fillna(method = 'ffill',inplace = True)\ntest.GarageQual.fillna(method = 'ffill',inplace = True)\ntest.GarageCond.fillna(method = 'ffill',inplace = True)\ntest = test.drop(['Alley','PoolQC','Fence','MiscFeature'],axis=1)\ntest = test.reset_index(drop = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T17:39:52.829050Z","iopub.execute_input":"2022-07-26T17:39:52.829389Z","iopub.status.idle":"2022-07-26T17:39:52.855472Z","shell.execute_reply.started":"2022-07-26T17:39:52.829359Z","shell.execute_reply":"2022-07-26T17:39:52.854289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"null_table = pd.DataFrame(test.isnull().sum())\nnull_table.columns = ['#']\nnull_table = null_table[null_table['#']>0]\nnull_table","metadata":{"execution":{"iopub.status.busy":"2022-07-26T17:39:52.856815Z","iopub.execute_input":"2022-07-26T17:39:52.857139Z","iopub.status.idle":"2022-07-26T17:39:52.879103Z","shell.execute_reply.started":"2022-07-26T17:39:52.857106Z","shell.execute_reply":"2022-07-26T17:39:52.877870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntest.MSZoning=test.MSZoning.replace(np.NaN,'RL')\ntest.Utilities = test.Utilities.replace(np.NaN,'AllPub')\ntest.Exterior1st = test.Exterior1st.replace(np.NaN,'VinylSd')\ntest.Exterior2nd = test.Exterior2nd.replace(np.NaN,'VinylSd')\ntest.BsmtQual = test.BsmtQual.replace(np.NaN,'TA')\ntest.BsmtCond = test.BsmtCond.replace(np.NaN,'TA')\ntest.BsmtFinType1 = test.BsmtFinType1.replace(np.NaN,'GLQ')\ntest.BsmtFullBath = test.BsmtFullBath.replace(np.NaN,0)\ntest.BsmtHalfBath = test.BsmtHalfBath.replace(np.NaN,0)\ntest.BsmtExposure = test.BsmtExposure.replace(np.NaN,'No')\ntest.BsmtFinSF1 = test.BsmtFinSF1.replace(np.NaN,0)\ntest.BsmtFinSF2 = test.BsmtFinSF2.replace(np.NaN,0)\ntest.BsmtUnfSF = test.BsmtUnfSF.replace(np.NaN,0)\ntest.TotalBsmtSF = test.TotalBsmtSF.replace(np.NaN,0)\ntest.KitchenQual = test.KitchenQual.replace(np.NaN,'TA')\ntest.Functional = test.Functional.replace(np.NaN,'Typ')\ntest.Functional = test.Functional.replace(np.NaN,'Typ')\ntest.FireplaceQu = test.FireplaceQu.replace(np.NaN,'GLQ')\ntest.GarageCars = test.GarageCars.replace(np.NaN,2)\ntest.GarageArea = test.GarageArea.replace(np.NaN,0)\ntest.SaleType = test.SaleType.replace(np.NaN,'WD')\n","metadata":{"execution":{"iopub.status.busy":"2022-07-26T17:39:52.880980Z","iopub.execute_input":"2022-07-26T17:39:52.881798Z","iopub.status.idle":"2022-07-26T17:39:52.908517Z","shell.execute_reply.started":"2022-07-26T17:39:52.881752Z","shell.execute_reply":"2022-07-26T17:39:52.907235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id = pd.DataFrame(test.Id)\nid.columns = ['Id']\ndel test['Id']\ntest = pd.get_dummies(test,drop_first=True)\ndel test['RoofStyle_Shed'],test['Functional_Sev'],test['FireplaceQu_GLQ']\ntest = test.iloc[:].values\ntest = sc.transform(test)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-26T17:40:06.236649Z","iopub.execute_input":"2022-07-26T17:40:06.237059Z","iopub.status.idle":"2022-07-26T17:40:06.298672Z","shell.execute_reply.started":"2022-07-26T17:40:06.237025Z","shell.execute_reply":"2022-07-26T17:40:06.297795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_prediction = rfr.predict(test)\nfinal_prediction = pd.DataFrame(final_prediction)\nfinal_prediction.columns=['SalePrice']\n\nsubmission = pd.concat([id,final_prediction],axis=1)\nsubmission\n\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-26T17:40:41.025389Z","iopub.execute_input":"2022-07-26T17:40:41.025783Z","iopub.status.idle":"2022-07-26T17:40:41.073428Z","shell.execute_reply.started":"2022-07-26T17:40:41.025753Z","shell.execute_reply":"2022-07-26T17:40:41.072160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T17:41:24.217390Z","iopub.execute_input":"2022-07-26T17:41:24.217784Z","iopub.status.idle":"2022-07-26T17:41:24.232171Z","shell.execute_reply.started":"2022-07-26T17:41:24.217747Z","shell.execute_reply":"2022-07-26T17:41:24.231234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}