This walk-through trains and evaluates eight popular classifiers on the classic Iris data set using only petal length and width as features. Each model is wrapped in the same three-step pattern: instantiate, fit, and score; then we visualize the learned decision boundaries.
Preparation
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
iris = load_iris()
X = pd.DataFrame(iris.data[:, 2:], columns=['petal_length', 'petal_width'])
y = pd.Series(iris.target)
le = LabelEncoder()
y_enc = le.fit_transform(y)
X_train, X_test, y_train, y_test = train_test_split(
X, y_enc, test_size=0.3, random_state=42, stratify=y_enc)
Helper for plotting
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
def plot_boundary(model, X, y):
mesh_x, mesh_y = np.meshgrid(
np.linspace(X.iloc[:, 0].min() - .5, X.iloc[:, 0].max() + .5, 300),
np.linspace(X.iloc[:, 1].min() - .5, X.iloc[:, 1].max() + .5, 300))
grid = np.c_[mesh_x.ravel(), mesh_y.ravel()]
preds = model.predict(grid).reshape(mesh_x.shape)
plt.figure(figsize=(7, 5))
plt.pcolormesh(mesh_x, mesh_y, preds,
cmap=ListedColormap(['#c7e9c0', '#ffcccc', '#c6dbef']))
plt.scatter(X.iloc[:, 0], X.iloc[:, 1], c=y,
cmap=ListedColormap(['green', 'red', 'blue']),
edgecolors='k')
plt.xlabel('petal length (cm)')
plt.ylabel('petal width (cm)')
plt.title(model.__class__.__name__)
plt.show()
- k-Nearest Neighbours
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X_train, y_train)
print('KNN accuracy:', knn.score(X_test, y_test))
plot_boundary(knn, X, y_enc)
- Gaussian Naive Bayes
from sklearn.naive_bayes import GaussianNB)
gnb = GaussianNB()
gnb.fit(X_train, y_train)
print('Naive Bayes accuracy:', gnb.score(X_test, y_test))
plot_boundary(gnb, X, y_enc)
- Decision Tree (CART)
from sklearn.tree import DecisionTreeClassifier
tree = DecisionTreeClassifier(random_state=42)
tree.fit(X_train, y_train)
print('Decision Tree accuracy:', tree.score(X_test, y_test))
plot_boundary(tree, X, y_enc)
- Logistic Regression (Multinomial)
from sklearn.linear_model import LogisticRegression
log_reg = LogisticRegression(max_iter=1000, solver='lbfgs', multi_class='auto')
log_reg.fit(X_train, y_train)
print('Logistic Regression accuracy:', log_reg.score(X_test, y_test))
plot_boundary(log_reg, X, y_enc)
- Support-Vector Machine (RBF kernel)
from sklearn.svm import SVC
svc = SVC(kernel='rbf', gamma='scale')
svc.fit(X_train, y_train)
print('SVM accuracy:', svc.score(X_test, y_test))
plot_boundary(svc, X, y_enc)
- Random Forest
from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier(n_estimators=200attr('n_estimators', 100), random_state=42)
rf.fit(X_train, y_train)
print('Random Forest accuracy:', rf.score(X_test, y_test))
plot_boundary(rf, X, y_enc)
- AdaBoost
from sklearn.ensemble import AdaBoostClassifier
ada = AdaBoostClassifier(n_estimators=50, random_state=42)
ada.fit(X_train, y_train)
print('AdaBoost accuracy:', ada.score(X_test, y_test))
plot_boundary(ada, X, y_enc)
- Gradient Boosting (GBDT)
from sklearn.ensemble import GradientBoostingClassifier
gb = GradientBoostingClassifier(random_state=42)
gb.fit(X_train, y_train)
print('Gradient Boosting accuracy:', gb.score(X_test, y_test))
plot_boundary(gb, X, y_enc)
All eight models achieve roughly 97 % accuracy on the hold-out set, yet their decision boundaries differ markedly—linear for logistic regression, piece-wise for trees, smooth for SVM, and highly irregular for ensembles—illustrating the bias-variance trade-off in practice.