We use cookies to enhance your experience on the site
CodeWorlds

Supervised Learning

Supervised Learning is the most commonly used type of ML. The model learns from pairs of (input, correct output).

Classification vs Regression

Classification - Predicting Categories

1from sklearn.datasets import make_classification
2from sklearn.linear_model import LogisticRegression
3
4# Generate Safari data
5X, y = make_classification(n_samples=1000, n_features=4, n_classes=3)
6# y: 0 = Lion, 1 = Elephant, 2 = Cheetah
7
8# Logistic Regression for classification
9model = LogisticRegression()
10model.fit(X_train, y_train)
11
12# Predicting class
13prediction = model.predict([[180, 200, 4, 0.8]])  # Lion?
14probabilities = model.predict_proba([[180, 200, 4, 0.8]])

Regression - Predicting Continuous Values

1from sklearn.linear_model import LinearRegression
2
3# Predicting population based on reserve area
4X = [[100], [200], [300], [400]]  # area in km²
5y = [150, 280, 420, 550]           # population
6
7model = LinearRegression()
8model.fit(X, y)
9
10# Prediction
11new_area = [[250]]
12predicted_population = model.predict(new_area)
13print(f"Predicted population: {predicted_population[0]:.0f}")

Evaluation Metrics

Classification Metrics

1from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
2from sklearn.metrics import confusion_matrix, classification_report
3
4y_true = [0, 1, 1, 0, 1, 1, 0, 0]
5y_pred = [0, 1, 0, 0, 1, 1, 1, 0]
6
7# Accuracy - % of correct predictions
8print(f"Accuracy: {accuracy_score(y_true, y_pred):.2%}")
9
10# Precision - how many of the predicted positives were correct
11print(f"Precision: {precision_score(y_true, y_pred):.2%}")
12
13# Recall - how many of the actual positives did we detect
14print(f"Recall: {recall_score(y_true, y_pred):.2%}")
15
16# F1-score - harmonic mean of precision and recall
17print(f"F1: {f1_score(y_true, y_pred):.2%}")
18
19# Confusion Matrix
20print(confusion_matrix(y_true, y_pred))
21# [[TN, FP],
22#  [FN, TP]]
23
24# Full report
25print(classification_report(y_true, y_pred))

Regression Metrics

1from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
2import numpy as np
3
4y_true = [100, 200, 300, 400]
5y_pred = [110, 190, 310, 380]
6
7# MSE - Mean Squared Error
8mse = mean_squared_error(y_true, y_pred)
9print(f"MSE: {mse:.2f}")
10
11# RMSE - Root Mean Squared Error
12rmse = np.sqrt(mse)
13print(f"RMSE: {rmse:.2f}")
14
15# MAE - Mean Absolute Error
16mae = mean_absolute_error(y_true, y_pred)
17print(f"MAE: {mae:.2f}")
18
19# R² - coefficient of determination (0-1, closer to 1 is better)
20r2 = r2_score(y_true, y_pred)
21print(f"R²: {r2:.4f}")

Cross-validation

1from sklearn.model_selection import cross_val_score, KFold
2
3model = RandomForestClassifier()
4
5# 5-fold cross-validation
6scores = cross_val_score(model, X, y, cv=5)
7print(f"CV scores: {scores}")
8print(f"Mean: {scores.mean():.2%} (+/- {scores.std() * 2:.2%})")
9
10# Custom KFold
11kfold = KFold(n_splits=5, shuffle=True, random_state=42)
12for train_idx, val_idx in kfold.split(X):
13    X_train, X_val = X[train_idx], X[val_idx]
14    y_train, y_val = y[train_idx], y[val_idx]
15    # Train and evaluate...
Go to CodeWorlds