1 line
19 KiB
Plaintext
1 line
19 KiB
Plaintext
{"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":"# House Prices Prediction using TensorFlow Decision Forests","metadata":{"id":"5v5mm4amQRrm","papermill":{"duration":0.010092,"end_time":"2023-03-07T06:21:39.774967","exception":false,"start_time":"2023-03-07T06:21:39.764875","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"## Import the library","metadata":{"id":"FVOXAyXl3-fA","papermill":{"duration":0.008317,"end_time":"2023-03-07T06:21:39.809564","exception":false,"start_time":"2023-03-07T06:21:39.801247","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import tensorflow as tf\nimport tensorflow_decision_forests as tfdf\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\n\n%matplotlib inline","metadata":{"id":"IGmyjJJatzBZ","papermill":{"duration":8.300496,"end_time":"2023-03-07T06:21:48.118668","exception":false,"start_time":"2023-03-07T06:21:39.818172","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-09-22T13:28:22.176827Z","iopub.execute_input":"2023-09-22T13:28:22.178428Z","iopub.status.idle":"2023-09-22T13:28:33.635611Z","shell.execute_reply.started":"2023-09-22T13:28:22.178337Z","shell.execute_reply":"2023-09-22T13:28:33.634306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"TensorFlow v\" + tf.__version__)\nprint(\"TensorFlow Decision Forests v\" + tfdf.__version__)","metadata":{"id":"dh4qwB4iN7Ue","papermill":{"duration":0.019012,"end_time":"2023-03-07T06:21:48.149058","exception":false,"start_time":"2023-03-07T06:21:48.130046","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-09-22T13:28:33.639823Z","iopub.execute_input":"2023-09-22T13:28:33.641567Z","iopub.status.idle":"2023-09-22T13:28:33.649957Z","shell.execute_reply.started":"2023-09-22T13:28:33.641502Z","shell.execute_reply":"2023-09-22T13:28:33.648230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load the dataset\n","metadata":{"id":"-3vxMmCPvqpf","papermill":{"duration":0.009922,"end_time":"2023-03-07T06:21:48.16945","exception":false,"start_time":"2023-03-07T06:21:48.159528","status":"completed"},"tags":[]}},{"cell_type":"code","source":"train_file_path = \"../input/house-prices-advanced-regression-techniques/train.csv\"\ndataset_df = pd.read_csv(train_file_path)\nprint(\"Full train dataset shape is {}\".format(dataset_df.shape))","metadata":{"id":"JVMPH_IDOBH2","papermill":{"duration":0.066785,"end_time":"2023-03-07T06:21:48.245226","exception":false,"start_time":"2023-03-07T06:21:48.178441","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-09-22T13:28:33.653508Z","iopub.execute_input":"2023-09-22T13:28:33.654825Z","iopub.status.idle":"2023-09-22T13:28:33.744694Z","shell.execute_reply.started":"2023-09-22T13:28:33.654772Z","shell.execute_reply":"2023-09-22T13:28:33.742737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_df.head(5)","metadata":{"papermill":{"duration":0.049873,"end_time":"2023-03-07T06:21:48.321938","exception":false,"start_time":"2023-03-07T06:21:48.272065","status":"completed"},"tags":[],"id":"kgbP5R6X416m","execution":{"iopub.status.busy":"2023-09-22T13:28:33.746512Z","iopub.execute_input":"2023-09-22T13:28:33.747025Z","iopub.status.idle":"2023-09-22T13:28:33.800553Z","shell.execute_reply.started":"2023-09-22T13:28:33.746964Z","shell.execute_reply":"2023-09-22T13:28:33.798999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_df = dataset_df.drop('Id', axis=1)\ndataset_df.head(3)","metadata":{"papermill":{"duration":0.043419,"end_time":"2023-03-07T06:21:48.412206","exception":false,"start_time":"2023-03-07T06:21:48.368787","status":"completed"},"tags":[],"id":"0lItmbYS416n","execution":{"iopub.status.busy":"2023-09-22T13:28:33.804486Z","iopub.execute_input":"2023-09-22T13:28:33.805342Z","iopub.status.idle":"2023-09-22T13:28:33.842940Z","shell.execute_reply.started":"2023-09-22T13:28:33.805295Z","shell.execute_reply":"2023-09-22T13:28:33.841467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_df.info()","metadata":{"papermill":{"duration":0.046783,"end_time":"2023-03-07T06:21:48.489619","exception":false,"start_time":"2023-03-07T06:21:48.442836","status":"completed"},"tags":[],"id":"du6DU4Of416n","execution":{"iopub.status.busy":"2023-09-22T13:28:33.845224Z","iopub.execute_input":"2023-09-22T13:28:33.845872Z","iopub.status.idle":"2023-09-22T13:28:33.888900Z","shell.execute_reply.started":"2023-09-22T13:28:33.845816Z","shell.execute_reply":"2023-09-22T13:28:33.887491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## House Price Distribution\n\nNow let us take a look at how the house prices are distributed.","metadata":{"papermill":{"duration":0.010252,"end_time":"2023-03-07T06:21:48.510224","exception":false,"start_time":"2023-03-07T06:21:48.499972","status":"completed"},"tags":[],"id":"PxdZCHvk416o"}},{"cell_type":"code","source":"print(dataset_df['SalePrice'].describe())\nplt.figure(figsize=(9, 8))\nsns.distplot(dataset_df['SalePrice'], color='g', bins=100, hist_kws={'alpha': 0.4});","metadata":{"papermill":{"duration":0.497946,"end_time":"2023-03-07T06:21:49.018361","exception":false,"start_time":"2023-03-07T06:21:48.520415","status":"completed"},"tags":[],"id":"qROZWZyE416o","execution":{"iopub.status.busy":"2023-09-22T13:28:33.890632Z","iopub.execute_input":"2023-09-22T13:28:33.891033Z","iopub.status.idle":"2023-09-22T13:28:34.670938Z","shell.execute_reply.started":"2023-09-22T13:28:33.890993Z","shell.execute_reply":"2023-09-22T13:28:34.669647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list(set(dataset_df.dtypes.tolist()))","metadata":{"papermill":{"duration":0.022381,"end_time":"2023-03-07T06:21:49.0727","exception":false,"start_time":"2023-03-07T06:21:49.050319","status":"completed"},"tags":[],"id":"-hrMItSC416o","execution":{"iopub.status.busy":"2023-09-22T13:28:34.672385Z","iopub.execute_input":"2023-09-22T13:28:34.672871Z","iopub.status.idle":"2023-09-22T13:28:34.681082Z","shell.execute_reply.started":"2023-09-22T13:28:34.672831Z","shell.execute_reply":"2023-09-22T13:28:34.679881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_num = dataset_df.select_dtypes(include = ['float64', 'int64'])\ndf_num.head()","metadata":{"papermill":{"duration":0.038307,"end_time":"2023-03-07T06:21:49.122233","exception":false,"start_time":"2023-03-07T06:21:49.083926","status":"completed"},"tags":[],"id":"Vg2PQvfb416o","execution":{"iopub.status.busy":"2023-09-22T13:28:34.683180Z","iopub.execute_input":"2023-09-22T13:28:34.683693Z","iopub.status.idle":"2023-09-22T13:28:34.717425Z","shell.execute_reply.started":"2023-09-22T13:28:34.683624Z","shell.execute_reply":"2023-09-22T13:28:34.716012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_num.hist(figsize=(16, 20), bins=50, xlabelsize=8, ylabelsize=8);","metadata":{"papermill":{"duration":8.021473,"end_time":"2023-03-07T06:21:57.176534","exception":false,"start_time":"2023-03-07T06:21:49.155061","status":"completed"},"tags":[],"id":"Dj4h_dIw416o","execution":{"iopub.status.busy":"2023-09-22T13:28:34.720635Z","iopub.execute_input":"2023-09-22T13:28:34.721432Z","iopub.status.idle":"2023-09-22T13:28:44.223029Z","shell.execute_reply.started":"2023-09-22T13:28:34.721360Z","shell.execute_reply":"2023-09-22T13:28:44.221930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n\ndef split_dataset(dataset, test_ratio=0.30):\n test_indices = np.random.rand(len(dataset)) < test_ratio\n return dataset[~test_indices], dataset[test_indices]\n\ntrain_ds_pd, valid_ds_pd = split_dataset(dataset_df)\nprint(\"{} examples in training, {} examples in testing.\".format(\n len(train_ds_pd), len(valid_ds_pd)))","metadata":{"id":"tsQad0t7SBv2","papermill":{"duration":0.025712,"end_time":"2023-03-07T06:21:57.266147","exception":false,"start_time":"2023-03-07T06:21:57.240435","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-09-22T13:28:44.224308Z","iopub.execute_input":"2023-09-22T13:28:44.225263Z","iopub.status.idle":"2023-09-22T13:28:44.237012Z","shell.execute_reply.started":"2023-09-22T13:28:44.225214Z","shell.execute_reply":"2023-09-22T13:28:44.235551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = 'SalePrice'\ntrain_ds = tfdf.keras.pd_dataframe_to_tf_dataset(train_ds_pd, label=label, task = tfdf.keras.Task.REGRESSION)\nvalid_ds = tfdf.keras.pd_dataframe_to_tf_dataset(valid_ds_pd, label=label, task = tfdf.keras.Task.REGRESSION)","metadata":{"id":"xQgimfirSGQ9","papermill":{"duration":0.26438,"end_time":"2023-03-07T06:21:57.596711","exception":false,"start_time":"2023-03-07T06:21:57.332331","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-09-22T13:28:44.238378Z","iopub.execute_input":"2023-09-22T13:28:44.238819Z","iopub.status.idle":"2023-09-22T13:28:44.611052Z","shell.execute_reply.started":"2023-09-22T13:28:44.238774Z","shell.execute_reply":"2023-09-22T13:28:44.609079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tfdf.keras.get_all_models()","metadata":{"id":"MFmnkRR_Ui9w","papermill":{"duration":0.024872,"end_time":"2023-03-07T06:21:57.685403","exception":false,"start_time":"2023-03-07T06:21:57.660531","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-09-22T13:28:44.613563Z","iopub.execute_input":"2023-09-22T13:28:44.614028Z","iopub.status.idle":"2023-09-22T13:28:44.623408Z","shell.execute_reply.started":"2023-09-22T13:28:44.613986Z","shell.execute_reply":"2023-09-22T13:28:44.621886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n","metadata":{"id":"irxAS91IRVAX","papermill":{"duration":0.012674,"end_time":"2023-03-07T06:21:57.73704","exception":false,"start_time":"2023-03-07T06:21:57.724366","status":"completed"},"tags":[]}},{"cell_type":"code","source":"rf = tfdf.keras.RandomForestModel(task = tfdf.keras.Task.REGRESSION)\nrf.compile(metrics=[\"mse\"]) # Optional, you can use this to include a list of eval metrics","metadata":{"id":"O7bqOQMYTRXZ","papermill":{"duration":0.079382,"end_time":"2023-03-07T06:21:57.854964","exception":false,"start_time":"2023-03-07T06:21:57.775582","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-09-22T13:28:44.629529Z","iopub.execute_input":"2023-09-22T13:28:44.630491Z","iopub.status.idle":"2023-09-22T13:28:44.694265Z","shell.execute_reply.started":"2023-09-22T13:28:44.630436Z","shell.execute_reply":"2023-09-22T13:28:44.692887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf.fit(x=train_ds)","metadata":{"id":"Ax6RircN92LW","papermill":{"duration":14.312048,"end_time":"2023-03-07T06:22:12.207321","exception":false,"start_time":"2023-03-07T06:21:57.895273","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-09-22T13:28:44.696148Z","iopub.execute_input":"2023-09-22T13:28:44.696580Z","iopub.status.idle":"2023-09-22T13:29:00.814266Z","shell.execute_reply.started":"2023-09-22T13:28:44.696540Z","shell.execute_reply":"2023-09-22T13:29:00.812804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tfdf.model_plotter.plot_model_in_colab(rf, tree_idx=0, max_depth=3)","metadata":{"id":"mTx73NgET9f8","papermill":{"duration":0.126324,"end_time":"2023-03-07T06:22:12.377534","exception":false,"start_time":"2023-03-07T06:22:12.25121","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-09-22T13:29:00.815869Z","iopub.execute_input":"2023-09-22T13:29:00.816278Z","iopub.status.idle":"2023-09-22T13:29:00.848514Z","shell.execute_reply.started":"2023-09-22T13:29:00.816238Z","shell.execute_reply":"2023-09-22T13:29:00.846868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nlogs = rf.make_inspector().training_logs()\nplt.plot([log.num_trees for log in logs], [log.evaluation.rmse for log in logs])\nplt.xlabel(\"Number of trees\")\nplt.ylabel(\"RMSE (out-of-bag)\")\nplt.show()","metadata":{"id":"ryddKoqLWrTp","papermill":{"duration":0.229991,"end_time":"2023-03-07T06:22:12.653052","exception":false,"start_time":"2023-03-07T06:22:12.423061","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-09-22T13:29:00.851091Z","iopub.execute_input":"2023-09-22T13:29:00.851570Z","iopub.status.idle":"2023-09-22T13:29:01.119830Z","shell.execute_reply.started":"2023-09-22T13:29:00.851525Z","shell.execute_reply":"2023-09-22T13:29:01.118610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inspector = rf.make_inspector()\ninspector.evaluation()","metadata":{"id":"gdY8DvriTxky","papermill":{"duration":0.032483,"end_time":"2023-03-07T06:22:12.732339","exception":false,"start_time":"2023-03-07T06:22:12.699856","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-09-22T13:29:01.121709Z","iopub.execute_input":"2023-09-22T13:29:01.122890Z","iopub.status.idle":"2023-09-22T13:29:01.135450Z","shell.execute_reply.started":"2023-09-22T13:29:01.122830Z","shell.execute_reply":"2023-09-22T13:29:01.133802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"evaluation = rf.evaluate(x=valid_ds,return_dict=True)\n\nfor name, value in evaluation.items():\n print(f\"{name}: {value:.4f}\")","metadata":{"id":"39x97YqWZlgm","papermill":{"duration":1.513826,"end_time":"2023-03-07T06:22:14.294393","exception":false,"start_time":"2023-03-07T06:22:12.780567","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-09-22T13:29:01.137497Z","iopub.execute_input":"2023-09-22T13:29:01.138539Z","iopub.status.idle":"2023-09-22T13:29:02.060861Z","shell.execute_reply.started":"2023-09-22T13:29:01.138481Z","shell.execute_reply":"2023-09-22T13:29:02.059444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Available variable importances:\")\nfor importance in inspector.variable_importances().keys():\n print(\"\\t\", importance)","metadata":{"id":"xok16_jMgGZH","papermill":{"duration":0.028662,"end_time":"2023-03-07T06:22:14.371495","exception":false,"start_time":"2023-03-07T06:22:14.342833","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-09-22T13:29:02.062478Z","iopub.execute_input":"2023-09-22T13:29:02.062866Z","iopub.status.idle":"2023-09-22T13:29:02.072383Z","shell.execute_reply.started":"2023-09-22T13:29:02.062829Z","shell.execute_reply":"2023-09-22T13:29:02.070816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inspector.variable_importances()[\"NUM_AS_ROOT\"]","metadata":{"id":"eI073gJHgHxr","papermill":{"duration":0.02844,"end_time":"2023-03-07T06:22:14.449021","exception":false,"start_time":"2023-03-07T06:22:14.420581","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-09-22T13:29:02.074708Z","iopub.execute_input":"2023-09-22T13:29:02.075341Z","iopub.status.idle":"2023-09-22T13:29:02.092148Z","shell.execute_reply.started":"2023-09-22T13:29:02.075300Z","shell.execute_reply":"2023-09-22T13:29:02.090592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 4))\n\n# Mean decrease in AUC of the class 1 vs the others.\nvariable_importance_metric = \"NUM_AS_ROOT\"\nvariable_importances = inspector.variable_importances()[variable_importance_metric]\n\n# Extract the feature name and importance values.\n#\n# `variable_importances` is a list of <feature, importance> tuples.\nfeature_names = [vi[0].name for vi in variable_importances]\nfeature_importances = [vi[1] for vi in variable_importances]\n# The feature are ordered in decreasing importance value.\nfeature_ranks = range(len(feature_names))\n\nbar = plt.barh(feature_ranks, feature_importances, label=[str(x) for x in feature_ranks])\nplt.yticks(feature_ranks, feature_names)\nplt.gca().invert_yaxis()\n\n# TODO: Replace with \"plt.bar_label()\" when available.\n# Label each bar with values\nfor importance, patch in zip(feature_importances, bar.patches):\n plt.text(patch.get_x() + patch.get_width(), patch.get_y(), f\"{importance:.4f}\", va=\"top\")\n\nplt.xlabel(variable_importance_metric)\nplt.title(\"NUM AS ROOT of the class 1 vs the others\")\nplt.tight_layout()\nplt.show()","metadata":{"id":"cyyzelTl53AH","execution":{"iopub.status.busy":"2023-09-22T13:29:02.093622Z","iopub.execute_input":"2023-09-22T13:29:02.094941Z","iopub.status.idle":"2023-09-22T13:29:02.509208Z","shell.execute_reply.started":"2023-09-22T13:29:02.094890Z","shell.execute_reply":"2023-09-22T13:29:02.507504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission\nFinally predict on the competition test data using the model.","metadata":{"papermill":{"duration":0.016075,"end_time":"2023-03-07T06:22:14.482026","exception":false,"start_time":"2023-03-07T06:22:14.465951","status":"completed"},"tags":[],"id":"jM9uB_7T416r"}},{"cell_type":"code","source":"test_file_path = \"../input/house-prices-advanced-regression-techniques/test.csv\"\ntest_data = pd.read_csv(test_file_path)\nids = test_data.pop('Id')\n\ntest_ds = tfdf.keras.pd_dataframe_to_tf_dataset(\n test_data,\n task = tfdf.keras.Task.REGRESSION)\n\npreds = rf.predict(test_ds)\noutput = pd.DataFrame({'Id': ids,\n 'SalePrice': preds.squeeze()})\n\noutput.head()\n","metadata":{"papermill":{"duration":1.717453,"end_time":"2023-03-07T06:22:16.215717","exception":false,"start_time":"2023-03-07T06:22:14.498264","status":"completed"},"tags":[],"id":"gLySv9yJ416s","execution":{"iopub.status.busy":"2023-09-22T13:29:02.510762Z","iopub.execute_input":"2023-09-22T13:29:02.511146Z","iopub.status.idle":"2023-09-22T13:29:03.966947Z","shell.execute_reply.started":"2023-09-22T13:29:02.511109Z","shell.execute_reply":"2023-09-22T13:29:03.965488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission_df = pd.read_csv('../input/house-prices-advanced-regression-techniques/sample_submission.csv')\nsample_submission_df['SalePrice'] = rf.predict(test_ds)\nsample_submission_df.to_csv('/kaggle/working/submission.csv', index=False)\nsample_submission_df.head()","metadata":{"papermill":{"duration":0.839277,"end_time":"2023-03-07T06:22:17.072045","exception":false,"start_time":"2023-03-07T06:22:16.232768","status":"completed"},"tags":[],"id":"0wALPJtE416s","execution":{"iopub.status.busy":"2023-09-22T13:29:03.968793Z","iopub.execute_input":"2023-09-22T13:29:03.970219Z","iopub.status.idle":"2023-09-22T13:29:04.349799Z","shell.execute_reply.started":"2023-09-22T13:29:03.970161Z","shell.execute_reply":"2023-09-22T13:29:04.348486Z"},"trusted":true},"execution_count":null,"outputs":[]}]} |