{"cells":[{"metadata":{"_uuid":"0e5c1450ffc87c62c4beeff0093d3ca1d9cf0b2e"},"cell_type":"markdown","source":"## Recap\nHere's the code you've written so far."},{"metadata":{"_uuid":"c442e75c4479f610b4c67c7396fd8d5c8e89b295","trusted":true},"cell_type":"code","source":"# Code you have previously used to load data\nimport pandas as pd\nfrom sklearn.metrics import mean_absolute_error\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.tree import DecisionTreeRegressor\n\n\n# Path of the file to read\niowa_file_path = '../input/home-data-for-ml-course/train.csv'\n\nhome_data = pd.read_csv(iowa_file_path)\n# Create target object and call it y\ny = home_data.SalePrice\n# Create X\nfeatures = ['LotArea', 'YearBuilt', '1stFlrSF', '2ndFlrSF', 'FullBath', 'BedroomAbvGr', 'TotRmsAbvGrd']\nX = home_data[features]\n\n# Split into validation and training data\ntrain_X, val_X, train_y, val_y = train_test_split(X, y, random_state=1)\n\n# Specify Model\niowa_model = DecisionTreeRegressor(random_state=1)\n# Fit Model\niowa_model.fit(train_X, train_y)\n\n# Make validation predictions and calculate mean absolute error\nval_predictions = iowa_model.predict(val_X)\nval_mae = mean_absolute_error(val_predictions, val_y)\nprint(\"Validation MAE when not specifying max_leaf_nodes: {:,.0f}\".format(val_mae))\n\n# Using best value for max_leaf_nodes\niowa_model = DecisionTreeRegressor(max_leaf_nodes=100, random_state=1)\niowa_model.fit(train_X, train_y)\nval_predictions = iowa_model.predict(val_X)\nval_mae = mean_absolute_error(val_predictions, val_y)\nprint(\"Validation MAE for best value of max_leaf_nodes: {:,.0f}\".format(val_mae))\n\n\n# Set up code checking\nfrom learntools.core import binder\nbinder.bind(globals())\nfrom learntools.machine_learning.ex6 import *\nprint(\"\\nSetup complete\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"bc2073f8c5e8b0a7da166811a3cb2c894b59b5a6"},"cell_type":"markdown","source":"# Exercises\nData science isn't always this easy. But replacing the decision tree with a Random Forest is going to be an easy win."},{"metadata":{"_uuid":"f47a80d9b4e0a5368d6d2524cc996692b3e7414f"},"cell_type":"markdown","source":"## Step 1: Use a Random Forest"},{"metadata":{"_uuid":"e64b900fdd0718dcc9718f871fdc1b5d6c8c5b64","trusted":true},"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor\n\n# Define the model. Set random_state to 1\nrf_model = RandomForestRegressor(random_state=1)\n\n# fit your model\nrf_model.fit(train_X, train_y)\n\n# Calculate the mean absolute error of your Random Forest model on the validation data\nrf_val_mae = mean_absolute_error(rf_model.predict(val_X), val_y)\n\nprint(\"Validation MAE for Random Forest Model: {}\".format(rf_val_mae))\n\nstep_1.check()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5084d9ae5ce92b7990afce5b4edff573dcb4f65e","trusted":false},"cell_type":"code","source":"# The lines below will show you a hint or the solution.\n# step_1.hint() \n# step_1.solution()\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a5aff1cd68817ea11d02cf03d9bd4e8e3e995b0c"},"cell_type":"markdown","source":"# Think about Your Results\nUnder what circumstances might you prefer the Decision Tree to the Random Forest, even though the Random Forest generally gives more accurate predictions?  Weigh in or follow the discussion in [this discussion thread](https://www.kaggle.com/learn-forum/60582).\n\n# Keep Going\n\nSo far, you have followed specific instructions at each step of your project. This helped learn key ideas and build your first model, but now you know enough to try things on your own. \n\nMachine Learning competitions are a great way to try your own ideas and learn more as you independently navigate a machine learning project.  Learn **[how to submit your work to a Kaggle competition](https://www.kaggle.com/dansbecker/submitting-from-a-kernel)**.\n\n---\n**[Course Home Page](https://www.kaggle.com/learn/machine-learning)**\n\n\n"}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}