{"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":"# Loading Required Libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sb\nfrom sklearn.preprocessing import LabelEncoder,StandardScaler\nfrom sklearn.model_selection import train_test_split,GridSearchCV\nimport warnings\nwarnings.filterwarnings(action = 'ignore')\nfrom sklearn.preprocessing import LabelEncoder,StandardScaler\nfrom sklearn.linear_model import Lasso,LinearRegression,ElasticNet,Ridge\nfrom sklearn.neighbors import KNeighborsRegressor\nfrom sklearn.tree import DecisionTreeRegressor\nfrom sklearn.ensemble import RandomForestRegressor,AdaBoostRegressor\nfrom sklearn.model_selection import train_test_split,GridSearchCV,cross_val_score,cross_val_predict\nfrom sklearn.metrics import mean_squared_error,mean_absolute_error,r2_score\nimport xgboost\nimport optuna","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:07:44.941489Z","iopub.execute_input":"2022-07-28T03:07:44.942010Z","iopub.status.idle":"2022-07-28T03:07:44.952963Z","shell.execute_reply.started":"2022-07-28T03:07:44.941965Z","shell.execute_reply":"2022-07-28T03:07:44.951518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading Data","metadata":{}},{"cell_type":"code","source":"data = pd.read_csv('../input/house-prices-advanced-regression-techniques/train.csv')\ntest = pd.read_csv('../input/house-prices-advanced-regression-techniques/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:07:45.007083Z","iopub.execute_input":"2022-07-28T03:07:45.007495Z","iopub.status.idle":"2022-07-28T03:07:45.062551Z","shell.execute_reply.started":"2022-07-28T03:07:45.007465Z","shell.execute_reply":"2022-07-28T03:07:45.061189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(data.info())","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:07:45.108400Z","iopub.execute_input":"2022-07-28T03:07:45.108791Z","iopub.status.idle":"2022-07-28T03:07:45.137065Z","shell.execute_reply.started":"2022-07-28T03:07:45.108760Z","shell.execute_reply":"2022-07-28T03:07:45.135835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:07:45.287833Z","iopub.execute_input":"2022-07-28T03:07:45.288503Z","iopub.status.idle":"2022-07-28T03:07:45.317123Z","shell.execute_reply.started":"2022-07-28T03:07:45.288462Z","shell.execute_reply":"2022-07-28T03:07:45.315572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(test.info())","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:07:45.378321Z","iopub.execute_input":"2022-07-28T03:07:45.379487Z","iopub.status.idle":"2022-07-28T03:07:45.408038Z","shell.execute_reply.started":"2022-07-28T03:07:45.379448Z","shell.execute_reply":"2022-07-28T03:07:45.406778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:07:45.555071Z","iopub.execute_input":"2022-07-28T03:07:45.556176Z","iopub.status.idle":"2022-07-28T03:07:45.584597Z","shell.execute_reply.started":"2022-07-28T03:07:45.556123Z","shell.execute_reply":"2022-07-28T03:07:45.583377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Pre Processing","metadata":{}},{"cell_type":"code","source":"def convert_categorical_to_numerical(data,unique_val = 10):\n    obj_df = data.select_dtypes(include=[object])\n    unique_list = [f for f in obj_df.columns if obj_df[f].unique().shape[0]>=unique_val]\n    print(unique_list)\n    obj_df_sel = obj_df.drop(columns=unique_list)\n    for col in obj_df_sel.columns:\n        data[col] = pd.factorize(data[col])[0]\n    print(\"conversion successfull\")\n    return data\n\ndef auto_data_impute(data,get_rid_percent=2):\n    for x,y in data.isnull().sum().items():\n        percent = y/data.shape[0]\n        if percent <= get_rid_percent/100:\n            data[x]  = data[x].fillna(data[x].mean())\n        else:\n            print(\"removed column : \",x)\n            data = data.drop([x],axis=1)\n    print(\"Data imputation successfull\")\n    return data\n\ndef scaling_data(data,target):\n    num_df = data.select_dtypes(include = [int,float])\n    for i in num_df:\n        sc = StandardScaler()\n        if i!=target:\n            data[i] = sc.fit_transform(data[i].values.reshape(-1,1))\n    print(\"scaling was successfull\")\n    return data","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:07:45.647147Z","iopub.execute_input":"2022-07-28T03:07:45.648238Z","iopub.status.idle":"2022-07-28T03:07:45.660874Z","shell.execute_reply.started":"2022-07-28T03:07:45.648187Z","shell.execute_reply":"2022-07-28T03:07:45.660077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Dropping ID Column\ndata.drop('Id',1,inplace = True)\npid = test.Id\ntest.drop('Id',1,inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:07:45.678184Z","iopub.execute_input":"2022-07-28T03:07:45.679111Z","iopub.status.idle":"2022-07-28T03:07:45.690061Z","shell.execute_reply.started":"2022-07-28T03:07:45.679073Z","shell.execute_reply":"2022-07-28T03:07:45.688539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2> Training Data","metadata":{}},{"cell_type":"code","source":"train = scaling_data(data,target = 'SalePrice')","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:07:45.692546Z","iopub.execute_input":"2022-07-28T03:07:45.693406Z","iopub.status.idle":"2022-07-28T03:07:45.732471Z","shell.execute_reply.started":"2022-07-28T03:07:45.693357Z","shell.execute_reply":"2022-07-28T03:07:45.731578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = convert_categorical_to_numerical(data)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:07:45.733968Z","iopub.execute_input":"2022-07-28T03:07:45.735007Z","iopub.status.idle":"2022-07-28T03:07:45.785783Z","shell.execute_reply.started":"2022-07-28T03:07:45.734971Z","shell.execute_reply":"2022-07-28T03:07:45.784989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:07:45.787258Z","iopub.execute_input":"2022-07-28T03:07:45.788175Z","iopub.status.idle":"2022-07-28T03:07:45.820043Z","shell.execute_reply.started":"2022-07-28T03:07:45.788138Z","shell.execute_reply":"2022-07-28T03:07:45.819157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"le = LabelEncoder()\ntrain['Neighborhood'] = le.fit_transform(train['Neighborhood'])\ntrain['Exterior1st'] = le.fit_transform(train['Exterior1st'])\ntrain['Exterior2nd'] = le.fit_transform(train['Exterior2nd'])\n","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:07:45.821259Z","iopub.execute_input":"2022-07-28T03:07:45.821829Z","iopub.status.idle":"2022-07-28T03:07:45.831560Z","shell.execute_reply.started":"2022-07-28T03:07:45.821799Z","shell.execute_reply":"2022-07-28T03:07:45.830350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = auto_data_impute(train)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:07:45.833332Z","iopub.execute_input":"2022-07-28T03:07:45.833880Z","iopub.status.idle":"2022-07-28T03:07:45.883633Z","shell.execute_reply.started":"2022-07-28T03:07:45.833848Z","shell.execute_reply":"2022-07-28T03:07:45.882341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:07:45.885452Z","iopub.execute_input":"2022-07-28T03:07:45.885807Z","iopub.status.idle":"2022-07-28T03:07:45.921260Z","shell.execute_reply.started":"2022-07-28T03:07:45.885775Z","shell.execute_reply":"2022-07-28T03:07:45.920313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2> Testing Data","metadata":{}},{"cell_type":"code","source":"test = scaling_data(test,None)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:07:45.922773Z","iopub.execute_input":"2022-07-28T03:07:45.923562Z","iopub.status.idle":"2022-07-28T03:07:45.961909Z","shell.execute_reply.started":"2022-07-28T03:07:45.923528Z","shell.execute_reply":"2022-07-28T03:07:45.960984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = convert_categorical_to_numerical(test)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:07:45.963315Z","iopub.execute_input":"2022-07-28T03:07:45.963863Z","iopub.status.idle":"2022-07-28T03:07:46.012729Z","shell.execute_reply.started":"2022-07-28T03:07:45.963832Z","shell.execute_reply":"2022-07-28T03:07:46.011768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"le = LabelEncoder()\ntest['Neighborhood'] = le.fit_transform(test['Neighborhood'])\ntest['Exterior1st'] = le.fit_transform(test['Exterior1st'])\ntest['Exterior2nd'] = le.fit_transform(test['Exterior2nd'])\ntest['SaleType'] = le.fit_transform(test['SaleType'])","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:07:46.014811Z","iopub.execute_input":"2022-07-28T03:07:46.015363Z","iopub.status.idle":"2022-07-28T03:07:46.025778Z","shell.execute_reply.started":"2022-07-28T03:07:46.015331Z","shell.execute_reply":"2022-07-28T03:07:46.024744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = auto_data_impute(test)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:07:46.027303Z","iopub.execute_input":"2022-07-28T03:07:46.027836Z","iopub.status.idle":"2022-07-28T03:07:46.079150Z","shell.execute_reply.started":"2022-07-28T03:07:46.027803Z","shell.execute_reply":"2022-07-28T03:07:46.078212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h3> Verifying if both train and test set have the same columns","metadata":{}},{"cell_type":"code","source":"print(train.columns)\nprint(test.columns)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:07:46.080441Z","iopub.execute_input":"2022-07-28T03:07:46.081002Z","iopub.status.idle":"2022-07-28T03:07:46.087377Z","shell.execute_reply.started":"2022-07-28T03:07:46.080964Z","shell.execute_reply":"2022-07-28T03:07:46.086559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:07:46.088650Z","iopub.execute_input":"2022-07-28T03:07:46.089043Z","iopub.status.idle":"2022-07-28T03:07:46.123463Z","shell.execute_reply.started":"2022-07-28T03:07:46.089011Z","shell.execute_reply":"2022-07-28T03:07:46.122381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:07:46.124733Z","iopub.execute_input":"2022-07-28T03:07:46.125279Z","iopub.status.idle":"2022-07-28T03:07:46.154622Z","shell.execute_reply.started":"2022-07-28T03:07:46.125246Z","shell.execute_reply":"2022-07-28T03:07:46.153715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exploring the dataset","metadata":{}},{"cell_type":"code","source":"train.hist(figsize = (25,30),edgecolor = 'black');","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:07:46.157609Z","iopub.execute_input":"2022-07-28T03:07:46.158225Z","iopub.status.idle":"2022-07-28T03:07:56.443582Z","shell.execute_reply.started":"2022-07-28T03:07:46.158192Z","shell.execute_reply":"2022-07-28T03:07:56.442215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (100,50))\nsb.heatmap(data.corr(),annot = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:07:56.445355Z","iopub.execute_input":"2022-07-28T03:07:56.446271Z","iopub.status.idle":"2022-07-28T03:08:22.429638Z","shell.execute_reply.started":"2022-07-28T03:07:56.446222Z","shell.execute_reply":"2022-07-28T03:08:22.427271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1> Preprocessing for machine learning model </h1>","metadata":{}},{"cell_type":"code","source":"X = train.iloc[:,:77]\ny = train['SalePrice']","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:08:22.431154Z","iopub.execute_input":"2022-07-28T03:08:22.431522Z","iopub.status.idle":"2022-07-28T03:08:22.437507Z","shell.execute_reply.started":"2022-07-28T03:08:22.431490Z","shell.execute_reply":"2022-07-28T03:08:22.436503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train,x_part,y_train,y_part = train_test_split(X,y,test_size = 0.30,random_state = 1)\nx_test,x_valid,y_test,y_valid = train_test_split(x_part,y_part,test_size = 0.10,random_state = 1)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:08:22.438802Z","iopub.execute_input":"2022-07-28T03:08:22.439971Z","iopub.status.idle":"2022-07-28T03:08:22.453479Z","shell.execute_reply.started":"2022-07-28T03:08:22.439890Z","shell.execute_reply":"2022-07-28T03:08:22.452629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1> Model Selection </h1>","metadata":{}},{"cell_type":"code","source":"def model_performance(model,model_name,x_train = x_train,y_train = y_train,x_test = x_test,y_test = y_test,x_valid = x_valid,y_valid = y_valid):\n    \n    y_train_pred = model.predict(x_train)\n    y_test_pred = model.predict(x_test)\n    y_val_pred = model.predict(x_valid)\n    \n    Training_Score = np.round(model.score(x_train,y_train),3)\n    Testing_Score = np.round(model.score(x_test,y_test),3)\n    Validation_score = np.round(model.score(x_valid,y_valid))\n    \n    mse_training = np.round(mean_squared_error(y_train,y_train_pred),3)\n    mse_testing = np.round(mean_squared_error(y_test,y_test_pred),3)\n    mse_validation = np.round(mean_squared_error(y_valid,y_val_pred),3)\n    \n    mae_training = np.round(mean_absolute_error(y_train,y_train_pred),3)\n    mae_testing = np.round(mean_absolute_error(y_test,y_test_pred),3)\n    mae_valid = np.round(mean_absolute_error(y_valid,y_val_pred),3)\n    \n    r2_training = np.round(r2_score(y_train,y_train_pred),3)\n    r2_testing = np.round(r2_score(y_test,y_test_pred),3)\n    r2_valid = np.round(r2_score(y_valid,y_val_pred),3)\n    \n    \n    print(\"Model Performance for:\",model_name)\n    print(\"\")\n    \n    print(\"Training Score:\",Training_Score)\n    print(\"Testing Score:\",Testing_Score)\n    print(\"Validation Score\",Validation_score)\n    print(\"\")\n    \n    print(\"Training Data Mean Squared Error:\",mse_training)\n    print(\"Testing Data Mean Squared Error:\",mse_testing)\n    print(\"Validation Data Mean Squared Error:\",mse_validation)\n\n    print(\"\")\n    \n    print(\"Training Data Mean Absolute Error:\",mae_training)\n    print(\"Testing Data Mean Absolute Error:\",mae_testing)\n    print(\"Validation Data Mean Absolute Error:\",mae_valid)\n    print(\"\")\n    \n    print(\"Training Data r2_score:\",r2_training)\n    print(\"Testing Data r2_score:\",r2_testing)\n    print(\"Validation Data r2_score:\",r2_valid)\n    print(\"\")\n    \n    print(\"Residual Analysis:\")\n    plt.figure(figsize = (20,5))\n    plt.scatter(y_train,(y_train-y_train_pred),color = \"red\",label = 'Training Predictions')\n    plt.scatter(y_test,(y_test-y_test_pred),color = \"green\",label = 'Testing Predictions')\n    plt.scatter(y_valid,(y_valid-y_val_pred),color = 'blue',label = \"Validation Predictions\")\n    plt.legend()\n    plt.show()\n    \n    return Training_Score,Testing_Score,Validation_score,mse_training,mse_testing,mse_validation,mae_training,mae_testing,mae_valid,r2_training,r2_testing,r2_valid\n","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:08:22.455140Z","iopub.execute_input":"2022-07-28T03:08:22.455772Z","iopub.status.idle":"2022-07-28T03:08:22.473120Z","shell.execute_reply.started":"2022-07-28T03:08:22.455737Z","shell.execute_reply":"2022-07-28T03:08:22.472188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2> 1. Linear Regression","metadata":{}},{"cell_type":"code","source":"model1 = LinearRegression()\nmodel1.fit(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:08:22.474506Z","iopub.execute_input":"2022-07-28T03:08:22.475263Z","iopub.status.idle":"2022-07-28T03:08:22.526509Z","shell.execute_reply.started":"2022-07-28T03:08:22.475224Z","shell.execute_reply":"2022-07-28T03:08:22.525193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr_perf = model_performance(model1,model_name = model1)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:08:22.528615Z","iopub.execute_input":"2022-07-28T03:08:22.529475Z","iopub.status.idle":"2022-07-28T03:08:22.988713Z","shell.execute_reply.started":"2022-07-28T03:08:22.529425Z","shell.execute_reply":"2022-07-28T03:08:22.987146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2>2. Ridge","metadata":{}},{"cell_type":"code","source":"model2 = Ridge(alpha = 0.01)\nmodel2.fit(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:08:22.989812Z","iopub.execute_input":"2022-07-28T03:08:22.990189Z","iopub.status.idle":"2022-07-28T03:08:23.025904Z","shell.execute_reply.started":"2022-07-28T03:08:22.990158Z","shell.execute_reply":"2022-07-28T03:08:23.023620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ridge_perf = model_performance(model2,model2)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:08:23.028820Z","iopub.execute_input":"2022-07-28T03:08:23.032423Z","iopub.status.idle":"2022-07-28T03:08:23.461541Z","shell.execute_reply.started":"2022-07-28T03:08:23.032359Z","shell.execute_reply":"2022-07-28T03:08:23.460318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2> 3. KNeighborsRegressor","metadata":{}},{"cell_type":"code","source":"model3 = KNeighborsRegressor(n_neighbors = 6)\nmodel3.fit(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:08:23.463459Z","iopub.execute_input":"2022-07-28T03:08:23.464224Z","iopub.status.idle":"2022-07-28T03:08:23.476405Z","shell.execute_reply.started":"2022-07-28T03:08:23.464176Z","shell.execute_reply":"2022-07-28T03:08:23.475229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn_perf = model_performance(model3,model3)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:08:23.481124Z","iopub.execute_input":"2022-07-28T03:08:23.481835Z","iopub.status.idle":"2022-07-28T03:08:23.955285Z","shell.execute_reply.started":"2022-07-28T03:08:23.481796Z","shell.execute_reply":"2022-07-28T03:08:23.953897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2>4. Decision Tree Classifier </h2>","metadata":{}},{"cell_type":"code","source":"param_grid = {'max_depth':np.arange(1,20),'min_samples_split':np.arange(2,10),'min_samples_leaf':np.arange(2,10)}\ngrid  = GridSearchCV(DecisionTreeRegressor(),param_grid = param_grid,cv = 5)\ngrid.fit(X,y)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:08:23.956766Z","iopub.execute_input":"2022-07-28T03:08:23.957159Z","iopub.status.idle":"2022-07-28T03:10:25.325114Z","shell.execute_reply.started":"2022-07-28T03:08:23.957125Z","shell.execute_reply":"2022-07-28T03:10:25.323760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grid.best_params_","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:10:25.327004Z","iopub.execute_input":"2022-07-28T03:10:25.327482Z","iopub.status.idle":"2022-07-28T03:10:25.335324Z","shell.execute_reply.started":"2022-07-28T03:10:25.327440Z","shell.execute_reply":"2022-07-28T03:10:25.334112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model4 = DecisionTreeRegressor(max_depth = 9,min_samples_leaf = 8,min_samples_split = 9)\nmodel4.fit(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:10:25.336857Z","iopub.execute_input":"2022-07-28T03:10:25.338194Z","iopub.status.idle":"2022-07-28T03:10:25.366157Z","shell.execute_reply.started":"2022-07-28T03:10:25.338143Z","shell.execute_reply":"2022-07-28T03:10:25.364951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dt_perf = model_performance(model4,model4)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:10:25.367584Z","iopub.execute_input":"2022-07-28T03:10:25.368028Z","iopub.status.idle":"2022-07-28T03:10:25.708642Z","shell.execute_reply.started":"2022-07-28T03:10:25.367982Z","shell.execute_reply":"2022-07-28T03:10:25.707732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2> 5. RandomForest","metadata":{}},{"cell_type":"code","source":"model5 = RandomForestRegressor()\nmodel5.fit(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:10:25.709847Z","iopub.execute_input":"2022-07-28T03:10:25.710489Z","iopub.status.idle":"2022-07-28T03:10:27.178191Z","shell.execute_reply.started":"2022-07-28T03:10:25.710452Z","shell.execute_reply":"2022-07-28T03:10:27.177337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf_perf = model_performance(model5,model5)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:10:27.179416Z","iopub.execute_input":"2022-07-28T03:10:27.179938Z","iopub.status.idle":"2022-07-28T03:10:27.646375Z","shell.execute_reply.started":"2022-07-28T03:10:27.179895Z","shell.execute_reply":"2022-07-28T03:10:27.645115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2>6. XGBoost","metadata":{}},{"cell_type":"code","source":"model6 = xgboost.XGBRegressor(colsample_bytree=0.4,gamma=0,learning_rate=0.07,max_depth=3,min_child_weight=1.5,n_estimators=10000,reg_alpha=0.75,reg_lambda=0.45,subsample=0.6,seed=42)\nmodel6.fit(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:10:27.648722Z","iopub.execute_input":"2022-07-28T03:10:27.649235Z","iopub.status.idle":"2022-07-28T03:11:01.536606Z","shell.execute_reply.started":"2022-07-28T03:10:27.649189Z","shell.execute_reply":"2022-07-28T03:11:01.535777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xgb_perf = model_performance(model6,model6)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:11:01.537901Z","iopub.execute_input":"2022-07-28T03:11:01.538565Z","iopub.status.idle":"2022-07-28T03:11:02.080302Z","shell.execute_reply.started":"2022-07-28T03:11:01.538530Z","shell.execute_reply":"2022-07-28T03:11:02.078752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Creating a Model and Making Predictions for the competition","metadata":{}},{"cell_type":"code","source":"model = xgboost.XGBRegressor(colsample_bytree=0.4,gamma=0,learning_rate=0.07,max_depth=3,min_child_weight=1.5,n_estimators=10000,reg_alpha=0.75,reg_lambda=0.45,subsample=0.6,seed=42)\nmodel.fit(X,y)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:11:02.081815Z","iopub.execute_input":"2022-07-28T03:11:02.082182Z","iopub.status.idle":"2022-07-28T03:11:39.126058Z","shell.execute_reply.started":"2022-07-28T03:11:02.082152Z","shell.execute_reply":"2022-07-28T03:11:39.124454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model2 = RandomForestRegressor()\nmodel2.fit(X,y)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:18:14.001794Z","iopub.execute_input":"2022-07-28T03:18:14.002333Z","iopub.status.idle":"2022-07-28T03:18:16.117837Z","shell.execute_reply.started":"2022-07-28T03:18:14.002294Z","shell.execute_reply":"2022-07-28T03:18:16.116352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = model.predict(test)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:11:39.128220Z","iopub.execute_input":"2022-07-28T03:11:39.128837Z","iopub.status.idle":"2022-07-28T03:11:39.218021Z","shell.execute_reply.started":"2022-07-28T03:11:39.128790Z","shell.execute_reply":"2022-07-28T03:11:39.216994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction2 = model2.predict(test)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:18:30.561956Z","iopub.execute_input":"2022-07-28T03:18:30.562637Z","iopub.status.idle":"2022-07-28T03:18:30.613704Z","shell.execute_reply.started":"2022-07-28T03:18:30.562601Z","shell.execute_reply":"2022-07-28T03:18:30.612290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = (prediction+prediction2)/2","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:18:52.660588Z","iopub.execute_input":"2022-07-28T03:18:52.661161Z","iopub.status.idle":"2022-07-28T03:18:52.668486Z","shell.execute_reply.started":"2022-07-28T03:18:52.661115Z","shell.execute_reply":"2022-07-28T03:18:52.666583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'Id':pid,'SalePrice': prediction})\noutput.to_csv('House_prices.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:18:55.801692Z","iopub.execute_input":"2022-07-28T03:18:55.802667Z","iopub.status.idle":"2022-07-28T03:18:55.816297Z","shell.execute_reply.started":"2022-07-28T03:18:55.802616Z","shell.execute_reply":"2022-07-28T03:18:55.814671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output","metadata":{"execution":{"iopub.status.busy":"2022-07-28T03:18:56.912098Z","iopub.execute_input":"2022-07-28T03:18:56.913356Z","iopub.status.idle":"2022-07-28T03:18:56.927792Z","shell.execute_reply.started":"2022-07-28T03:18:56.913312Z","shell.execute_reply":"2022-07-28T03:18:56.926625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}