Make predictions
Making predictions with data is one of the fundamental goals of machine learning. Now that you know how to split the data and fit a model, it's time to make predictions about unseen samples with your models.
You are going to make predictions about your test set using a model obtained by fitting the training data to a tree specification.
Available in your workspace are the datasets that you generated previously (diabetes_train
and diabetes_test
) and a decision tree specification tree_spec
, which was generated using the following code:
tree_spec <- decision_tree() %>%
set_engine("rpart") %>%
set_mode("classification")
This exercise is part of the course
Machine Learning with Tree-Based Models in R
Exercise instructions
- Fit your specification to the training data using
outcome
as the target variable and all predictors to createmodel
. - Use your model to predict the outcome of diabetes for every observation in the test set and assign the result to
predictions
. - Add the true test set outcome to
predictions
as a column namedtrue_class
and save the result aspredictions_combined
. - Use the
head()
function to print the first rows of the result.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Train your model
model <- tree_spec %>%
___
# Generate predictions
predictions <- ___(model,
___)
# Add the true outcomes
predictions_combined <- predictions %>%
___(true_class = ___)
# Print the first lines of the result
___