{"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-07-11T06:41:24.020756Z","iopub.execute_input":"2022-07-11T06:41:24.021719Z","iopub.status.idle":"2022-07-11T06:41:24.030097Z","shell.execute_reply.started":"2022-07-11T06:41:24.021666Z","shell.execute_reply":"2022-07-11T06:41:24.028988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.model_selection import train_test_split\nfrom sklearn import metrics\nfrom sklearn.impute import SimpleImputer","metadata":{"execution":{"iopub.status.busy":"2022-07-11T06:41:24.110912Z","iopub.execute_input":"2022-07-11T06:41:24.111277Z","iopub.status.idle":"2022-07-11T06:41:24.117213Z","shell.execute_reply.started":"2022-07-11T06:41:24.111246Z","shell.execute_reply":"2022-07-11T06:41:24.116237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Read Data","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv(\"/kaggle/input/house-prices-advanced-regression-techniques/train.csv\")\ntest_data = pd.read_csv(\"/kaggle/input/house-prices-advanced-regression-techniques/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-11T06:41:24.197197Z","iopub.execute_input":"2022-07-11T06:41:24.197652Z","iopub.status.idle":"2022-07-11T06:41:24.254586Z","shell.execute_reply.started":"2022-07-11T06:41:24.197617Z","shell.execute_reply":"2022-07-11T06:41:24.253649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()\ntrain_columns = train_data.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-11T06:41:24.273067Z","iopub.execute_input":"2022-07-11T06:41:24.274086Z","iopub.status.idle":"2022-07-11T06:41:24.278709Z","shell.execute_reply.started":"2022-07-11T06:41:24.274032Z","shell.execute_reply":"2022-07-11T06:41:24.277916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T06:41:24.345795Z","iopub.execute_input":"2022-07-11T06:41:24.346200Z","iopub.status.idle":"2022-07-11T06:41:24.373667Z","shell.execute_reply.started":"2022-07-11T06:41:24.346169Z","shell.execute_reply":"2022-07-11T06:41:24.372658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.info()\nhouse_id = test_data.Id","metadata":{"execution":{"iopub.status.busy":"2022-07-11T06:41:24.418239Z","iopub.execute_input":"2022-07-11T06:41:24.418978Z","iopub.status.idle":"2022-07-11T06:41:24.446223Z","shell.execute_reply.started":"2022-07-11T06:41:24.418941Z","shell.execute_reply":"2022-07-11T06:41:24.445175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Transform non num columns to num\nclasses_dict = {}\n\nfor col in train_data.columns:\n    if train_data[col].dtype == 'O':\n        le = LabelEncoder()\n        le.fit(np.append(train_data[col], test_data[col]))\n        train_data[col] = le.transform(train_data[col])\n        test_data[col] = le.transform(test_data[col])\n        classes_dict[col] = list(le.classes_)\n    \nprint(classes_dict)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T06:41:24.528225Z","iopub.execute_input":"2022-07-11T06:41:24.529363Z","iopub.status.idle":"2022-07-11T06:41:24.628598Z","shell.execute_reply.started":"2022-07-11T06:41:24.529305Z","shell.execute_reply":"2022-07-11T06:41:24.627362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Analyse Data","metadata":{}},{"cell_type":"code","source":"print(\"The following Columns in the training data have Null values: \")\nprint('\\n')\ntrain_null_columns = train_data.columns[train_data.isna().any()].to_list()\nfor col in train_null_columns:\n    print(col)\nprint(f'That are {len(train_null_columns)} columns.')\n\nprint('\\n')\nprint('#####')\nprint('\\n')\n\nprint(\"The following Columns in the test data have Null values: \")\nprint('\\n')\ntest_null_columns = test_data.columns[test_data.isna().any()].to_list()\nfor col in test_null_columns:\n    print(col)\nprint(f'That are {len(test_null_columns)} columns.')","metadata":{"execution":{"iopub.status.busy":"2022-07-11T06:41:24.633081Z","iopub.execute_input":"2022-07-11T06:41:24.633460Z","iopub.status.idle":"2022-07-11T06:41:24.661567Z","shell.execute_reply.started":"2022-07-11T06:41:24.633426Z","shell.execute_reply":"2022-07-11T06:41:24.660299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"These are the columns in the test data that have null values but are complete in the training set: \\n\")\nnull_test_full_train = np.setdiff1d(test_null_columns, train_null_columns)\nfor col in null_test_full_train:\n    print(col)\n    \nprint('\\n')\nprint('#####')\nprint('\\n')\n\nprint(\"These are the columns in the training data that have null values but are complete in the test set: \\n\")\nnull_train_full_test = np.setdiff1d(train_null_columns, test_null_columns)\nfor col in null_train_full_test:\n    print(col)\n    \nprint('\\n')\nprint('#####')\nprint('\\n')\n\nprint(\"These are the columns that have null values in both sets: \\n\")\nindices = np.in1d(test_null_columns, train_null_columns)\nnull_cols_both_sets = np.array(test_null_columns)[indices]\nfor col in null_cols_both_sets:\n    print(col)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T06:41:24.700341Z","iopub.execute_input":"2022-07-11T06:41:24.700916Z","iopub.status.idle":"2022-07-11T06:41:24.711340Z","shell.execute_reply.started":"2022-07-11T06:41:24.700873Z","shell.execute_reply":"2022-07-11T06:41:24.710139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"percentage = 0.7\nprint(f\"These are the columns in the training set that have less than {percentage*100}% non null values: \\n\")\nfor col in train_null_columns:\n    length_non_null = train_data[col].notna().sum()\n    if length_non_null <= 0.7*len(train_data):\n        print(col)\n\n        \nprint('\\n')\nprint('#####')\nprint('\\n')\n\n\nprint(f\"These are the columns in the test set that have less than {percentage*100}% non null values: \\n\")\nfor col in test_null_columns:\n    length_non_null = test_data[col].notna().sum()\n    if length_non_null <= 0.7*len(test_data):\n        print(col)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T06:41:24.764298Z","iopub.execute_input":"2022-07-11T06:41:24.764760Z","iopub.status.idle":"2022-07-11T06:41:24.780637Z","shell.execute_reply.started":"2022-07-11T06:41:24.764720Z","shell.execute_reply":"2022-07-11T06:41:24.779308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Note that there are special null values for some columns. \n\nThe following columns define set null values if the correspinding feature has value 0:\n- Alley -> no alley access\n- BsmtQual -> no basement\n- BsmtCond -> no basement\n- BsmtExposure -> no basement\n- BsmtFinType1 -> no basement\n- BsmtFinType2 -> no basement\n- FireplaceQu -> no fireplace\n- GarageType -> no garage\n- GarageFinish\n- GarageQual\n- GarageCond\n- PoolQC -> no pool\n- Fence -> no fence","metadata":{}},{"cell_type":"markdown","source":"corr_matrix = X_train.corr()\nfig = plt.figure(figsize=(10, 10))\ndisplay(corr_matrix)\nsns.heatmap(corr_matrix);# Visualisation","metadata":{"execution":{"iopub.status.busy":"2022-07-11T05:46:02.796320Z","iopub.execute_input":"2022-07-11T05:46:02.797721Z","iopub.status.idle":"2022-07-11T05:46:02.803874Z","shell.execute_reply.started":"2022-07-11T05:46:02.797666Z","shell.execute_reply":"2022-07-11T05:46:02.802312Z"}}},{"cell_type":"code","source":"corr_matrix = train_data.corr()\nfig = plt.figure(figsize=(10, 10))\ndisplay(corr_matrix)\nsns.heatmap(corr_matrix);","metadata":{"execution":{"iopub.status.busy":"2022-07-11T06:41:24.841342Z","iopub.execute_input":"2022-07-11T06:41:24.841814Z","iopub.status.idle":"2022-07-11T06:41:25.903193Z","shell.execute_reply.started":"2022-07-11T06:41:24.841778Z","shell.execute_reply":"2022-07-11T06:41:25.901986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('display.max_rows', 500)\ncorr_to_price = np.abs(corr_matrix[\"SalePrice\"]).sort_values()\nprint(corr_to_price)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T06:41:25.905807Z","iopub.execute_input":"2022-07-11T06:41:25.906281Z","iopub.status.idle":"2022-07-11T06:41:25.917453Z","shell.execute_reply.started":"2022-07-11T06:41:25.906236Z","shell.execute_reply":"2022-07-11T06:41:25.916027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data preprocessing","metadata":{}},{"cell_type":"code","source":"# replace nan values with mean\nprint(type(train_data))\nimp_mean = SimpleImputer(missing_values=np.nan, strategy='mean')\nimp_mean.fit(train_data)\ntrain_data = imp_mean.transform(train_data)\nprint(type(train_data))\n\nimp_mean.fit(test_data)\ntest_data = imp_mean.transform(test_data)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T06:41:25.919416Z","iopub.execute_input":"2022-07-11T06:41:25.920113Z","iopub.status.idle":"2022-07-11T06:41:25.948017Z","shell.execute_reply.started":"2022-07-11T06:41:25.920067Z","shell.execute_reply":"2022-07-11T06:41:25.947259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Fit","metadata":{}},{"cell_type":"code","source":"print(train_data)\nprint(train_columns)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T06:41:25.949629Z","iopub.execute_input":"2022-07-11T06:41:25.949908Z","iopub.status.idle":"2022-07-11T06:41:25.957040Z","shell.execute_reply.started":"2022-07-11T06:41:25.949880Z","shell.execute_reply":"2022-07-11T06:41:25.956096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train =  train_data[:, -1]\nX_train = train_data[:,:-1]\n\nX_train_subset, X_cv,y_train_subset,y_cv = train_test_split(X_train,y_train,test_size=0.3)\n\nregr = RandomForestRegressor(random_state=0, oob_score=True)\nregr.fit(X_train_subset, y_train_subset)\n\npred = regr.predict(X_cv)\n\nprint(regr.score(X_cv, y_cv))\nprint(metrics.r2_score(y_cv, pred))\nprint(metrics.mean_squared_error(y_cv, pred, squared=False))","metadata":{"execution":{"iopub.status.busy":"2022-07-11T06:41:25.957974Z","iopub.execute_input":"2022-07-11T06:41:25.958302Z","iopub.status.idle":"2022-07-11T06:41:27.632176Z","shell.execute_reply.started":"2022-07-11T06:41:25.958271Z","shell.execute_reply":"2022-07-11T06:41:27.631123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{"execution":{"iopub.status.busy":"2022-07-11T06:38:25.225441Z","iopub.execute_input":"2022-07-11T06:38:25.225868Z","iopub.status.idle":"2022-07-11T06:38:25.233431Z","shell.execute_reply.started":"2022-07-11T06:38:25.225834Z","shell.execute_reply":"2022-07-11T06:38:25.231910Z"}}},{"cell_type":"code","source":"regr = RandomForestRegressor(random_state=0, oob_score=True)\nregr.fit(X_train, y_train)\n\npred = regr.predict(test_data)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T06:41:27.633682Z","iopub.execute_input":"2022-07-11T06:41:27.634002Z","iopub.status.idle":"2022-07-11T06:41:29.974160Z","shell.execute_reply.started":"2022-07-11T06:41:27.633973Z","shell.execute_reply":"2022-07-11T06:41:29.973179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'Id': house_id, 'SalePrice': pred})\noutput.to_csv('submission.csv', index=False)\nprint(\"Your submission was successfully saved!\")","metadata":{"execution":{"iopub.status.busy":"2022-07-11T06:41:29.975793Z","iopub.execute_input":"2022-07-11T06:41:29.976419Z","iopub.status.idle":"2022-07-11T06:41:29.990991Z","shell.execute_reply.started":"2022-07-11T06:41:29.976383Z","shell.execute_reply":"2022-07-11T06:41:29.989829Z"},"trusted":true},"execution_count":null,"outputs":[]}]}