{"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 pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LinearRegression,LassoCV\nfrom sklearn.preprocessing import StandardScaler, RobustScaler\nfrom sklearn.pipeline import make_pipeline\nfrom sklearn.metrics import mean_absolute_error \nfrom sklearn.metrics import mean_squared_error \nfrom sklearn.metrics import median_absolute_error\nimport math\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-21T00:35:31.481470Z","iopub.execute_input":"2022-07-21T00:35:31.481919Z","iopub.status.idle":"2022-07-21T00:35:31.488551Z","shell.execute_reply.started":"2022-07-21T00:35:31.481884Z","shell.execute_reply":"2022-07-21T00:35:31.487390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datatrain=pd.read_csv(\"../input/house-prices-advanced-regression-techniques/train.csv\")\ndatatrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T00:08:01.602267Z","iopub.execute_input":"2022-07-21T00:08:01.603348Z","iopub.status.idle":"2022-07-21T00:08:01.659772Z","shell.execute_reply.started":"2022-07-21T00:08:01.603303Z","shell.execute_reply":"2022-07-21T00:08:01.658724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datatest=pd.read_csv(\"../input/house-prices-advanced-regression-techniques/test.csv\")\ndatatest.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T00:08:01.661059Z","iopub.execute_input":"2022-07-21T00:08:01.661353Z","iopub.status.idle":"2022-07-21T00:08:01.705038Z","shell.execute_reply.started":"2022-07-21T00:08:01.661328Z","shell.execute_reply":"2022-07-21T00:08:01.703941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datatrain.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T00:08:01.707288Z","iopub.execute_input":"2022-07-21T00:08:01.707633Z","iopub.status.idle":"2022-07-21T00:08:01.731002Z","shell.execute_reply.started":"2022-07-21T00:08:01.707603Z","shell.execute_reply":"2022-07-21T00:08:01.729807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datatrain.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T00:08:01.732319Z","iopub.execute_input":"2022-07-21T00:08:01.733413Z","iopub.status.idle":"2022-07-21T00:08:01.834158Z","shell.execute_reply.started":"2022-07-21T00:08:01.733365Z","shell.execute_reply":"2022-07-21T00:08:01.833047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datatest.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T00:08:01.835444Z","iopub.execute_input":"2022-07-21T00:08:01.835772Z","iopub.status.idle":"2022-07-21T00:08:01.941336Z","shell.execute_reply.started":"2022-07-21T00:08:01.835740Z","shell.execute_reply":"2022-07-21T00:08:01.940188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#check null value\n\ndatatrain.isnull().sum()\n","metadata":{"execution":{"iopub.status.busy":"2022-07-21T00:08:01.942577Z","iopub.execute_input":"2022-07-21T00:08:01.943146Z","iopub.status.idle":"2022-07-21T00:08:01.955774Z","shell.execute_reply.started":"2022-07-21T00:08:01.943116Z","shell.execute_reply":"2022-07-21T00:08:01.954770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(datatrain.isnull(),yticklabels=False,cbar=False,cmap='viridis')\n","metadata":{"execution":{"iopub.status.busy":"2022-07-21T00:08:01.956873Z","iopub.execute_input":"2022-07-21T00:08:01.958012Z","iopub.status.idle":"2022-07-21T00:08:02.354061Z","shell.execute_reply.started":"2022-07-21T00:08:01.957948Z","shell.execute_reply":"2022-07-21T00:08:02.352761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datatrain.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-21T00:08:02.355408Z","iopub.execute_input":"2022-07-21T00:08:02.355697Z","iopub.status.idle":"2022-07-21T00:08:02.362820Z","shell.execute_reply.started":"2022-07-21T00:08:02.355672Z","shell.execute_reply":"2022-07-21T00:08:02.361688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datatrain.mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T00:08:02.366879Z","iopub.execute_input":"2022-07-21T00:08:02.367638Z","iopub.status.idle":"2022-07-21T00:08:02.404126Z","shell.execute_reply.started":"2022-07-21T00:08:02.367590Z","shell.execute_reply":"2022-07-21T00:08:02.403259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dt=datatrain.replace(np.NAN,datatrain.mean()).head(15)\ndt","metadata":{"execution":{"iopub.status.busy":"2022-07-21T00:08:03.545138Z","iopub.execute_input":"2022-07-21T00:08:03.545741Z","iopub.status.idle":"2022-07-21T00:08:03.605547Z","shell.execute_reply.started":"2022-07-21T00:08:03.545705Z","shell.execute_reply":"2022-07-21T00:08:03.604471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dt.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-21T00:08:03.607457Z","iopub.execute_input":"2022-07-21T00:08:03.607779Z","iopub.status.idle":"2022-07-21T00:08:03.615430Z","shell.execute_reply.started":"2022-07-21T00:08:03.607750Z","shell.execute_reply":"2022-07-21T00:08:03.614030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datatest.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-21T00:08:03.617125Z","iopub.execute_input":"2022-07-21T00:08:03.618245Z","iopub.status.idle":"2022-07-21T00:08:03.628019Z","shell.execute_reply.started":"2022-07-21T00:08:03.618201Z","shell.execute_reply":"2022-07-21T00:08:03.626559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dtest=datatrain.replace(np.NAN,datatrain.mean()).head(15)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-21T00:08:03.629613Z","iopub.execute_input":"2022-07-21T00:08:03.630089Z","iopub.status.idle":"2022-07-21T00:08:03.675473Z","shell.execute_reply.started":"2022-07-21T00:08:03.630044Z","shell.execute_reply":"2022-07-21T00:08:03.674269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"categorical_columns_train = []\nfor i in dt.columns:\n    if dt[i].dtypes == 'object':\n        categorical_columns_train.append(i)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T00:08:03.677606Z","iopub.execute_input":"2022-07-21T00:08:03.677927Z","iopub.status.idle":"2022-07-21T00:08:03.686260Z","shell.execute_reply.started":"2022-07-21T00:08:03.677898Z","shell.execute_reply":"2022-07-21T00:08:03.685277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"categorical_columns_train","metadata":{"execution":{"iopub.status.busy":"2022-07-21T00:08:03.687711Z","iopub.execute_input":"2022-07-21T00:08:03.688748Z","iopub.status.idle":"2022-07-21T00:08:03.702845Z","shell.execute_reply.started":"2022-07-21T00:08:03.688702Z","shell.execute_reply":"2022-07-21T00:08:03.701364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#exclude = ['ExterQual','ExterCond','BsmtQual','BsmtCond','HeatingQC','KitchenQual','GarageQual','GarageCond','BsmtFinType1','BsmtFinType2','BsmtExposure']\nif  categorical_columns_train:\n        area_dummies = pd.get_dummies(dt,columns=categorical_columns_train,drop_first='True')\n       ","metadata":{"execution":{"iopub.status.busy":"2022-07-21T00:08:03.704417Z","iopub.execute_input":"2022-07-21T00:08:03.705667Z","iopub.status.idle":"2022-07-21T00:08:03.743642Z","shell.execute_reply.started":"2022-07-21T00:08:03.705569Z","shell.execute_reply":"2022-07-21T00:08:03.742462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dt2=area_dummies.copy()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T00:08:03.747511Z","iopub.execute_input":"2022-07-21T00:08:03.747826Z","iopub.status.idle":"2022-07-21T00:08:03.753046Z","shell.execute_reply.started":"2022-07-21T00:08:03.747799Z","shell.execute_reply":"2022-07-21T00:08:03.752123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dt2","metadata":{"execution":{"iopub.status.busy":"2022-07-21T00:08:04.095900Z","iopub.execute_input":"2022-07-21T00:08:04.096623Z","iopub.status.idle":"2022-07-21T00:08:04.120074Z","shell.execute_reply.started":"2022-07-21T00:08:04.096587Z","shell.execute_reply":"2022-07-21T00:08:04.118929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dt2.drop(\"Id\",axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T00:08:04.122573Z","iopub.execute_input":"2022-07-21T00:08:04.123397Z","iopub.status.idle":"2022-07-21T00:08:04.152032Z","shell.execute_reply.started":"2022-07-21T00:08:04.123351Z","shell.execute_reply":"2022-07-21T00:08:04.150884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=dt2.drop(\"SalePrice\",axis=1)\ny=dt2.loc[:,\"SalePrice\"]\ny=np.array(y).reshape(-1,1)\ny[:10]","metadata":{"execution":{"iopub.status.busy":"2022-07-21T00:08:04.204355Z","iopub.execute_input":"2022-07-21T00:08:04.204932Z","iopub.status.idle":"2022-07-21T00:08:04.213478Z","shell.execute_reply.started":"2022-07-21T00:08:04.204899Z","shell.execute_reply":"2022-07-21T00:08:04.212414Z"},"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.45, random_state=44, shuffle =True)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-21T00:08:44.725059Z","iopub.execute_input":"2022-07-21T00:08:44.725416Z","iopub.status.idle":"2022-07-21T00:08:44.732294Z","shell.execute_reply.started":"2022-07-21T00:08:44.725386Z","shell.execute_reply":"2022-07-21T00:08:44.731022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"linear_reg_pipe = make_pipeline(RobustScaler(), LinearRegression())\nlinear_reg_pipe.fit(X_train,y_train)\nlinear_reg_pipe.score(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T00:13:16.747829Z","iopub.execute_input":"2022-07-21T00:13:16.748240Z","iopub.status.idle":"2022-07-21T00:13:16.778159Z","shell.execute_reply.started":"2022-07-21T00:13:16.748205Z","shell.execute_reply":"2022-07-21T00:13:16.776762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=linear_reg_pipe.predict(X_test)\ny_pred","metadata":{"execution":{"iopub.status.busy":"2022-07-21T00:17:02.035185Z","iopub.execute_input":"2022-07-21T00:17:02.035619Z","iopub.status.idle":"2022-07-21T00:17:02.046469Z","shell.execute_reply.started":"2022-07-21T00:17:02.035583Z","shell.execute_reply":"2022-07-21T00:17:02.045486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MAEValue = mean_absolute_error(y_test, y_pred, multioutput='uniform_average') # it can be raw_values\nprint('Mean Absolute Error Value is : ', MAEValue)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T00:17:21.869955Z","iopub.execute_input":"2022-07-21T00:17:21.870453Z","iopub.status.idle":"2022-07-21T00:17:21.877681Z","shell.execute_reply.started":"2022-07-21T00:17:21.870414Z","shell.execute_reply":"2022-07-21T00:17:21.876483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MSEValue = mean_squared_error(y_test, y_pred, multioutput='uniform_average') # it can be raw_values\nprint('Mean Squared Error Value is : ', MSEValue)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T00:17:41.040316Z","iopub.execute_input":"2022-07-21T00:17:41.040719Z","iopub.status.idle":"2022-07-21T00:17:41.047452Z","shell.execute_reply.started":"2022-07-21T00:17:41.040689Z","shell.execute_reply":"2022-07-21T00:17:41.046537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"linear_reg_pipe.score(X_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T00:18:35.681948Z","iopub.execute_input":"2022-07-21T00:18:35.682334Z","iopub.status.idle":"2022-07-21T00:18:35.693137Z","shell.execute_reply.started":"2022-07-21T00:18:35.682305Z","shell.execute_reply":"2022-07-21T00:18:35.691872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"RMSE = math.sqrt(MSEValue)\nprint(\"RMES\",RMSE)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T00:36:08.055314Z","iopub.execute_input":"2022-07-21T00:36:08.056400Z","iopub.status.idle":"2022-07-21T00:36:08.060908Z","shell.execute_reply.started":"2022-07-21T00:36:08.056359Z","shell.execute_reply":"2022-07-21T00:36:08.059651Z"},"trusted":true},"execution_count":null,"outputs":[]}]}