Model Validation
Model validation is the process of evaluating the performance of a machine learning model. It is essential to ensure that the model generalizes well to new, unseen data. There are several techniques for model validation, including:
-
Train/Test Split: The dataset is divided into two parts: a training set and a test set. The model is trained on the training set and evaluated on the test set. This is the simplest form of model validation.
-
Cross-Validation: The dataset is divided into k subsets, or folds. The model is trained on k-1 folds and evaluated on the remaining fold. This process is repeated k times, with each fold serving as the test set once. The results are averaged to obtain a more robust estimate of the model's performance.
Overfitting and Underfitting
Model validation is crucial for detecting overfitting and underfitting:
-
Overfitting: The model performs well on the training data but poorly on new, unseen data. This indicates that the model has learned the noise in the training data rather than the underlying patterns.
-
Underfitting: The model performs poorly on both the training and test data. This indicates that the model is too simple to capture the underlying patterns in the data.
Metrics
There are several metrics used to evaluate the performance of a machine learning model:
accuracy_score
The accuracy score is the proportion of correctly classified instances out of the total instances.
from sklearn.metrics import accuracy_score
y_true = [0, 1, 1, 0, 1]
y_pred = [0, 1, 0, 0, 1]
accuracy_score(y_true, y_pred) # 0.8
This show that the model correctly classified 80% of the instances.
confusion_matrix
The confusion matrix is a table that shows the number of true positives, true negatives, false positives, and false negatives.
from sklearn.metrics import confusion_matrix
y_true = [0, 1, 1, 0, 1]
y_pred = [0, 1, 0, 0, 1]
confusion_matrix(y_true, y_pred)
"""
array([[2, 0],
[1, 2]])
"""
This shows that the model correctly classified 2 instances as class 0, 2 instances as class 1, and misclassified 1 instance as class 0 and 0 instances as class 1.
classification_report
The classification report provides a summary of the precision, recall, F1-score, and support for each class.
from sklearn.metrics import classification_report
y_true = [0, 1, 1, 0, 1]
y_pred = [0, 1, 0, 0, 1]
print(classification_report(y_true, y_pred))
"""
precision recall f1-score support
0 0.67 1.00 0.80 2
1 1.00 0.67 0.80 3
accuracy 0.80 5
macro avg 0.83 0.83 0.80 5
weighted avg 0.87 0.80 0.80 5
"""
For class 0, the recall is 100%, this shows that the model can indentify the instances of class 0 but the precision is 67%, this shows that the model can classify 67% of the instances as class 0.
For class 1, the recall is 67%, this shows that the model can indentify only 67% of class 1 instances but the precision is 100%, this shows that the model can classify all them correctly.
Summary
- Precision: The proportion of true positive predictions out of all positive predictions.
- Recall: The proportion of true positive predictions out of all actual positive instances.
- F1-score: The harmonic mean of precision and recall.
- Support: The number of instances in each class.
- Accuracy: The proportion of correctly classified instances out of the total instances.
- Macro avg: The average of the precision, recall, and F1-score across all classes.
- Weighted avg: The weighted average of the precision, recall, and F1-score across all classes.