Back to Article
model_selection.ipynb
Download Notebook

Here, a simple Machine Learning pipeline will be conducted using the \(k\)-NN model. Importing packages.

In [2]:
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split, GridSearchCV, KFold
from sklearn.neighbors import KNeighborsClassifier

Loading the Iris dataset.

In [4]:
iris = load_iris()
X, y = iris.data, iris.target

print(f"Dataset shape: {X.shape}")
print(f"Classes: {iris.target_names}")
Dataset shape: (150, 4)
Classes: ['setosa' 'versicolor' 'virginica']

Performing the train and test split, with 70/30 ratio.

In [6]:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=100)

Creating a hyperparameter search space.

In [8]:
param_grid = {'n_neighbors': [1, 3, 5, 7, 9, 11, 13, 15]}

Setting a \(5\)-fold Cross-Validation.

In [10]:
cv = KFold(n_splits=5, shuffle=True, random_state=100)

Fitting and finding the best value of \(k\).

In [12]:
grid_search = GridSearchCV(
    estimator=KNeighborsClassifier(),
    param_grid=param_grid,
    cv=cv,
    scoring='accuracy'
)

grid_search.fit(X_train, y_train)

print(f"Best k: {grid_search.best_params_['n_neighbors']}")
print(f"Validation Accuracy: {grid_search.best_score_:.4f}")
Best k: 3
Validation Accuracy: 0.9714

Evaluating in the test set.

In [14]:
best_knn = grid_search.best_estimator_
final_test_accuracy = best_knn.score(X_test, y_test)

print(f"Test Accuracy: {final_test_accuracy:.4f}")
Test Accuracy: 0.9778