EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Data Science

Model Deployment and Production

Computer Science / Data Science tutorial chapter - Published 2025-12-17 - Data Science

Model Selection

Model selection involves choosing the best-performing machine learning model from a set of candidates. This is often based on performance metrics like accuracy, precision, recall, F1 score, or others, depending on the specific problem (classification, regression, etc.).

  • Cross-Validation: A common technique used for model selection. It involves splitting the dataset into multiple folds and training the model on different folds while validating on the remaining data. This helps to avoid overfitting and ensures the model generalizes well to unseen data.
  • Grid Search and Random Search: These are techniques used to tune hyperparameters (parameters set before training) by searching through a predefined set of hyperparameter values (Grid Search) or randomly sampling from a distribution of hyperparameters (Random Search).

Example: Grid Search for Hyperparameter Tuning

from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVC
from sklearn.datasets import load_iris

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

# Define a model
model = SVC()

# Define a parameter grid
param_grid = {
    'C': [0.1, 1, 10],
    'kernel': ['linear', 'rbf'],
    'gamma': [0.1, 1, 10]
}

# Use GridSearchCV to find the best parameters
grid_search = GridSearchCV(model, param_grid, cv=5)
grid_search.fit(X, y)

# Print the best parameters
print(f"Best Parameters: {grid_search.best_params_}")
End of lesson.