Print
Random Forest Regression
Read It: Random Forest Regression
Similar to the classification model, random forest regression models build a forest of trees and aggregate them. However, the aggregation with regression is different. Since regression deals with quantitative response variables, there is no need to adopt a majorty-rule voting scheme, instead, the final result is simply the average of each of the individual trees. Additionally, there are some minor changes in the code.
Below, we demonstrate how to apply a random forest regression model in Python. We will include four key steps:
- Split the data into training and test/validation sets (80/20 is a common split)
- Specify the model parameters (e.g., number of trees, maximum depth of any given tree, etc.)
- Fit the model to the training data set
- Evaluate predictive error (e.g., RMSE, MAE, etc.) using the test/validation data set
Watch It: Video - Random Forest Regression (10:18 minutes)
Try It: GOOGLE COLAB
- Click the Google Colab file used in the video here.
- Go to the Colab file and click "File" then "Save a copy in Drive", this will create a new Colab file that you can edit in your own Google Drive account.
- Once you have it saved in your Drive, try to edit the following code to run a random forest regression model:
Note: You must be logged into your PSU Google Workspace in order to access the file.
# load the dataset recs = pd.read_csv(...) # split the data into 20/80 test/training sets train, test = train_test_split(...) # reformat the data to a tensorflow dataset train_ds = tfdf.keras.pd_dataframe_to_tf_dataset(train, label = ..., ...) test_ds = tfdf.keras.pd_dataframe_to_tf_dataset(test, label = ..., ...) # create the random forest model reg_model = tfdf.keras.RandomForestModel(...) reg_model.compile(metrics = ...) # fit model with training data reg_model.fit(...) # evaluate model accuracy using test data evaluation = model.evaluate(...) # calculate RMSE rmse = ... rmse
Once you have implemented this code on your own, come back to this page to test your knowledge.