Model Prediction and Evaluation
Predicting and Evaluating Models
Welcome back! In the previous lesson, we learned how to train a simple machine learning model using the Linear SVM method with the caret package in R. Now that you have a trained model, it's time to make predictions and evaluate how well your model performs.
What You'll Learn
In this lesson, you will learn how to:
- Make predictions using your trained model.
- Evaluate the performance of your model using a confusion matrix.
Making predictions and evaluating your model are crucial steps in the machine learning workflow. They help you assess whether your model performs well on unseen data and identify areas for improvement.
Data Splitting and Model Training
In this lesson, we continue from where we left off in the last lesson. Before making predictions, remember that the initial steps include loading the dataset, splitting the data into training and testing sets, and training your model. Here’s a brief reminder of those steps:
Making Predictions
Once you have your trained model, the next step is to make predictions on your test data. In R, you can use the predict function from the caret package to generate these predictions. Here's how you can do it:
In this code snippet:
modelis your trained machine learning model.irisTestis your test dataset, which contains the same features as your training data, including the target labels. Thepredictfunction will ignore the labels and use only the features for making predictions.
Evaluating the Model: Confusion Matrix
After making predictions, you need to evaluate how well your model performed. One of the common ways to do this is by using a confusion matrix. But what exactly is a confusion matrix?
A confusion matrix is a table used to evaluate the performance of a classification model. It compares the actual target values with the predicted values and provides a detailed breakdown of your model's performance. The matrix includes the following terms:
- True Positives (TP): The number of correct positive predictions.
- True Negatives (TN): The number of correct negative predictions.
- False Positives (FP): The number of incorrect positive predictions.
- False Negatives (FN): The number of incorrect negative predictions.
The rows in a confusion matrix represent the actual classes, and the columns represent the predicted classes. This makes it easy to see where your model is making correct and incorrect classifications.
Here's how you can create and print a confusion matrix in R:
In this code snippet:
confusionMatrixis a function from thecaretpackage that takes the predictions and the actual target values as arguments and returns a confusion matrix.irisTest$Speciesis the actual target values from your test dataset.
Here’s a rough idea of what you might see:
