{"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":"import numpy as np\nimport pandas as pd","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-26T04:11:34.035445Z","iopub.execute_input":"2022-07-26T04:11:34.036577Z","iopub.status.idle":"2022-07-26T04:11:34.064563Z","shell.execute_reply.started":"2022-07-26T04:11:34.036442Z","shell.execute_reply":"2022-07-26T04:11:34.063593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv('../input/house-prices-advanced-regression-techniques/test.csv')\ntest=pd.read_csv('../input/house-prices-advanced-regression-techniques/train.csv')","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-26T04:11:34.066560Z","iopub.execute_input":"2022-07-26T04:11:34.067567Z","iopub.status.idle":"2022-07-26T04:11:34.141509Z","shell.execute_reply.started":"2022-07-26T04:11:34.067528Z","shell.execute_reply":"2022-07-26T04:11:34.140250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape,test.shape","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-26T04:11:34.143364Z","iopub.execute_input":"2022-07-26T04:11:34.143937Z","iopub.status.idle":"2022-07-26T04:11:34.152598Z","shell.execute_reply.started":"2022-07-26T04:11:34.143902Z","shell.execute_reply":"2022-07-26T04:11:34.151544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.columns","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-26T04:11:34.154054Z","iopub.execute_input":"2022-07-26T04:11:34.154488Z","iopub.status.idle":"2022-07-26T04:11:34.167018Z","shell.execute_reply.started":"2022-07-26T04:11:34.154444Z","shell.execute_reply":"2022-07-26T04:11:34.166013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#conacte tarin and test\ndf=pd.concat([train,test])\ndf.shape","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-26T04:11:34.169246Z","iopub.execute_input":"2022-07-26T04:11:34.170337Z","iopub.status.idle":"2022-07-26T04:11:34.197349Z","shell.execute_reply.started":"2022-07-26T04:11:34.170299Z","shell.execute_reply":"2022-07-26T04:11:34.196572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-26T04:11:34.198364Z","iopub.execute_input":"2022-07-26T04:11:34.198913Z","iopub.status.idle":"2022-07-26T04:11:34.230731Z","shell.execute_reply.started":"2022-07-26T04:11:34.198872Z","shell.execute_reply":"2022-07-26T04:11:34.229620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-26T04:11:34.232365Z","iopub.execute_input":"2022-07-26T04:11:34.233624Z","iopub.status.idle":"2022-07-26T04:11:34.366750Z","shell.execute_reply.started":"2022-07-26T04:11:34.233573Z","shell.execute_reply":"2022-07-26T04:11:34.365575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#how is data look like\ndf.info()","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-26T04:11:34.368471Z","iopub.execute_input":"2022-07-26T04:11:34.368941Z","iopub.status.idle":"2022-07-26T04:11:34.407462Z","shell.execute_reply.started":"2022-07-26T04:11:34.368897Z","shell.execute_reply":"2022-07-26T04:11:34.406702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-26T04:11:34.408636Z","iopub.execute_input":"2022-07-26T04:11:34.409643Z","iopub.status.idle":"2022-07-26T04:11:35.479870Z","shell.execute_reply.started":"2022-07-26T04:11:34.409606Z","shell.execute_reply":"2022-07-26T04:11:35.478565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#cheak null values\ndf.isnull().sum()","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-26T04:11:35.481393Z","iopub.execute_input":"2022-07-26T04:11:35.481780Z","iopub.status.idle":"2022-07-26T04:11:35.507327Z","shell.execute_reply.started":"2022-07-26T04:11:35.481745Z","shell.execute_reply":"2022-07-26T04:11:35.506440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Id'].dtypes","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-26T04:11:35.511346Z","iopub.execute_input":"2022-07-26T04:11:35.511895Z","iopub.status.idle":"2022-07-26T04:11:35.518116Z","shell.execute_reply.started":"2022-07-26T04:11:35.511854Z","shell.execute_reply":"2022-07-26T04:11:35.517147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numeric_dtype=['int64','float64']","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-26T04:11:35.519484Z","iopub.execute_input":"2022-07-26T04:11:35.520065Z","iopub.status.idle":"2022-07-26T04:11:35.527900Z","shell.execute_reply.started":"2022-07-26T04:11:35.520033Z","shell.execute_reply":"2022-07-26T04:11:35.526747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for column in df.columns:\n    if df[column].dtype not in numeric_dtype:\n        print(column,df[column].value_counts())","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-26T04:11:35.529484Z","iopub.execute_input":"2022-07-26T04:11:35.530095Z","iopub.status.idle":"2022-07-26T04:11:35.591063Z","shell.execute_reply.started":"2022-07-26T04:11:35.530062Z","shell.execute_reply":"2022-07-26T04:11:35.589878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#impute missing values\nfrom sklearn.impute import SimpleImputer\nimpute_numreric=SimpleImputer(strategy='mean')\nimpute_cat=SimpleImputer(strategy='most_frequent')\n\nfor column in df.columns:\n    if df[column].dtype in numeric_dtype and df[column].isnull().sum()!=0:\n        df[column]=impute_numreric.fit_transform(df[[column]])\n    elif df[column].dtype not in numeric_dtype and df[column].isnull().sum()!=0:\n        df[column]=impute_cat.fit_transform(df[[column]])","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-26T04:11:35.592831Z","iopub.execute_input":"2022-07-26T04:11:35.593699Z","iopub.status.idle":"2022-07-26T04:11:36.104983Z","shell.execute_reply.started":"2022-07-26T04:11:35.593626Z","shell.execute_reply":"2022-07-26T04:11:36.103733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#no missing values in data\ndf.isnull().sum()","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-26T04:11:36.106793Z","iopub.execute_input":"2022-07-26T04:11:36.107518Z","iopub.status.idle":"2022-07-26T04:11:36.136616Z","shell.execute_reply.started":"2022-07-26T04:11:36.107471Z","shell.execute_reply":"2022-07-26T04:11:36.135372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#encoding data\nfrom sklearn.preprocessing import LabelEncoder\nlbl=LabelEncoder()\nfor column in df.columns:\n    if df[column].dtype not in numeric_dtype:\n        df[column]=lbl.fit_transform(df[column])","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-26T04:11:36.138609Z","iopub.execute_input":"2022-07-26T04:11:36.139629Z","iopub.status.idle":"2022-07-26T04:11:36.232627Z","shell.execute_reply.started":"2022-07-26T04:11:36.139577Z","shell.execute_reply":"2022-07-26T04:11:36.231368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-26T04:11:36.234325Z","iopub.execute_input":"2022-07-26T04:11:36.235078Z","iopub.status.idle":"2022-07-26T04:11:36.272416Z","shell.execute_reply.started":"2022-07-26T04:11:36.235028Z","shell.execute_reply":"2022-07-26T04:11:36.271273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#import Regression algorithm\nfrom sklearn.linear_model import LinearRegression,Ridge,Lasso\nfrom sklearn.tree import DecisionTreeRegressor,plot_tree\nfrom sklearn.ensemble import VotingRegressor,BaggingRegressor,RandomForestRegressor\nfrom sklearn.preprocessing import StandardScaler,MinMaxScaler\nfrom sklearn.decomposition import PCA\nfrom sklearn.model_selection import train_test_split,cross_val_score\nfrom sklearn.metrics import r2_score,mean_squared_error,mean_absolute_error","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-26T04:11:36.274193Z","iopub.execute_input":"2022-07-26T04:11:36.274644Z","iopub.status.idle":"2022-07-26T04:11:36.375775Z","shell.execute_reply.started":"2022-07-26T04:11:36.274599Z","shell.execute_reply":"2022-07-26T04:11:36.374829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df=df\n","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-26T04:11:36.377015Z","iopub.execute_input":"2022-07-26T04:11:36.377501Z","iopub.status.idle":"2022-07-26T04:11:36.381636Z","shell.execute_reply.started":"2022-07-26T04:11:36.377470Z","shell.execute_reply":"2022-07-26T04:11:36.380744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=new_df.drop('SalePrice',axis=1)\ny=df['SalePrice']","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-26T04:11:36.382825Z","iopub.execute_input":"2022-07-26T04:11:36.383567Z","iopub.status.idle":"2022-07-26T04:11:36.399233Z","shell.execute_reply.started":"2022-07-26T04:11:36.383535Z","shell.execute_reply":"2022-07-26T04:11:36.398255Z"},"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=4)","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-26T04:11:36.400654Z","iopub.execute_input":"2022-07-26T04:11:36.401255Z","iopub.status.idle":"2022-07-26T04:11:36.410495Z","shell.execute_reply.started":"2022-07-26T04:11:36.401223Z","shell.execute_reply":"2022-07-26T04:11:36.409572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr=LinearRegression()\nrd=Ridge(alpha=10)\nls=Lasso(alpha=10)\nlr.fit(x_train,y_train)\nrd.fit(x_train,y_train)\nls.fit(x_train,y_train)\ny_prde_lr=lr.predict(x_test)\ny_prde_rd=rd.predict(x_test)\ny_prde_ls=ls.predict(x_test)","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-26T04:11:36.411832Z","iopub.execute_input":"2022-07-26T04:11:36.412378Z","iopub.status.idle":"2022-07-26T04:11:36.703256Z","shell.execute_reply.started":"2022-07-26T04:11:36.412346Z","shell.execute_reply":"2022-07-26T04:11:36.695327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"r2_acore\")\nprint(\"LinearRegression:\",mean_absolute_error(y_prde_lr,y_test))\nprint(\"Ridg:\",mean_absolute_error(y_prde_rd,y_test))\nprint(\"Lasso:\",mean_absolute_error(y_prde_ls,y_test))","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-26T04:11:36.705579Z","iopub.execute_input":"2022-07-26T04:11:36.709004Z","iopub.status.idle":"2022-07-26T04:11:36.732933Z","shell.execute_reply.started":"2022-07-26T04:11:36.708946Z","shell.execute_reply":"2022-07-26T04:11:36.730888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#79 percent accuracy with Decision Tree\ndt=DecisionTreeRegressor()\ndt.fit(x_train,y_train)\ny_pred_dt=dt.predict(x_test)\nr2_score(y_pred_dt,y_test)","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-26T04:11:36.736595Z","iopub.execute_input":"2022-07-26T04:11:36.738903Z","iopub.status.idle":"2022-07-26T04:11:36.856538Z","shell.execute_reply.started":"2022-07-26T04:11:36.738850Z","shell.execute_reply":"2022-07-26T04:11:36.855539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#accurate result\ncross_val_score(dt,x_train,y_train,cv=5).mean()","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-26T04:11:36.858896Z","iopub.execute_input":"2022-07-26T04:11:36.860036Z","iopub.status.idle":"2022-07-26T04:11:37.107648Z","shell.execute_reply.started":"2022-07-26T04:11:36.859973Z","shell.execute_reply":"2022-07-26T04:11:37.106649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#applying random forest\nrf=RandomForestRegressor(n_estimators=500,n_jobs=-1,min_samples_split=3,max_depth=20)\nrf.fit(x_train,y_train)\nrf.feature_importances_","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-26T04:11:37.109086Z","iopub.execute_input":"2022-07-26T04:11:37.109902Z","iopub.status.idle":"2022-07-26T04:11:42.425250Z","shell.execute_reply.started":"2022-07-26T04:11:37.109854Z","shell.execute_reply":"2022-07-26T04:11:42.424095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=rf.predict(x_test)\nr2_score(y_pred,y_test)\ny_pred","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-26T04:11:42.427036Z","iopub.execute_input":"2022-07-26T04:11:42.427619Z","iopub.status.idle":"2022-07-26T04:11:42.646919Z","shell.execute_reply.started":"2022-07-26T04:11:42.427587Z","shell.execute_reply":"2022-07-26T04:11:42.645755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#cross val score\ncross_val_score(rf,x_train,y_train,cv=10).mean()","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-26T04:11:42.651522Z","iopub.execute_input":"2022-07-26T04:11:42.652315Z","iopub.status.idle":"2022-07-26T04:12:29.820126Z","shell.execute_reply.started":"2022-07-26T04:11:42.652273Z","shell.execute_reply":"2022-07-26T04:12:29.818730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'Id':x_test['Id'].values,'SalePrice':y_pred})\nsubmission","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-26T04:12:29.821480Z","iopub.execute_input":"2022-07-26T04:12:29.821836Z","iopub.status.idle":"2022-07-26T04:12:29.837273Z","shell.execute_reply.started":"2022-07-26T04:12:29.821805Z","shell.execute_reply":"2022-07-26T04:12:29.836471Z"},"trusted":true},"execution_count":null,"outputs":[]}]}