Module 6 of 25 · AI & Machine Learning Fundamentals · Beginner

Model Evaluation and Selection

Duration: 5 min

This module delves into the critical processes of evaluating and selecting machine learning models. Understanding these processes is essential for ensuring that your models are accurate, reliable, and suitable for deployment. We will cover key evaluation metrics, cross-validation techniques, and strategies for comparing different models to make informed decisions.

Evaluation Metrics

Evaluation metrics are quantitative measures used to assess the performance of a machine learning model. Common metrics include accuracy, precision, recall, F1 score, and AUC-ROC. Each metric provides different insights into model performance, and the choice of metric depends on the specific problem and the cost of different types of errors.

from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score

# Example predictions and true labels
y_true = [0, 1, 1, 0, 1, 0]
y_pred = [0, 0, 1, 0, 1, 1]

# Calculate metrics
accuracy = accuracy_score(y_true, y_pred)
precision = precision_score(y_true, y_pred)
recall = recall_score(y_true, y_pred)
f1 = f1_score(y_true, y_pred)

# Print results
print(f'Accuracy: {accuracy}')
print(f'Precision: {precision}')
print(f'Recall: {recall}')
print(f'F1 Score: {f1}')

Try it in Google Colab: Open in Colab

Accuracy: 0.6666666666666666
Precision: 0.5
Recall: 0.6666666666666666
F1 Score: 0.5714285714285714

Cross-Validation

Cross-validation is a technique used to assess the generalizability of a model by training and evaluating it on different subsets of the data. K-fold cross-validation is a common approach where the data is split into K subsets, and the model is trained and evaluated K times, each time using a different subset as the validation set and the remaining data as the training set.

from sklearn.model_selection import cross_val_score
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier

# Load dataset
iris = load_iris()
X, y = iris.data, iris.target

# Define model
model = RandomForestClassifier(n_estimators=100)

# Perform 5-fold cross-validation
scores = cross_val_score(model, X, y, cv=5)

# Print results
print(f'Cross-validation scores: {scores}')
print(f'Mean cross-validation score: {scores.mean()}')

💡 Tip: When performing cross-validation, ensure that the data is shuffled to avoid any bias in the evaluation due to the order of the data.

❓ Which evaluation metric is best for imbalanced datasets?

❓ What is the purpose of K-fold cross-validation?

← Previous Continue interactively → Next →

Related Courses