{"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":"# Price Sale \nLet's go work on this dataset","metadata":{}},{"cell_type":"code","source":"#import\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport seaborn as sns\nfrom scipy import stats\nfrom scipy.stats import norm\nfrom sklearn.model_selection import train_test_split\n\n# Models\nfrom sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor, AdaBoostRegressor, BaggingRegressor\nfrom sklearn.kernel_ridge import KernelRidge\nfrom sklearn.linear_model import Ridge, RidgeCV\nfrom sklearn.linear_model import ElasticNet, ElasticNetCV\nfrom sklearn.svm import SVR\nfrom mlxtend.regressor import StackingCVRegressor\nimport lightgbm as lgb\nfrom lightgbm import LGBMRegressor\nfrom xgboost import XGBRegressor\nfrom sklearn.model_selection import KFold, cross_val_score\nfrom sklearn.metrics import mean_squared_error\n\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:15:30.987013Z","iopub.execute_input":"2022-08-06T16:15:30.987449Z","iopub.status.idle":"2022-08-06T16:15:30.997736Z","shell.execute_reply.started":"2022-08-06T16:15:30.987412Z","shell.execute_reply":"2022-08-06T16:15:30.996244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Step1 : load dataset","metadata":{}},{"cell_type":"code","source":"df_train = pd.read_csv('../input/house-prices-advanced-regression-techniques/train.csv')\ndf_test = pd.read_csv('../input/house-prices-advanced-regression-techniques/test.csv')\ncombine = [df_train,df_test]","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:15:30.999932Z","iopub.execute_input":"2022-08-06T16:15:31.001383Z","iopub.status.idle":"2022-08-06T16:15:31.059989Z","shell.execute_reply.started":"2022-08-06T16:15:31.001331Z","shell.execute_reply":"2022-08-06T16:15:31.059151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_name = 'SalePrice' \n\ndf = df_train.copy()\nnames_select=[]\nfor name in df.columns:\n    print(f'\\n{name}')\n    unique = df[name].nunique()\n    missing =df[name].isnull().sum()\n    leng = len(df[name])\n    rate = missing / leng\n    try:\n        corS = df[name].corr(df[target_name],method='spearman')\n    except Exception as err:\n        corS=0\n        print(f\"{name} Problem with this variable\")\n    if (abs(corS)>0.5):\n        names_select.append(name)\n        print(f'\\n{name} information for this feature')\n        try : \n            data_exp = df[[name,target_name]]\n            \n            print(\"Spearman:\",corS)\n            corK = df[name].corr(df[target_name],method='kendall')\n            print(\"Kendall:\",corK)\n            if(df[name].dtypes != 'object'):\n                corP = df[name].corr(df[target_name],method='pearson')\n                print(\"pearson:\",corP)\n        except Exception as err:\n            print(\"probably test data\")\n\n        print(\"Unique: \",unique)\n        print(\"Missing: \",missing)\n        print(\"Missing rate: \",rate)\n        \n        \n       \n","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:15:31.061440Z","iopub.execute_input":"2022-08-06T16:15:31.062322Z","iopub.status.idle":"2022-08-06T16:15:31.313543Z","shell.execute_reply.started":"2022-08-06T16:15:31.062286Z","shell.execute_reply":"2022-08-06T16:15:31.312135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Deal with Nan values\n\nthis site can help : https://notebook.community/chapagain/kaggle-competitions-solution/House%20Prices:%20Advanced%20Regression%20Techniques/House_Price\n","metadata":{}},{"cell_type":"markdown","source":"#### Remove feature with to much missing values","metadata":{}},{"cell_type":"code","source":"del_feature = ['MasVnrType','Alley','FireplaceQu','MiscFeature']","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:15:31.315581Z","iopub.execute_input":"2022-08-06T16:15:31.316156Z","iopub.status.idle":"2022-08-06T16:15:31.321861Z","shell.execute_reply.started":"2022-08-06T16:15:31.316109Z","shell.execute_reply":"2022-08-06T16:15:31.320487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for dataset in combine:\n    dataset.drop(del_feature,axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:15:31.324509Z","iopub.execute_input":"2022-08-06T16:15:31.324861Z","iopub.status.idle":"2022-08-06T16:15:31.337606Z","shell.execute_reply.started":"2022-08-06T16:15:31.324829Z","shell.execute_reply":"2022-08-06T16:15:31.336629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### See what happended with Bsmt Var","metadata":{}},{"cell_type":"markdown","source":"I am French and I didn't know what the abbreviation meant, so i keep it here\n\nBsmtQual\n\nEx - Excellent (100+ inches) -\nGd - Good (90-99 inches)\nTA - Typical (80-89 inches)\nFa - Fair (70-79 inches)\nPo - Poor (<70 inches\nNB - No Basement\n\nBsmtExposure\n\nGd - Good Exposure\nAv - Average Exposure (split levels or foyers typically score average or above) -\nMn - Mimimum Exposure\nNo - No Exposure\nNB - No Basement","metadata":{}},{"cell_type":"code","source":"#researche section\nBsmt_feature = df_train.columns[df_train.columns.str.contains('Bsmt')]\ndf_train.loc[df_train['BsmtQual'].isna()==True][Bsmt_feature]\n#df_train[Bsmt_feature]=df_train[Bsmt_feature].fillna('NB')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:15:31.339005Z","iopub.execute_input":"2022-08-06T16:15:31.339601Z","iopub.status.idle":"2022-08-06T16:15:31.366418Z","shell.execute_reply.started":"2022-08-06T16:15:31.339567Z","shell.execute_reply":"2022-08-06T16:15:31.365475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for dataset in combine:\n    dataset[Bsmt_feature]=dataset[Bsmt_feature].fillna('NB')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:15:31.367622Z","iopub.execute_input":"2022-08-06T16:15:31.368153Z","iopub.status.idle":"2022-08-06T16:15:31.384576Z","shell.execute_reply.started":"2022-08-06T16:15:31.368120Z","shell.execute_reply":"2022-08-06T16:15:31.383131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['BsmtFinType1'].unique()\n","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:15:31.386271Z","iopub.execute_input":"2022-08-06T16:15:31.387291Z","iopub.status.idle":"2022-08-06T16:15:31.394169Z","shell.execute_reply.started":"2022-08-06T16:15:31.387253Z","shell.execute_reply":"2022-08-06T16:15:31.393026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### same thing with garage","metadata":{}},{"cell_type":"code","source":"\n#researche section\nGarage_feature = df_train.columns[df_train.columns.str.contains('Garage')]\ndf_train.loc[df_train['GarageType'].isna()==True][Bsmt_feature]","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:15:31.395941Z","iopub.execute_input":"2022-08-06T16:15:31.396330Z","iopub.status.idle":"2022-08-06T16:15:31.426411Z","shell.execute_reply.started":"2022-08-06T16:15:31.396296Z","shell.execute_reply":"2022-08-06T16:15:31.425023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['GarageType'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:15:31.428003Z","iopub.execute_input":"2022-08-06T16:15:31.428403Z","iopub.status.idle":"2022-08-06T16:15:31.436564Z","shell.execute_reply.started":"2022-08-06T16:15:31.428369Z","shell.execute_reply":"2022-08-06T16:15:31.435356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for dataset in combine:\n    dataset[Garage_feature]=dataset[Garage_feature].fillna('NoGarage')\n    dataset['GarageYrBlt'].replace('NoGarage', 0,inplace=True)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:15:31.441597Z","iopub.execute_input":"2022-08-06T16:15:31.442709Z","iopub.status.idle":"2022-08-06T16:15:31.460562Z","shell.execute_reply.started":"2022-08-06T16:15:31.442657Z","shell.execute_reply":"2022-08-06T16:15:31.459295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Fence\n\nfor dataset in combine:\n    dataset['Fence']=dataset['Fence'].fillna('NoFence')\n\n","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:15:31.462320Z","iopub.execute_input":"2022-08-06T16:15:31.462976Z","iopub.status.idle":"2022-08-06T16:15:31.473125Z","shell.execute_reply.started":"2022-08-06T16:15:31.462938Z","shell.execute_reply":"2022-08-06T16:15:31.471882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#pool\nfor dataset in combine:\n    dataset['PoolQC']=dataset['PoolQC'].fillna('NoPool')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:15:31.475119Z","iopub.execute_input":"2022-08-06T16:15:31.475483Z","iopub.status.idle":"2022-08-06T16:15:31.492413Z","shell.execute_reply.started":"2022-08-06T16:15:31.475447Z","shell.execute_reply":"2022-08-06T16:15:31.491148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#MasVnrArea\nfor dataset in combine:\n    dataset['MasVnrArea']=dataset['MasVnrArea'].fillna(0)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:15:31.494141Z","iopub.execute_input":"2022-08-06T16:15:31.494489Z","iopub.status.idle":"2022-08-06T16:15:31.505614Z","shell.execute_reply.started":"2022-08-06T16:15:31.494456Z","shell.execute_reply":"2022-08-06T16:15:31.504661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### LotFrontage\nLotFrontage: Linear feet of street connected to property\n\n16.67% values are missing for LotFrontage. We can assume that the distance of the street connected to the property (LotFrontage) will be same as that of that particular property's neighbor property (Neighborhood).\n\nWe can fill the missing value by the median LotFrontage of all the Neighborhood.","metadata":{}},{"cell_type":"code","source":"\n# this graph is really cool\nsns.barplot(data=df_train,x='Neighborhood',y='LotFrontage', estimator=np.median)\nplt.xticks(rotation=90)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:15:31.506756Z","iopub.execute_input":"2022-08-06T16:15:31.507343Z","iopub.status.idle":"2022-08-06T16:15:33.131974Z","shell.execute_reply.started":"2022-08-06T16:15:31.507310Z","shell.execute_reply":"2022-08-06T16:15:33.131142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfor dataset in combine:\n    dataset[\"LotFrontage\"] = dataset.groupby(\"Neighborhood\")[\"LotFrontage\"].transform(\n        lambda x: x.fillna(x.median()))","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:15:33.133204Z","iopub.execute_input":"2022-08-06T16:15:33.134303Z","iopub.status.idle":"2022-08-06T16:15:33.167522Z","shell.execute_reply.started":"2022-08-06T16:15:33.134263Z","shell.execute_reply":"2022-08-06T16:15:33.166599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"code","source":"target_name = 'SalePrice' \n\ndf = df_train.copy()\nnames_select=[]\nfor name in df.columns:\n    try:\n        corS = df[name].corr(df[target_name],method='spearman')\n    except Exception as err:\n        corS=0\n        print(f\"{name} Problem with this variable\")\n    if (abs(corS)>0.04):\n        names_select.append(name)\n       \n        \n       \n","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:15:33.169257Z","iopub.execute_input":"2022-08-06T16:15:33.169971Z","iopub.status.idle":"2022-08-06T16:15:33.470404Z","shell.execute_reply.started":"2022-08-06T16:15:33.169926Z","shell.execute_reply":"2022-08-06T16:15:33.469354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Train data missed values:\\n\")\nbeat=df_train.copy()\nbeat=beat[names_select]\nprint(beat.isnull().sum())","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:15:33.471723Z","iopub.execute_input":"2022-08-06T16:15:33.472065Z","iopub.status.idle":"2022-08-06T16:15:33.487783Z","shell.execute_reply.started":"2022-08-06T16:15:33.472015Z","shell.execute_reply":"2022-08-06T16:15:33.486478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"objList = beat.select_dtypes(include=['object'])\n#Label Encoding for object to numeric conversion\nfrom sklearn.preprocessing import LabelEncoder\nle = LabelEncoder()\n\nfor feat in objList:\n    beat[feat] = le.fit_transform(beat[feat].astype(str))\n\nprint (beat.info())","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:15:33.489735Z","iopub.execute_input":"2022-08-06T16:15:33.490252Z","iopub.status.idle":"2022-08-06T16:15:33.554503Z","shell.execute_reply.started":"2022-08-06T16:15:33.490167Z","shell.execute_reply":"2022-08-06T16:15:33.553256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import GradientBoostingRegressor\nfrom sklearn.metrics import mean_squared_error\ny=beat['SalePrice']\n\nX = beat.drop(['SalePrice'],axis=1)\ny\nGBR= GradientBoostingRegressor()\n\n\nmodel_beat = GBR.fit(X,y)\ny_pred = model_beat.predict(X)\nnp.sqrt(mean_squared_error(y,y_pred))","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:15:33.555919Z","iopub.execute_input":"2022-08-06T16:15:33.556475Z","iopub.status.idle":"2022-08-06T16:15:34.282455Z","shell.execute_reply.started":"2022-08-06T16:15:33.556441Z","shell.execute_reply":"2022-08-06T16:15:34.281285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features = X.columns\nX_test = df_test[features]","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:15:34.283971Z","iopub.execute_input":"2022-08-06T16:15:34.285084Z","iopub.status.idle":"2022-08-06T16:15:34.293217Z","shell.execute_reply.started":"2022-08-06T16:15:34.285025Z","shell.execute_reply":"2022-08-06T16:15:34.292078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"objList = X_test.select_dtypes(include=['object'])\n#Label Encoding for object to numeric conversion\nfrom sklearn.preprocessing import LabelEncoder\nle = LabelEncoder()\n\nfor feat in objList:\n    X_test[feat] = le.fit_transform(X_test[feat].astype(str))\n\nprint (X_test.info())","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:15:34.294872Z","iopub.execute_input":"2022-08-06T16:15:34.295315Z","iopub.status.idle":"2022-08-06T16:15:34.381341Z","shell.execute_reply.started":"2022-08-06T16:15:34.295282Z","shell.execute_reply":"2022-08-06T16:15:34.379949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model_beat.predict(X_test)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:15:34.382694Z","iopub.execute_input":"2022-08-06T16:15:34.383034Z","iopub.status.idle":"2022-08-06T16:15:34.396422Z","shell.execute_reply.started":"2022-08-06T16:15:34.383003Z","shell.execute_reply":"2022-08-06T16:15:34.395354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'Id': df_test.Id, 'SalePrice': y_pred})\noutput.to_csv('submission.csv', index=False)\nprint(\"Your submission was successfully saved!\")","metadata":{"execution":{"iopub.status.busy":"2022-08-06T16:15:34.397763Z","iopub.execute_input":"2022-08-06T16:15:34.398162Z","iopub.status.idle":"2022-08-06T16:15:34.410851Z","shell.execute_reply.started":"2022-08-06T16:15:34.398126Z","shell.execute_reply":"2022-08-06T16:15:34.409418Z"},"trusted":true},"execution_count":null,"outputs":[]}]}