{"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)\n\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-02T13:27:49.780583Z","iopub.execute_input":"2022-08-02T13:27:49.781079Z","iopub.status.idle":"2022-08-02T13:27:49.791549Z","shell.execute_reply.started":"2022-08-02T13:27:49.781045Z","shell.execute_reply":"2022-08-02T13:27:49.790356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Importing","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\nwarnings.filterwarnings('ignore')\npd.set_option('display.max_columns',100)\npd.set_option('display.max_rows',100)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:27:49.940473Z","iopub.execute_input":"2022-08-02T13:27:49.941278Z","iopub.status.idle":"2022-08-02T13:27:49.947874Z","shell.execute_reply.started":"2022-08-02T13:27:49.941214Z","shell.execute_reply":"2022-08-02T13:27:49.946883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# importing train data\ntrain_data=pd.read_csv(r'../input/house-prices-advanced-regression-techniques/train.csv')\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:27:50.159920Z","iopub.execute_input":"2022-08-02T13:27:50.160416Z","iopub.status.idle":"2022-08-02T13:27:50.240075Z","shell.execute_reply.started":"2022-08-02T13:27:50.160375Z","shell.execute_reply":"2022-08-02T13:27:50.239214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# importing test data\ntest_data=pd.read_csv(r'../input/house-prices-advanced-regression-techniques/test.csv')\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:27:50.421498Z","iopub.execute_input":"2022-08-02T13:27:50.422219Z","iopub.status.idle":"2022-08-02T13:27:50.510894Z","shell.execute_reply.started":"2022-08-02T13:27:50.422170Z","shell.execute_reply":"2022-08-02T13:27:50.509647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.shape,test_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:27:50.579645Z","iopub.execute_input":"2022-08-02T13:27:50.580280Z","iopub.status.idle":"2022-08-02T13:27:50.586065Z","shell.execute_reply.started":"2022-08-02T13:27:50.580243Z","shell.execute_reply":"2022-08-02T13:27:50.585165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:27:50.809692Z","iopub.execute_input":"2022-08-02T13:27:50.810374Z","iopub.status.idle":"2022-08-02T13:27:50.935065Z","shell.execute_reply.started":"2022-08-02T13:27:50.810336Z","shell.execute_reply":"2022-08-02T13:27:50.933985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:27:51.029352Z","iopub.execute_input":"2022-08-02T13:27:51.030062Z","iopub.status.idle":"2022-08-02T13:27:51.148058Z","shell.execute_reply.started":"2022-08-02T13:27:51.030022Z","shell.execute_reply":"2022-08-02T13:27:51.146978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Preprocessing","metadata":{}},{"cell_type":"code","source":"# checking for variance on train data\ntrain_data.var()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:27:51.260945Z","iopub.execute_input":"2022-08-02T13:27:51.261341Z","iopub.status.idle":"2022-08-02T13:27:51.283493Z","shell.execute_reply.started":"2022-08-02T13:27:51.261309Z","shell.execute_reply":"2022-08-02T13:27:51.282205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"there is no feature with zero variance so no need to remove any feature till now.","metadata":{}},{"cell_type":"code","source":"train_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:27:51.439504Z","iopub.execute_input":"2022-08-02T13:27:51.440622Z","iopub.status.idle":"2022-08-02T13:27:51.461786Z","shell.execute_reply.started":"2022-08-02T13:27:51.440582Z","shell.execute_reply":"2022-08-02T13:27:51.460685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:27:51.648452Z","iopub.execute_input":"2022-08-02T13:27:51.648844Z","iopub.status.idle":"2022-08-02T13:27:51.673054Z","shell.execute_reply.started":"2022-08-02T13:27:51.648813Z","shell.execute_reply":"2022-08-02T13:27:51.671831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# columns with categorical data\ncategory_cols=train_data.select_dtypes(include='object').columns\ncategory_cols","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:27:51.819685Z","iopub.execute_input":"2022-08-02T13:27:51.821005Z","iopub.status.idle":"2022-08-02T13:27:51.833869Z","shell.execute_reply.started":"2022-08-02T13:27:51.820953Z","shell.execute_reply":"2022-08-02T13:27:51.832503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checking null values of train data\ntrain_data.isnull().sum()[train_data.isnull().sum()>0].sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:27:51.961282Z","iopub.execute_input":"2022-08-02T13:27:51.962482Z","iopub.status.idle":"2022-08-02T13:27:51.984181Z","shell.execute_reply.started":"2022-08-02T13:27:51.962415Z","shell.execute_reply":"2022-08-02T13:27:51.982935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # checking null values of test data\ntest_data.isnull().sum()[test_data.isnull().sum()>0].sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:27:52.108529Z","iopub.execute_input":"2022-08-02T13:27:52.108937Z","iopub.status.idle":"2022-08-02T13:27:52.128688Z","shell.execute_reply.started":"2022-08-02T13:27:52.108902Z","shell.execute_reply":"2022-08-02T13:27:52.127832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"as per observation,PoolQC,MiscFeature,Alley and Fence feature has almost all null values so can be dropped.","metadata":{}},{"cell_type":"code","source":"# dropping unnecessary columns\ntrain_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-02T13:27:52.289695Z","iopub.execute_input":"2022-08-02T13:27:52.290449Z","iopub.status.idle":"2022-08-02T13:27:52.302740Z","shell.execute_reply.started":"2022-08-02T13:27:52.290402Z","shell.execute_reply":"2022-08-02T13:27:52.301459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checking categorical columns with null values for train data\nnull_val_cat_cols_train=[]\nfor a in train_data.isnull().sum()[train_data.isnull().sum()>0].index:\n    if a in category_cols:\n        null_val_cat_cols_train.append(a)\nnull_val_cat_cols_train","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:27:52.400421Z","iopub.execute_input":"2022-08-02T13:27:52.400854Z","iopub.status.idle":"2022-08-02T13:27:52.419482Z","shell.execute_reply.started":"2022-08-02T13:27:52.400818Z","shell.execute_reply":"2022-08-02T13:27:52.418499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# filling null values of categorical column of train data\nfor i in null_val_cat_cols_train:\n    train_data[i].fillna(method='ffill',inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:27:52.587994Z","iopub.execute_input":"2022-08-02T13:27:52.588396Z","iopub.status.idle":"2022-08-02T13:27:52.603719Z","shell.execute_reply.started":"2022-08-02T13:27:52.588363Z","shell.execute_reply":"2022-08-02T13:27:52.602403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data[null_val_cat_cols_train].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:27:52.788253Z","iopub.execute_input":"2022-08-02T13:27:52.789343Z","iopub.status.idle":"2022-08-02T13:27:52.801286Z","shell.execute_reply.started":"2022-08-02T13:27:52.789282Z","shell.execute_reply":"2022-08-02T13:27:52.800399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.dropna(subset=['FireplaceQu'],axis=0,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:27:52.998457Z","iopub.execute_input":"2022-08-02T13:27:52.999329Z","iopub.status.idle":"2022-08-02T13:27:53.008180Z","shell.execute_reply.started":"2022-08-02T13:27:52.999291Z","shell.execute_reply":"2022-08-02T13:27:53.007064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data[null_val_cat_cols_train].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:27:53.239314Z","iopub.execute_input":"2022-08-02T13:27:53.239786Z","iopub.status.idle":"2022-08-02T13:27:53.252381Z","shell.execute_reply.started":"2022-08-02T13:27:53.239748Z","shell.execute_reply":"2022-08-02T13:27:53.251375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# filling 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)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:27:53.380354Z","iopub.execute_input":"2022-08-02T13:27:53.381618Z","iopub.status.idle":"2022-08-02T13:27:53.403943Z","shell.execute_reply.started":"2022-08-02T13:27:53.381562Z","shell.execute_reply":"2022-08-02T13:27:53.402513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:27:53.519084Z","iopub.execute_input":"2022-08-02T13:27:53.519983Z","iopub.status.idle":"2022-08-02T13:27:53.536307Z","shell.execute_reply.started":"2022-08-02T13:27:53.519927Z","shell.execute_reply":"2022-08-02T13:27:53.534862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checking categorical columns with null values for test data\nnull_val_cat_cols_test=[]\nfor a in test_data.isnull().sum()[test_data.isnull().sum()>0].index:\n    if a in category_cols:\n        null_val_cat_cols_test.append(a)\nnull_val_cat_cols_test","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:27:53.670469Z","iopub.execute_input":"2022-08-02T13:27:53.670940Z","iopub.status.idle":"2022-08-02T13:27:53.689935Z","shell.execute_reply.started":"2022-08-02T13:27:53.670907Z","shell.execute_reply":"2022-08-02T13:27:53.688715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# filling null values of categorical column of test data\nfor i in null_val_cat_cols_test:\n    test_data[i].fillna(method='ffill',inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:27:53.843710Z","iopub.execute_input":"2022-08-02T13:27:53.844193Z","iopub.status.idle":"2022-08-02T13:27:53.860529Z","shell.execute_reply.started":"2022-08-02T13:27:53.844158Z","shell.execute_reply":"2022-08-02T13:27:53.859536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data[null_val_cat_cols_test].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:27:53.989112Z","iopub.execute_input":"2022-08-02T13:27:53.990156Z","iopub.status.idle":"2022-08-02T13:27:54.002724Z","shell.execute_reply.started":"2022-08-02T13:27:53.990095Z","shell.execute_reply":"2022-08-02T13:27:54.001418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data['FireplaceQu'].fillna('Gd',inplace=True)\ntest_data[null_val_cat_cols_test].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:27:54.168396Z","iopub.execute_input":"2022-08-02T13:27:54.169139Z","iopub.status.idle":"2022-08-02T13:27:54.183593Z","shell.execute_reply.started":"2022-08-02T13:27:54.169080Z","shell.execute_reply":"2022-08-02T13:27:54.182535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# filling 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)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:27:54.399251Z","iopub.execute_input":"2022-08-02T13:27:54.399641Z","iopub.status.idle":"2022-08-02T13:27:54.430097Z","shell.execute_reply.started":"2022-08-02T13:27:54.399610Z","shell.execute_reply":"2022-08-02T13:27:54.429022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:27:54.558760Z","iopub.execute_input":"2022-08-02T13:27:54.559207Z","iopub.status.idle":"2022-08-02T13:27:54.573182Z","shell.execute_reply.started":"2022-08-02T13:27:54.559168Z","shell.execute_reply":"2022-08-02T13:27:54.571964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA","metadata":{}},{"cell_type":"code","source":"# checking for outliers for train data\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-02T13:27:54.789283Z","iopub.execute_input":"2022-08-02T13:27:54.790465Z","iopub.status.idle":"2022-08-02T13:28:04.207977Z","shell.execute_reply.started":"2022-08-02T13:27:54.790422Z","shell.execute_reply":"2022-08-02T13:28:04.206849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# removing 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-02T13:28:04.209828Z","iopub.execute_input":"2022-08-02T13:28:04.210262Z","iopub.status.idle":"2022-08-02T13:28:04.221504Z","shell.execute_reply.started":"2022-08-02T13:28:04.210229Z","shell.execute_reply":"2022-08-02T13:28:04.220579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Encoding categorical data","metadata":{}},{"cell_type":"code","source":"category_cols_new=train_data.select_dtypes(include='object').columns\ntrain_data_new=pd.get_dummies(train_data,columns=category_cols_new,prefix=category_cols_new)\ntest_data_new=pd.get_dummies(test_data,columns=category_cols_new,prefix=category_cols_new)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:28:04.222931Z","iopub.execute_input":"2022-08-02T13:28:04.223252Z","iopub.status.idle":"2022-08-02T13:28:04.308087Z","shell.execute_reply.started":"2022-08-02T13:28:04.223222Z","shell.execute_reply":"2022-08-02T13:28:04.306950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_new.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:28:04.310335Z","iopub.execute_input":"2022-08-02T13:28:04.310675Z","iopub.status.idle":"2022-08-02T13:28:04.365629Z","shell.execute_reply.started":"2022-08-02T13:28:04.310644Z","shell.execute_reply":"2022-08-02T13:28:04.364568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data_new.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:28:04.367241Z","iopub.execute_input":"2022-08-02T13:28:04.368150Z","iopub.status.idle":"2022-08-02T13:28:04.428105Z","shell.execute_reply.started":"2022-08-02T13:28:04.368075Z","shell.execute_reply":"2022-08-02T13:28:04.427332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_new.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:28:04.429524Z","iopub.execute_input":"2022-08-02T13:28:04.430083Z","iopub.status.idle":"2022-08-02T13:28:04.452551Z","shell.execute_reply.started":"2022-08-02T13:28:04.430048Z","shell.execute_reply":"2022-08-02T13:28:04.451353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data_new.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:28:04.454271Z","iopub.execute_input":"2022-08-02T13:28:04.454994Z","iopub.status.idle":"2022-08-02T13:28:04.478303Z","shell.execute_reply.started":"2022-08-02T13:28:04.454947Z","shell.execute_reply":"2022-08-02T13:28:04.477150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# train test split","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train,x_test,y_train,y_test=train_test_split(train_data_new.drop('SalePrice',axis=1),train_data_new['SalePrice'],test_size=0.20,random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:28:04.479944Z","iopub.execute_input":"2022-08-02T13:28:04.480667Z","iopub.status.idle":"2022-08-02T13:28:04.494201Z","shell.execute_reply.started":"2022-08-02T13:28:04.480621Z","shell.execute_reply":"2022-08-02T13:28:04.493000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape,y_train.shape,x_test.shape,y_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:28:04.495753Z","iopub.execute_input":"2022-08-02T13:28:04.496479Z","iopub.status.idle":"2022-08-02T13:28:04.503060Z","shell.execute_reply.started":"2022-08-02T13:28:04.496444Z","shell.execute_reply":"2022-08-02T13:28:04.502066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Scaling","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscale=StandardScaler()\nx_train_scale = scale.fit_transform(x_train)\nx_test_scale = scale.transform(x_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:28:04.506806Z","iopub.execute_input":"2022-08-02T13:28:04.507530Z","iopub.status.idle":"2022-08-02T13:28:04.536792Z","shell.execute_reply.started":"2022-08-02T13:28:04.507483Z","shell.execute_reply":"2022-08-02T13:28:04.535661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Feature Extraction","metadata":{}},{"cell_type":"code","source":"from sklearn.decomposition import PCA\nsklearn_pca = PCA(n_components=0.95)\nsklearn_pca.fit(x_train_scale)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:28:04.538241Z","iopub.execute_input":"2022-08-02T13:28:04.538592Z","iopub.status.idle":"2022-08-02T13:28:04.612628Z","shell.execute_reply.started":"2022-08-02T13:28:04.538559Z","shell.execute_reply":"2022-08-02T13:28:04.611063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_transformed = sklearn_pca.transform(x_train_scale)\nx_test_transformed =sklearn_pca.transform(x_test_scale)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:28:04.615000Z","iopub.execute_input":"2022-08-02T13:28:04.616007Z","iopub.status.idle":"2022-08-02T13:28:04.644007Z","shell.execute_reply.started":"2022-08-02T13:28:04.615944Z","shell.execute_reply":"2022-08-02T13:28:04.642075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_transformed.shape,x_test_transformed.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:28:04.647180Z","iopub.execute_input":"2022-08-02T13:28:04.649344Z","iopub.status.idle":"2022-08-02T13:28:04.664643Z","shell.execute_reply.started":"2022-08-02T13:28:04.649281Z","shell.execute_reply":"2022-08-02T13:28:04.663361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Modeling","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import r2_score,mean_squared_error,mean_absolute_percentage_error\nfrom sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor\nfrom sklearn.tree import DecisionTreeRegressor\nfrom sklearn.linear_model import LinearRegression\nfrom xgboost import XGBRegressor","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:28:04.668346Z","iopub.execute_input":"2022-08-02T13:28:04.669863Z","iopub.status.idle":"2022-08-02T13:28:04.682847Z","shell.execute_reply.started":"2022-08-02T13:28:04.669799Z","shell.execute_reply":"2022-08-02T13:28:04.681099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# creating 5 different models\nRF = RandomForestRegressor(random_state=42).fit(x_train_transformed, y_train.values)\nDT = DecisionTreeRegressor(random_state=42).fit(x_train_transformed, y_train.values)\nGBR = GradientBoostingRegressor(random_state=42).fit(x_train_transformed, y_train.values)\nLR = LinearRegression().fit(x_train_transformed, y_train.values)\nXGB = XGBRegressor().fit(x_train_transformed, y_train.values)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:28:04.685888Z","iopub.execute_input":"2022-08-02T13:28:04.687705Z","iopub.status.idle":"2022-08-02T13:28:20.502896Z","shell.execute_reply.started":"2022-08-02T13:28:04.687654Z","shell.execute_reply":"2022-08-02T13:28:20.501928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# the evaluation metrics\nmodels = [LR, DT, RF, GBR, XGB]\nRMSE = [mean_squared_error(y_test.values, mod.predict(x_test_transformed))**0.5 for mod in models]\nMAPE = [mean_absolute_percentage_error(y_test.values, mod.predict(x_test_transformed)) for mod in models]\nR2_Score = [r2_score(y_test.values, mod.predict(x_test_transformed)) for mod in models]","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:28:20.504303Z","iopub.execute_input":"2022-08-02T13:28:20.504862Z","iopub.status.idle":"2022-08-02T13:28:20.750534Z","shell.execute_reply.started":"2022-08-02T13:28:20.504826Z","shell.execute_reply":"2022-08-02T13:28:20.749608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# comparing 5 models\nModels = ['Linear Regression','Decision Tree','Random Forest','Gradient Boosting','XgBoost']\nevaluation = pd.DataFrame({'Models':Models,'RMSE':RMSE,'MAPE':MAPE, 'R2_Score':R2_Score})","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:28:20.751902Z","iopub.execute_input":"2022-08-02T13:28:20.752467Z","iopub.status.idle":"2022-08-02T13:28:20.760293Z","shell.execute_reply.started":"2022-08-02T13:28:20.752431Z","shell.execute_reply":"2022-08-02T13:28:20.759387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"evaluation","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:28:20.762225Z","iopub.execute_input":"2022-08-02T13:28:20.763800Z","iopub.status.idle":"2022-08-02T13:28:20.788152Z","shell.execute_reply.started":"2022-08-02T13:28:20.763757Z","shell.execute_reply":"2022-08-02T13:28:20.786573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"As per conclusion,Linear Regression model giving high accuracy of 89% and low rmse 22770 but Gradient Boosting model giving good prediction for test data with accuracy of 88% on train data.","metadata":{}},{"cell_type":"markdown","source":"# final submission","metadata":{}},{"cell_type":"code","source":"# including those columns which are not in test data after encoding\ncols=[]\nfor m in train_data_new.drop(columns='SalePrice',axis=1).columns:\n    if m not in test_data_new.columns:\n        cols.append(m)\ncols","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:28:20.790620Z","iopub.execute_input":"2022-08-02T13:28:20.791640Z","iopub.status.idle":"2022-08-02T13:28:20.809515Z","shell.execute_reply.started":"2022-08-02T13:28:20.791575Z","shell.execute_reply":"2022-08-02T13:28:20.807536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# adding these columns to the test data that was encoded to balance\nfor n in cols:\n    test_data_new[n]=0","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:28:20.811746Z","iopub.execute_input":"2022-08-02T13:28:20.812380Z","iopub.status.idle":"2022-08-02T13:28:20.823038Z","shell.execute_reply.started":"2022-08-02T13:28:20.812343Z","shell.execute_reply":"2022-08-02T13:28:20.822029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# excluding those columns which are not in train data after encoding\ncols2=[]\nfor c in test_data_new.columns:\n    if c not in train_data_new.drop(columns='SalePrice',axis=1).columns:\n        cols2.append(c)\ncols2","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:28:20.824237Z","iopub.execute_input":"2022-08-02T13:28:20.825525Z","iopub.status.idle":"2022-08-02T13:28:21.022168Z","shell.execute_reply.started":"2022-08-02T13:28:20.825487Z","shell.execute_reply":"2022-08-02T13:28:21.020970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dropping extra columns from test data which was encoded\ntest_data_new.drop(columns=cols2,axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:28:21.023737Z","iopub.execute_input":"2022-08-02T13:28:21.024251Z","iopub.status.idle":"2022-08-02T13:28:21.036032Z","shell.execute_reply.started":"2022-08-02T13:28:21.024203Z","shell.execute_reply":"2022-08-02T13:28:21.034876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data_new.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:28:21.037351Z","iopub.execute_input":"2022-08-02T13:28:21.037983Z","iopub.status.idle":"2022-08-02T13:28:21.047305Z","shell.execute_reply.started":"2022-08-02T13:28:21.037945Z","shell.execute_reply":"2022-08-02T13:28:21.046523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now, our encoded test dataset is balanced for prediction, lets do it !","metadata":{}},{"cell_type":"code","source":"# scaling test data\ntest_data_scale=scale.transform(test_data_new)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:28:21.049136Z","iopub.execute_input":"2022-08-02T13:28:21.049805Z","iopub.status.idle":"2022-08-02T13:28:21.065360Z","shell.execute_reply.started":"2022-08-02T13:28:21.049759Z","shell.execute_reply":"2022-08-02T13:28:21.064447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# feature extraction for test data\ntest_data_transformed=sklearn_pca.transform(test_data_scale)\ntest_data_transformed.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:28:21.066897Z","iopub.execute_input":"2022-08-02T13:28:21.068274Z","iopub.status.idle":"2022-08-02T13:28:21.083485Z","shell.execute_reply.started":"2022-08-02T13:28:21.068227Z","shell.execute_reply":"2022-08-02T13:28:21.082257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_prediction=GBR.predict(test_data_transformed)\nfinal_prediction","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:28:21.084987Z","iopub.execute_input":"2022-08-02T13:28:21.086218Z","iopub.status.idle":"2022-08-02T13:28:21.109266Z","shell.execute_reply.started":"2022-08-02T13:28:21.086169Z","shell.execute_reply":"2022-08-02T13:28:21.107627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_chk=pd.read_csv(r'../input/house-prices-advanced-regression-techniques/test.csv')\nid_chk.Id.values","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:28:21.116896Z","iopub.execute_input":"2022-08-02T13:28:21.120926Z","iopub.status.idle":"2022-08-02T13:28:21.179793Z","shell.execute_reply.started":"2022-08-02T13:28:21.120848Z","shell.execute_reply":"2022-08-02T13:28:21.178177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'Id':id_chk.Id.values,'SalePrice':final_prediction})\nsubmission.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:28:21.195032Z","iopub.execute_input":"2022-08-02T13:28:21.198797Z","iopub.status.idle":"2022-08-02T13:28:21.210909Z","shell.execute_reply.started":"2022-08-02T13:28:21.198749Z","shell.execute_reply":"2022-08-02T13:28:21.209754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:28:21.212413Z","iopub.execute_input":"2022-08-02T13:28:21.213416Z","iopub.status.idle":"2022-08-02T13:28:21.230245Z","shell.execute_reply.started":"2022-08-02T13:28:21.213370Z","shell.execute_reply":"2022-08-02T13:28:21.229106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}