{"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\n\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split, cross_val_score\nfrom sklearn.metrics import mean_squared_error\nfrom xgboost import XGBRegressor","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-05T12:07:30.830297Z","iopub.execute_input":"2022-07-05T12:07:30.831469Z","iopub.status.idle":"2022-07-05T12:07:32.302556Z","shell.execute_reply.started":"2022-07-05T12:07:30.831375Z","shell.execute_reply":"2022-07-05T12:07:32.301198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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\")\n\ntest_ids = test[\"Id\"]\ndata.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T13:34:04.236345Z","iopub.execute_input":"2022-07-05T13:34:04.236759Z","iopub.status.idle":"2022-07-05T13:34:04.297408Z","shell.execute_reply.started":"2022-07-05T13:34:04.236725Z","shell.execute_reply":"2022-07-05T13:34:04.295713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def clean_num(df):\n    df = df.drop([\"Id\"], axis=1)\n    df = df.select_dtypes(exclude=\"object\") #pick numerical data\n    \n    cols = list(df.columns)\n    for col in cols:\n        df[col].fillna(df[col].mean(), inplace=True) #fill missing values with mean\n            \n    #creating more features        \n    df[\"TotalPorch\"] = sum([df.WoodDeckSF,df.OpenPorchSF,df.EnclosedPorch,df[\"3SsnPorch\"],df.ScreenPorch])\n    df[\"Spaciousness\"] = (df[\"1stFlrSF\"] +df[\"2ndFlrSF\"]) / df[\"TotRmsAbvGrd\"]\n    df[\"FrontageRatio\"] = df.LotFrontage/df.LotArea\n    df[\"HouseOld\"] = df.YrSold - df.YearRemodAdd\n    df[\"GarageOld\"] = df.YrSold - df.GarageYrBlt\n    df[\"BsmtRatio\"] = df.TotalBsmtSF/df.LotArea\n    \n    return df\n\ndef clean_obj(df):\n    df = df.drop([\"Id\"], axis=1)\n    df = df.select_dtypes(include=\"object\") #pick non-numerical data\n    \n    cols = list(df.columns)\n    for col in cols:\n        df[col].fillna(df[col].value_counts().idxmax(), inplace=True) #fill missing values with the most common value\n\n    return df","metadata":{"execution":{"iopub.status.busy":"2022-07-05T13:16:07.321176Z","iopub.execute_input":"2022-07-05T13:16:07.321543Z","iopub.status.idle":"2022-07-05T13:16:07.331415Z","shell.execute_reply.started":"2022-07-05T13:16:07.321513Z","shell.execute_reply":"2022-07-05T13:16:07.329961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_num = clean_num(data)\ndata_obj = clean_obj(data)\n\ntest_num = clean_num(test)\ntest_obj = clean_obj(test)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T13:16:08.725730Z","iopub.execute_input":"2022-07-05T13:16:08.726850Z","iopub.status.idle":"2022-07-05T13:16:08.849749Z","shell.execute_reply.started":"2022-07-05T13:16:08.726804Z","shell.execute_reply":"2022-07-05T13:16:08.848539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"le = LabelEncoder()\ncols = list(data_obj.columns)\nfor col in cols:\n    data_obj[col] = le.fit_transform(data_obj[col])\n    test_obj[col] = le.transform(test_obj[col])","metadata":{"execution":{"iopub.status.busy":"2022-07-05T13:16:09.465990Z","iopub.execute_input":"2022-07-05T13:16:09.466523Z","iopub.status.idle":"2022-07-05T13:16:09.559940Z","shell.execute_reply.started":"2022-07-05T13:16:09.466475Z","shell.execute_reply":"2022-07-05T13:16:09.558823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.concat([data_num,data_obj], axis=1)\ntest = pd.concat([test_num,test_obj], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T13:16:10.255866Z","iopub.execute_input":"2022-07-05T13:16:10.256282Z","iopub.status.idle":"2022-07-05T13:16:10.273407Z","shell.execute_reply.started":"2022-07-05T13:16:10.256249Z","shell.execute_reply":"2022-07-05T13:16:10.271793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = data.drop([\"SalePrice\"], axis=1)\ny = data[\"SalePrice\"]\n\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.1)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T13:16:10.832559Z","iopub.execute_input":"2022-07-05T13:16:10.832953Z","iopub.status.idle":"2022-07-05T13:16:10.844519Z","shell.execute_reply.started":"2022-07-05T13:16:10.832922Z","shell.execute_reply":"2022-07-05T13:16:10.843491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = XGBRegressor(n_estimators=1600, max_depth=16, learning_rate=0.01, subsample=0.5,\n                     colsample_bytree=0.75, missing=-999, random_state=2020, n_jobs=10)\n\nmodel.fit(X_train, y_train, early_stopping_rounds=20, eval_set=[(X_val, y_val)], verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T13:16:11.387030Z","iopub.execute_input":"2022-07-05T13:16:11.387405Z","iopub.status.idle":"2022-07-05T13:16:29.583443Z","shell.execute_reply.started":"2022-07-05T13:16:11.387373Z","shell.execute_reply":"2022-07-05T13:16:29.582152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def score_dataset(X, y, m):\n    for colname in X.select_dtypes([\"category\", \"object\"]):\n        X[colname], _ = X[colname].factorize()\n    score = cross_val_score(m, X, y, cv=5, scoring=\"neg_mean_squared_log_error\",)\n    score = -1 * score.mean()\n    score = np.sqrt(score)\n    return score","metadata":{"execution":{"iopub.status.busy":"2022-07-05T13:17:14.592035Z","iopub.execute_input":"2022-07-05T13:17:14.592863Z","iopub.status.idle":"2022-07-05T13:17:14.599266Z","shell.execute_reply.started":"2022-07-05T13:17:14.592815Z","shell.execute_reply":"2022-07-05T13:17:14.597992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(X_val)\nmse = mean_squared_error(y_val,predictions)\nscore = score_dataset(X_val, y_val, model)\n\nprint(f\"Root Mean Squared Error: {np.sqrt(mse)}\")\nprint(f\"Score : {score}\")","metadata":{"execution":{"iopub.status.busy":"2022-07-05T13:17:16.511481Z","iopub.execute_input":"2022-07-05T13:17:16.511888Z","iopub.status.idle":"2022-07-05T13:18:21.784825Z","shell.execute_reply.started":"2022-07-05T13:17:16.511856Z","shell.execute_reply":"2022-07-05T13:18:21.783509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = model.predict(test)\ndf = pd.DataFrame({\"Id\": test_ids.values,\n                   \"SalePrice\": submission})\ndf.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:33:15.704706Z","iopub.execute_input":"2022-07-05T12:33:15.705458Z","iopub.status.idle":"2022-07-05T12:33:15.769583Z","shell.execute_reply.started":"2022-07-05T12:33:15.705414Z","shell.execute_reply":"2022-07-05T12:33:15.768590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}