{"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\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport warnings\nwarnings.filterwarnings('ignore')\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-08-13T06:04:59.838832Z","iopub.execute_input":"2022-08-13T06:04:59.839483Z","iopub.status.idle":"2022-08-13T06:05:01.007842Z","shell.execute_reply.started":"2022-08-13T06:04:59.839378Z","shell.execute_reply":"2022-08-13T06:05:01.006886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = pd.read_csv('../input/house-prices-advanced-regression-techniques/train.csv')\ntest_dataset = pd.read_csv('../input/house-prices-advanced-regression-techniques/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-13T06:05:01.010369Z","iopub.execute_input":"2022-08-13T06:05:01.011594Z","iopub.status.idle":"2022-08-13T06:05:01.102708Z","shell.execute_reply.started":"2022-08-13T06:05:01.011544Z","shell.execute_reply":"2022-08-13T06:05:01.101376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T06:05:01.104988Z","iopub.execute_input":"2022-08-13T06:05:01.105406Z","iopub.status.idle":"2022-08-13T06:05:01.145927Z","shell.execute_reply.started":"2022-08-13T06:05:01.105358Z","shell.execute_reply":"2022-08-13T06:05:01.144749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def check_df(dataframe,head=5):\n    print(\"#### Shape #### \")\n    print(dataframe.shape)\n    print(\"### Types ###\")\n    print(dataframe.dtypes)\n    print(\"### Head ###\")\n    print(dataframe.head(head))\n    print(\"### Tail ###\")\n    print(dataframe.tail(head))\n    print(\"### NA ###\")\n    print(dataframe.isnull().sum())\n    print(\"### Quantiles ###\")\n    print(dataframe.describe([0, 0.05,0.5,0.95,0.99,1]).T)\n\ncheck_df(train_dataset)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T06:05:01.147478Z","iopub.execute_input":"2022-08-13T06:05:01.147856Z","iopub.status.idle":"2022-08-13T06:05:01.287467Z","shell.execute_reply.started":"2022-08-13T06:05:01.147822Z","shell.execute_reply":"2022-08-13T06:05:01.286519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline\ntrain_dataset.hist(bins=50, figsize=(16,16))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T06:05:01.290098Z","iopub.execute_input":"2022-08-13T06:05:01.291022Z","iopub.status.idle":"2022-08-13T06:05:08.325950Z","shell.execute_reply.started":"2022-08-13T06:05:01.290986Z","shell.execute_reply":"2022-08-13T06:05:08.325094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def grap_col_names(dataframe,cat_th=10 , car_th = 20):\n    \"\"\"\n    Veri setindeki kategorik, numerik ve kategorik fakat kardinal değişkenlerin isimlerini verir.\n\n    Parameters\n    ----------\n    dataframe : dataframe\n        değişken isimleri alınmak istenen dataframe'dir.\n\n    cat_th : int,float\n        numerik fakat kategorik olan değişkenler için sınıf eşik değeri\n\n    car_th : int,float\n        kategorik fakat kardinal değişkenler için sınıf eşik değeri\n\n    Returns\n    -------\n        cat_cols : list\n            Kategorik değişken listesi\n        num_cols : list\n            Numerik değişken listesi\n        cat_but_car : list\n            Kategorik görünümlü kardinal değişken listesi\n\n    Notes\n    -------\n        cat_cols + num_cols + cat_but_car = toplam değişken sayısı\n        num_but_car değişkeni cat_cols'un içerisindedir.\n        Return olan 3 liste toplamı, toplam değişken sayısına eşittir.\n    \"\"\"\n\n    cat_cols = [col for col in dataframe.columns if str(dataframe[col].dtypes) in [\"category\", \"object\", \"bool\"]]\n    num_but_cat = [col for col in dataframe.columns if (dataframe[col].nunique() < 10) and dataframe[col].dtypes in [\"int64\", \"float64\"]]\n    cat_but_car = [col for col in dataframe.columns if\n                 (dataframe[col].nunique() > 20) and str(dataframe[col].dtypes) in [\"category\", \"object\"]]\n    cat_cols = cat_cols + num_but_cat\n    cat_cols = [col for col in cat_cols if col not in cat_but_car]\n\n    num_cols = [col for col in dataframe.columns if dataframe[col].dtypes in [\"int64\", \"float64\"]]\n    num_cols = [col for col in num_cols if col not in cat_cols]\n\n    print(f\"Observations: {dataframe.shape[0]}\")\n    print(f\"Variables: {dataframe.shape[1]}\")\n    print(f\"cat_cols: {len(cat_cols)}\")\n    print(f\"num_cols: {len(num_cols)}\")\n    print(f\"cat_but_car : {len(cat_but_car)}\")\n    print(f\"num_but_cat : {len(num_but_cat)}\")\n\n    return cat_cols,num_cols,cat_but_car\n\n\n\ncat_cols,num_cols,cat_but_car = grap_col_names(train_dataset)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T06:05:08.327581Z","iopub.execute_input":"2022-08-13T06:05:08.328221Z","iopub.status.idle":"2022-08-13T06:05:08.370833Z","shell.execute_reply.started":"2022-08-13T06:05:08.328186Z","shell.execute_reply":"2022-08-13T06:05:08.369619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def cat_summary(dataframe, col_name, plot=False):\n    print(pd.DataFrame({col_name: dataframe[col_name].value_counts(),\n                        \"Ratio\": 100 * dataframe[col_name].value_counts() / len(dataframe)}))\n    print(\"##########################################\")\n\n    if plot:\n        sns.countplot(x=dataframe[col_name], data=dataframe)\n        plt.show(block=True)\n\nfor col in cat_cols:\n    cat_summary(train_dataset, col, plot=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T06:05:08.372458Z","iopub.execute_input":"2022-08-13T06:05:08.373156Z","iopub.status.idle":"2022-08-13T06:05:17.695030Z","shell.execute_reply.started":"2022-08-13T06:05:08.373107Z","shell.execute_reply":"2022-08-13T06:05:17.694112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def num_summary(dataframe, numerical_col, plot=False):\n    quantiles = [0.05, 0.10, 0.20, 0.30, 0.40, 0.50, 0.60, 0.70, 0.80, 0.90, 0.95, 0.99]\n    print(dataframe[numerical_col].describe(quantiles).T)\n\n    if plot:\n        dataframe[numerical_col].hist()\n        plt.xlabel(numerical_col)\n        plt.title(numerical_col)\n        plt.show(block=True)\n\n\nfor col in num_cols:\n    num_summary(train_dataset, col, plot=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T06:05:17.696522Z","iopub.execute_input":"2022-08-13T06:05:17.697371Z","iopub.status.idle":"2022-08-13T06:05:23.139118Z","shell.execute_reply.started":"2022-08-13T06:05:17.697310Z","shell.execute_reply":"2022-08-13T06:05:23.137917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(10, 6))\nax.grid()\nax.scatter(train_dataset[\"GrLivArea\"], train_dataset[\"SalePrice\"], c=\"#3f72af\", zorder=3, alpha=0.9)\nax.axvline(4500, c=\"#112d4e\", ls=\"--\", zorder=2)\nax.set_xlabel(\"Ground living area (sq. ft)\", labelpad=10)\nax.set_ylabel(\"Sale price ($)\", labelpad=10)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T06:05:23.142475Z","iopub.execute_input":"2022-08-13T06:05:23.142859Z","iopub.status.idle":"2022-08-13T06:05:23.372957Z","shell.execute_reply.started":"2022-08-13T06:05:23.142826Z","shell.execute_reply":"2022-08-13T06:05:23.371770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxplot(train_dataset.GrLivArea)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T06:05:23.375557Z","iopub.execute_input":"2022-08-13T06:05:23.376076Z","iopub.status.idle":"2022-08-13T06:05:23.509081Z","shell.execute_reply.started":"2022-08-13T06:05:23.376004Z","shell.execute_reply":"2022-08-13T06:05:23.507743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numerical_df = train_dataset.select_dtypes(exclude=['object'])\nnumerical_df = numerical_df.drop([\"Id\"], axis=1)\nfor column in numerical_df:\n    plt.figure(figsize=(16, 4))\n    sns.set_theme(style=\"whitegrid\")\n    sns.boxplot(numerical_df[column])","metadata":{"execution":{"iopub.status.busy":"2022-08-13T06:05:23.510972Z","iopub.execute_input":"2022-08-13T06:05:23.512375Z","iopub.status.idle":"2022-08-13T06:05:30.418008Z","shell.execute_reply.started":"2022-08-13T06:05:23.512323Z","shell.execute_reply":"2022-08-13T06:05:30.417089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, ax = plt.subplots(figsize=(16, 16))\nsns.distplot(train_dataset.get(\"SalePrice\"), kde=False)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T06:05:30.419754Z","iopub.execute_input":"2022-08-13T06:05:30.420804Z","iopub.status.idle":"2022-08-13T06:05:30.767989Z","shell.execute_reply.started":"2022-08-13T06:05:30.420739Z","shell.execute_reply":"2022-08-13T06:05:30.766660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corrmat = train_dataset.corr()\nf, ax = plt.subplots(figsize=(16, 16))\nsns.heatmap(corrmat, vmax=.8, square=True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T06:05:30.769605Z","iopub.execute_input":"2022-08-13T06:05:30.770088Z","iopub.status.idle":"2022-08-13T06:05:32.005756Z","shell.execute_reply.started":"2022-08-13T06:05:30.770021Z","shell.execute_reply":"2022-08-13T06:05:32.004618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16,16))\ncolumns = corrmat.nlargest(10, 'SalePrice')['SalePrice'].index\ncorrelation_matrix = np.corrcoef(train_dataset[columns].values.T)\nsns.set(font_scale=1.25)\nheat_map = sns.heatmap(correlation_matrix, cbar=True, annot=True, square=True, fmt='.2f', annot_kws={'size': 10}, yticklabels=columns.values, xticklabels=columns.values)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T06:05:32.010178Z","iopub.execute_input":"2022-08-13T06:05:32.010581Z","iopub.status.idle":"2022-08-13T06:05:32.771136Z","shell.execute_reply.started":"2022-08-13T06:05:32.010544Z","shell.execute_reply":"2022-08-13T06:05:32.770129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = train_dataset[train_dataset.GrLivArea < 4500]\n","metadata":{"execution":{"iopub.status.busy":"2022-08-13T06:05:32.772398Z","iopub.execute_input":"2022-08-13T06:05:32.773543Z","iopub.status.idle":"2022-08-13T06:05:32.781909Z","shell.execute_reply.started":"2022-08-13T06:05:32.773491Z","shell.execute_reply":"2022-08-13T06:05:32.780499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total = test_dataset.isna().sum().sort_values(ascending=False)\n# concatenate this data into dataframe\nmissing_data = pd.concat([total], axis=1, keys=[\"Total\"])\nmissing_data.head(45)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T06:05:32.783785Z","iopub.execute_input":"2022-08-13T06:05:32.784317Z","iopub.status.idle":"2022-08-13T06:05:32.812374Z","shell.execute_reply.started":"2022-08-13T06:05:32.784266Z","shell.execute_reply":"2022-08-13T06:05:32.811275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total = total[total > 0]\nfig, ax = plt.subplots(figsize=(10, 6))\nax.grid()\nax.bar(total.index, total.values, zorder=2, color=\"#3f72af\")\nax.set_ylabel(\"No. of missing values\", labelpad=10)\nax.set_xlim(-0.6, len(total) - 0.4)\nax.xaxis.set_tick_params(rotation=90)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T06:05:32.813645Z","iopub.execute_input":"2022-08-13T06:05:32.814655Z","iopub.status.idle":"2022-08-13T06:05:33.230211Z","shell.execute_reply.started":"2022-08-13T06:05:32.814618Z","shell.execute_reply":"2022-08-13T06:05:33.229103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = train_dataset.drop(missing_data[missing_data.Total > 0 ].index, axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-13T06:05:33.231622Z","iopub.execute_input":"2022-08-13T06:05:33.231980Z","iopub.status.idle":"2022-08-13T06:05:33.240322Z","shell.execute_reply.started":"2022-08-13T06:05:33.231949Z","shell.execute_reply":"2022-08-13T06:05:33.239107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = test_dataset.dropna(axis=1)\ntest_dataset = test_dataset.drop([\"Electrical\"], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T06:05:33.241620Z","iopub.execute_input":"2022-08-13T06:05:33.242075Z","iopub.status.idle":"2022-08-13T06:05:33.259186Z","shell.execute_reply.started":"2022-08-13T06:05:33.242024Z","shell.execute_reply":"2022-08-13T06:05:33.257653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_dataset = pd.concat([train_dataset, test_dataset])\nfull_dataset = pd.get_dummies(full_dataset)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T06:05:33.260596Z","iopub.execute_input":"2022-08-13T06:05:33.261226Z","iopub.status.idle":"2022-08-13T06:05:33.301707Z","shell.execute_reply.started":"2022-08-13T06:05:33.261184Z","shell.execute_reply":"2022-08-13T06:05:33.300350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = full_dataset.iloc[train_dataset.index]\nx_test = full_dataset.iloc[test_dataset.index]","metadata":{"execution":{"iopub.status.busy":"2022-08-13T06:05:33.303322Z","iopub.execute_input":"2022-08-13T06:05:33.303702Z","iopub.status.idle":"2022-08-13T06:05:33.314677Z","shell.execute_reply.started":"2022-08-13T06:05:33.303666Z","shell.execute_reply":"2022-08-13T06:05:33.313336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = x.drop([\"SalePrice\"], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T06:05:33.316423Z","iopub.execute_input":"2022-08-13T06:05:33.316921Z","iopub.status.idle":"2022-08-13T06:05:33.324723Z","shell.execute_reply.started":"2022-08-13T06:05:33.316869Z","shell.execute_reply":"2022-08-13T06:05:33.323614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train_dataset.SalePrice","metadata":{"execution":{"iopub.status.busy":"2022-08-13T06:05:33.326102Z","iopub.execute_input":"2022-08-13T06:05:33.326981Z","iopub.status.idle":"2022-08-13T06:05:33.336238Z","shell.execute_reply.started":"2022-08-13T06:05:33.326936Z","shell.execute_reply":"2022-08-13T06:05:33.335101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train, x_val, y_train, y_val = train_test_split(x, y, train_size=0.8, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T06:05:33.338064Z","iopub.execute_input":"2022-08-13T06:05:33.338531Z","iopub.status.idle":"2022-08-13T06:05:33.479190Z","shell.execute_reply.started":"2022-08-13T06:05:33.338483Z","shell.execute_reply":"2022-08-13T06:05:33.477912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x.isna().sum().sort_values(ascending=False)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-13T06:05:33.480917Z","iopub.execute_input":"2022-08-13T06:05:33.481556Z","iopub.status.idle":"2022-08-13T06:05:33.494622Z","shell.execute_reply.started":"2022-08-13T06:05:33.481506Z","shell.execute_reply":"2022-08-13T06:05:33.493634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LinearRegression\nfrom scipy.stats import zscore\nregressor = LinearRegression()\nregressor.fit(x_train, y_train)\nregressor.score(x_val, y_val)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T06:05:33.495967Z","iopub.execute_input":"2022-08-13T06:05:33.496625Z","iopub.status.idle":"2022-08-13T06:05:33.630869Z","shell.execute_reply.started":"2022-08-13T06:05:33.496583Z","shell.execute_reply":"2022-08-13T06:05:33.629404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = x_test.drop([\"SalePrice\"], axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-13T06:05:33.633965Z","iopub.execute_input":"2022-08-13T06:05:33.636002Z","iopub.status.idle":"2022-08-13T06:05:33.649590Z","shell.execute_reply.started":"2022-08-13T06:05:33.635946Z","shell.execute_reply":"2022-08-13T06:05:33.647654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_preds = regressor.predict(x_test)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-13T06:05:33.652434Z","iopub.execute_input":"2022-08-13T06:05:33.654704Z","iopub.status.idle":"2022-08-13T06:05:33.684477Z","shell.execute_reply.started":"2022-08-13T06:05:33.654646Z","shell.execute_reply":"2022-08-13T06:05:33.682918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import xgboost\nxgb_reg = xgboost.XGBRegressor()\nxgb_reg.fit(x_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T06:05:33.690965Z","iopub.execute_input":"2022-08-13T06:05:33.695170Z","iopub.status.idle":"2022-08-13T06:05:34.653637Z","shell.execute_reply.started":"2022-08-13T06:05:33.695104Z","shell.execute_reply":"2022-08-13T06:05:34.652455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_preds = xgb_reg.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T06:05:34.655212Z","iopub.execute_input":"2022-08-13T06:05:34.655602Z","iopub.status.idle":"2022-08-13T06:05:34.674093Z","shell.execute_reply.started":"2022-08-13T06:05:34.655569Z","shell.execute_reply":"2022-08-13T06:05:34.673071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'Id': test_dataset.Id,\n                      'SalePrice': y_preds})\noutput.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T06:05:34.677780Z","iopub.execute_input":"2022-08-13T06:05:34.678180Z","iopub.status.idle":"2022-08-13T06:05:34.692276Z","shell.execute_reply.started":"2022-08-13T06:05:34.678148Z","shell.execute_reply":"2022-08-13T06:05:34.690849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}