Machine Learning Techniques: From Theory to Practice

Duration1h AI Allowed

This course introduces the main families of machine learning techniques, including unsupervised learning, supervised learning, cross-validation, hyperparameter tuning, and model evaluation. It was prepared by IMT Atlantique for the Data Science Toolkit and Applications course.

Table of contents

  1. Introduction to Machine Learning
  2. Unsupervised Learning: Finding Hidden Patterns
  3. Supervised Learning: Predicting the Future from the Past
  4. Cross-Validation: Ensuring Robust Models
  5. Hyperparameter Tuning: Fine-Tuning Your Model
  6. Evaluation Metrics: Measuring Model Performance
  7. Focus on P-value
  8. Conclusion and Next Steps
  9. Appendix: Cheat Sheets

1. Introduction to Machine Learning

1.1 What is Machine Learning?

Machine Learning (ML) is a subset of Artificial Intelligence (AI) that enables systems to learn from data and improve their performance over time without being explicitly programmed. It is a core component of Data Science, allowing us to build models that can:

  • Predict future outcomes (e.g., stock prices, customer churn).
  • Classify data into categories (e.g., spam vs. not spam, human vs. not human, dog/cat/bird/…).
  • Cluster data into groups (e.g., customer similar profiles, ).
  • Find patterns in complex datasets (e.g., association rules in retail).

1.2 Why is Machine Learning Important?

  • Automation: Reduces the need for manual rule-based programming.
  • Scalability: Handles large and complex datasets efficiently.
  • Insights: Uncovers hidden patterns and relationships in data.
  • Decision-Making: Provides data-driven insights for better business decisions.

1.3 Types of Machine Learning

There are several types of machine learning, each with special characteristics and applications. Some of the main types of machine learning algorithms are as follows:

  • Supervised learning
  • Unsupervised learning
  • Reinforcement learning

Additional categories include semi-supervised and self-supervised learning, which combine elements of supervised and unsupervised approaches.

ml-types ml-types

Figure 1: Types of machine learning.

Type Description Example Use Cases Algorithms
Supervised Learns from labeled data (input-output pairs). Spam detection, sales forecasting Logistic Regression, Random Forest, SVM
Unsupervised Learns from unlabeled data (no output). Customer segmentation, anomaly detection K-Means, DBSCAN, PCA
Reinforcement Learns by trial and error (rewards/punishments). Robotics, game AI Q-Learning, Deep Q-Networks

2. Unsupervised Learning: Finding Hidden Patterns

2.1 Definition and Core Concepts

Unsupervised Learning is a type of ML where the model learns from unlabeled data (i.e., data without known outputs). The goal is to discover hidden patterns or groupings in the data.

unsupervised-learning unsupervised-learning

Key terminology

  • Features (X): Input variables (same as supervised learning).
  • Clusters: Groups of similar data points.
  • Dimensionality Reduction: Reducing the number of features while preserving information.

2.2 Types of Unsupervised Learning Problems

Type Description Example Use Cases Algorithms
Clustering Group similar data points together. Customer segmentation, image segmentation K-Means, DBSCAN, Hierarchical
Association Find relationships between variables. Market basket analysis, recommendation systems Apriori, FP-Growth
Dimensionality Reduction Reduce the number of features. Visualization, feature selection PCA, t-SNE, LDA

2.3 Key Algorithms for Unsupervised Learning

2.3.1 Clustering Algorithms

K-Means Clustering

Partitions data into k clusters by minimizing the within-cluster variance.

How it works — Initialize k centroids, assign points to nearest centroid, recalculate centroids, repeat until convergence.

kmeans kmeans

Pros Simple and fast, works well for spherical clusters.

Cons Requires choosing k, sensitive to outliers, assumes spherical clusters of similar size.

Use cases — Customer segmentation, image compression, document clustering.

DBSCAN (Density-Based Spatial Clustering of Applications with Noise)

Groups data points that are close to each other (based on density) and marks outliers as noise.

Key Parameters — epsilon (ε, The maximum distance between two points for them to be considered as neighbors), min_samples (The minimum number of points required to form a dense region).

dbscan dbscan

DBSCAN-KMEANS DBSCAN-KMEANS

Pros No need to specify k, can find arbitrarily shaped clusters, robust to outliers.

Cons Sensitive to epsilon and min_samples, struggles with clusters of varying densities.

Use Cases — Anomaly detection, spatial data clustering, identifying dense regions in data.

Use K-Means when:

  • You already know (or can estimate) the number of clusters.
  • Clusters are compact and approximately spherical.
  • The dataset contains few outliers.
  • Speed is important. The dataset has many dimensions.

Use DBSCAN when:

  • You do not know the number of clusters.
  • You expect irregular cluster shapes.
  • The data contain outliers or noise.
  • You want to detect anomalies.
  • Cluster membership should depend on local density.

2.3.2 Association Algorithms

Apriori Algorithm

Finds frequent itemsets and derives association rules from them.

Key Metrics — Support, Confidence, Lift.

Pros Simple and interpretable, works well for transactional data.

Cons Computationally expensive for large datasets, requires setting minimum support and confidence thresholds.

Use Cases — Market basket analysis, recommendation systems.

Antecedent Consequent Support Confidence Lift
{Bread} {Milk} 0.30 0.80 1.2
{Butter} {Milk} 0.20 0.90 1.5

2.3.3 Dimensionality Reduction Algorithms

PCA (Principal Component Analysis)

Reduces the dimensionality of a dataset while preserving as much variance as possible.

Steps — Standardize data, compute covariance matrix, calculate eigenvectors, select top k eigenvectors, project data onto new subspace.

PCA PCA

Pros Reduces noise and redundancy, speeds up machine learning algorithms, helps in visualizing high-dimensional data.

Cons Loss of interpretability, assumes linear relationships between variables.

Use Cases — Visualizing high-dimensional data, feature selection, data compression.

2.3.4 Code Examples for Unsupervised Learning

K-Means clustering example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
from sklearn.cluster import KMeans
from sklearn.datasets import load_iris
import matplotlib.pyplot as plt

iris = load_iris()
X = iris.data[:, :2]

kmeans = KMeans(n_clusters=3, random_state=42)
kmeans.fit(X)
y_pred = kmeans.predict(X)

plt.scatter(X[:, 0], X[:, 1], c=y_pred, cmap='viridis')
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], marker='x', color='red', s=100)
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('K-Means Clustering (k=3)')
plt.show()
DBSCAN example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from sklearn.cluster import DBSCAN
from sklearn.datasets import make_moons
import matplotlib.pyplot as plt

X, _ = make_moons(n_samples=300, noise=0.05, random_state=42)
dbscan = DBSCAN(eps=0.3, min_samples=5)
y_pred = dbscan.fit_predict(X)

plt.scatter(X[:, 0], X[:, 1], c=y_pred, cmap='viridis')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('DBSCAN Clustering')
plt.show()
Apriori example using mlxtend
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Install mlxtend if not already installed: pip install mlxtend
from mlxtend.frequent_patterns import apriori, association_rules
import pandas as pd

dataset = [['Milk', 'Onion', 'Nutmeg', 'Kidney Beans', 'Eggs', 'Yogurt'],
           ['Dill', 'Onion', 'Nutmeg', 'Kidney Beans', 'Eggs', 'Yogurt'],
           ['Milk', 'Apple', 'Kidney Beans', 'Eggs']]

te = pd.get_dummies(pd.DataFrame(dataset, columns=['Transaction']))
te = te.astype(int)

frequent_itemsets = apriori(te, min_support=0.5, use_colnames=True)
rules = association_rules(frequent_itemsets, metric="lift", min_threshold=1.0)
print(rules[['antecedents', 'consequents', 'support', 'confidence', 'lift']])
PCA example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
from sklearn.decomposition import PCA
from sklearn.datasets import load_iris
import matplotlib.pyplot as plt

iris = load_iris()
X = iris.data

pca = PCA(n_components=2)
X_pca = pca.fit_transform(X)

plt.scatter(X_pca[:, 0], X_pca[:, 1], c=iris.target, cmap='viridis')
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')
plt.title('PCA: Dimensionality Reduction')
plt.show()

3. Supervised Learning: Predicting the Future from the Past

3.1 Definition and Core Concepts

Supervised Learning is a type of ML where the model is trained on a labeled dataset (i.e., input data with known output labels). The goal is to learn a mapping from inputs to outputs so that the model can predict the output for new, unseen inputs.

Key terminology

  • Features (X): Input variables (e.g., age, income, education).
  • Label/Target (y): Output variable (e.g., “churn” or “no churn”).
  • Training: The process of fitting the model to the labeled data.
  • Prediction: Using the trained model to predict outputs for new inputs.

supervised supervised

3.2 Types of Supervised Learning Problems

3.2.1 Classification

Goal: Predict a discrete label (category) for a given input. Examples: Spam detection (spam/not spam), medical diagnosis (disease/no disease), customer churn prediction (churn/no churn).

3.2.2 Regression

Goal: Predict a continuous value for a given input. Examples: House price prediction, sales forecasting, temperature prediction.

3.3 Key Algorithms for Supervised Learning

3.3.1 Classification Algorithms

Logistic Regression

A linear model for binary classification that uses the logistic function (sigmoid) to predict probabilities.

logistic-regression logistic-regression

Pros Simple and interpretable, works well for linearly separable data, computationally efficient.

Cons Assumes linear relationship, sensitive to outliers, not suitable for complex non-linear relationships.

Use Cases — Binary classification (e.g., yes/no, true/false), risk assessment (e.g., loan approval).

Random Forest

An ensemble learning method that builds multiple decision trees during training and outputs the mode (classification) or mean (regression) of the individual trees.

random-forest random-forest

Pros Handles non-linear relationships well, robust to outliers and noise, works with high-dimensional data.

Cons Less interpretable than decision trees, can overfit if trees are too deep, slower than linear models for large datasets.

Use Cases — Classification (e.g., image recognition), regression (e.g., predicting house prices).

Support Vector Machines (SVM)

Finds the optimal hyperplane that best separates the classes in the feature space.

Key Concepts — Support Vectors, Margin, Kernel Trick.

svm svm

Pros Effective in high-dimensional spaces, robust to overfitting, works well with small datasets.

Cons Sensitive to feature scaling, not ideal for large datasets, less interpretable than linear models.

Use Cases — Text classification, image recognition, bioinformatics.

3.3.2 Regression Algorithms

Linear Regression

A linear approach to modeling the relationship between a dependent variable (target) and one or more independent variables (features).

Mathematical Formulation — $y = w_1x_1 + w_2x_2 + \cdots + w_nx_n + b$

linear-regression linear-regression

Pros Simple and easy to interpret, fast to train, works well for linear relationships.

Cons Assumes linearity, sensitive to outliers, poor performance for non-linear data.

Use Cases — Predicting house prices, sales forecasting, trend analysis.

XGBoost (Extreme Gradient Boosting)

An ensemble learning method based on gradient boosting. Builds trees sequentially, where each new tree corrects the errors of the previous one.

Xgboost Xgboost

Pros High performance, handles missing values automatically, robust to outliers.

Cons Complex to tune, slower than Random Forest for training, less interpretable.

Use Cases — Structured/tabular data, time-series forecasting, anomaly detection.

3.3.3 Code Examples for Supervised Learning

Logistic regression example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report
from sklearn.datasets import load_breast_cancer

data = load_breast_cancer()
X, y = data.data, data.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

model = LogisticRegression(max_iter=1000)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)

print(f"Accuracy: {accuracy_score(y_test, y_pred):.2f}")
print(classification_report(y_test, y_pred))
Random Forest example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris

data = load_iris()
X, y = data.data, data.target

model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X, y)

importances = model.feature_importances_
features = data.feature_names
for feature, importance in zip(features, importances):
    print(f"{feature}: {importance:.2f}")

4. Cross-Validation: Ensuring Robust Models

4.1 Definition and Core Concepts

Cross-Validation is a resampling technique used to evaluate machine learning models by partitioning the original dataset into training and validation subsets.

Why use cross-validation?

  • Avoid Overfitting: Ensures the model performs well on unseen data.
  • Better Estimate of Model Performance: Provides a more reliable estimate of the model’s generalization error.
  • Optimal Use of Data: Maximizes the use of available data for both training and validation.

4.2 Techniques for Cross-Validation

4.2.1 Holdout Method

Splits the dataset into training set (typically 70-80%) and test set (typically 20-30%).

Pros Simple and fast, easy to implement.

Cons High variance in performance estimates, underutilizes data.

4.2.2 k-Fold Cross-Validation

Divides the dataset into k equal-sized folds. The model is trained on k-1 folds and validated on the remaining fold.

kfold-cross_validation kfold-cross_validation

Pros Lower variance in performance estimates, maximizes data usage.

Cons Computationally expensive, not ideal for very large datasets.

4.2.3 GroupKFold Cross-Validation

  • GroupKFold is used when samples are not independent because multiple observations belong to the same group.
  • Its goal is to prevent data leakage by ensuring that all samples from a given group appear either in the training set or in the test set, but never in both.
  • When to use GroupKFold: Use it whenever your dataset contains repeated measurements or related observations.

4.2.4 Stratified k-Fold Cross-Validation

A variant of k-fold that preserves the percentage of samples for each class in each fold.

Pros Handles imbalanced datasets well, provides a more reliable estimate for classification problems.

Cons Slightly more complex to implement than standard k-fold.

4.2.5 Code Examples for Cross-Validation

Holdout method example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris
from sklearn.metrics import accuracy_score

data = load_iris()
X, y = data.data, data.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

model = RandomForestClassifier(random_state=42)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
print(f"Test Accuracy: {accuracy_score(y_test, y_pred):.2f}")
k-fold cross-validation example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
from sklearn.model_selection import KFold, cross_val_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris

data = load_iris()
X, y = data.data, data.target
model = RandomForestClassifier(random_state=42)

kfold = KFold(n_splits=5, shuffle=True, random_state=42)
scores = cross_val_score(model, X, y, cv=kfold, scoring='accuracy')
print(f"Cross-Validation Scores: {scores}")
print(f"Mean Accuracy: {scores.mean():.2f}{scores.std():.2f})")

5. Hyperparameter Tuning: Fine-Tuning Your Model

5.1 Definition and Core Concepts

Hyperparameters are external configurations of a machine learning model that are not learned during training (e.g., number of trees in a Random Forest, learning rate in XGBoost). Hyperparameter tuning is the process of finding the optimal combination of hyperparameters to maximize model performance.

5.2 Techniques for Hyperparameter Tuning

Exhaustively searches over a predefined set of hyperparameter values.

Pros Guarantees finding the best combination within the specified range, simple to implement.

Cons Computationally expensive, not feasible for large hyperparameter spaces.

Randomly samples hyperparameter combinations from a predefined distribution.

Pros Faster than Grid Search for large spaces, often finds good combinations quickly.

Cons No guarantee of finding the best combination, may miss important regions of the hyperparameter space.

5.2.3 Bayesian Optimization

Uses probabilistic models (e.g., Gaussian Processes) to guide the search for optimal hyperparameters.

Pros More efficient than Grid/Random Search, can handle complex hyperparameter spaces.

Cons Complex to implement, requires more computational overhead per iteration.

hyperparameter-tuning hyperparameter-tuning

5.2.4 Code Examples for Hyperparameter Tuning

Grid Search example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris

data = load_iris()
X, y = data.data, data.target

param_grid = {
    'n_estimators': [50, 100, 200],
    'max_depth': [None, 5, 10, 20],
    'min_samples_split': [2, 5, 10]
}

model = RandomForestClassifier(random_state=42)
grid_search = GridSearchCV(model, param_grid, cv=5, scoring='accuracy', n_jobs=-1)
grid_search.fit(X, y)

print(f"Best Hyperparameters: {grid_search.best_params_}")
print(f"Best Accuracy: {grid_search.best_score_:.2f}")

6. Evaluation Metrics: Measuring Model Performance

6.1 Evaluation Metrics for Classification

6.1.1 Confusion Matrix

Actual \ Predicted Positive Negative
Positive True Positive (TP) False Negative (FN)
Negative False Positive (FP) True Negative (TN)

6.1.2 Derived Metrics from Confusion Matrix

1. Accuracy
  • Shows how many predictions the model got right out of all the predictions.
  • It gives idea of overall performance but it can be misleading when one class is more dominant over the other.
  • For example a model that predicts the majority class correctly most of the time might have high accuracy but still fail to capture important details about other classes.
  • Formula: (TP + TN) / (TP + TN + FP + FN)
  • When to Use: Balanced datasets.
2. Precision
  • Precision focus on the quality of the model’s positive predictions.
  • It tells us how many of the “positive” predictions were actually correct.
  • It is important in situations where false positives need to be minimized such as detecting spam emails or fraud.
  • Formula: TP / (TP + FP)
  • When to Use: High cost of false positives.
3. Recall (Sensitivity or True Positive Rate)
  • Recall measures how how good the model is at predicting positives.
  • It shows the proportion of true positives detected out of all the actual positive instances.
  • High recall is essential when missing positive cases has significant consequences like in medical tests.
  • Formula: TP / (TP + FN)
  • When to Use: High cost of false negatives.
4. F1-Score
  • F1-score combines precision and recall into a single metric to balance their trade-off.
  • It provides a better sense of a model’s overall performance particularly for imbalanced datasets.
  • It is helpful when both false positives and false negatives are important though it assumes precision and recall are equally important but in some situations one might matter more than the other.
  • Formula: 2 * (Precision * Recall) / (Precision + Recall)
  • When to Use: Balance between precision and recall.
5. ROC Curve and AUC
  • The ROC curve gives a visual representation of the trade-offs between the true positive rate (TPR) and false positive rate (FPR) at various thresholds.
  • ROC curve provides insights into how well the model can balance the trade-offs between detecting positive instances and avoiding false positives across different thresholds.
  • AUC, or Area Under the Curve, is a single scalar value ranging from 0 to 1, that gives a performance snapshot of the model.
  • You only calculate AUC after generating the ROC curve because the AUC represents the area beneath the curve.
  • TPR (Recall): TP / (TP + FN)
  • FPR: FP / (FP + TN)
  • AUC: Area Under Curve (1.0 = perfect, 0.5 = random).

ROC-AUC ROC-AUC

6.1.3 Code Example: Classification Metrics

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, roc_auc_score, confusion_matrix, classification_report
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier

data = load_breast_cancer()
X, y = data.data, data.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

model = RandomForestClassifier(random_state=42)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
y_proba = model.predict_proba(X_test)[:, 1]

print(f"Accuracy: {accuracy_score(y_test, y_pred):.2f}")
print(f"Precision: {precision_score(y_test, y_pred):.2f}")
print(f"Recall: {recall_score(y_test, y_pred):.2f}")
print(f"F1-Score: {f1_score(y_test, y_pred):.2f}")
print(f"ROC-AUC: {roc_auc_score(y_test, y_proba):.2f}")
print(f"Confusion Matrix:\n{confusion_matrix(y_test, y_pred)}")
print(f"Classification Report:\n{classification_report(y_test, y_pred)}")

6.2 Evaluation Metrics for Regression

6.2.1 Mean Absolute Error (MAE)

  • Mean Absolute Error measures the average absolute difference between actual and predicted values.
  • It treats all errors equally, regardless of their direction and provides results in the same unit as the target variable, making it easy to interpret.
  • Formula: mean(|y_true - y_pred|)
  • When to Use: Simple, interpretable metric.
  • Interpretation example: MAE = 42.79: An MAE of 42.79 indicates that, on average, the model’s predictions deviate from the actual values by approximately 42.79 units.

6.2.2 Mean Squared Error (MSE)

  • Mean Squared Error calculates the average of squared differences between actual and predicted values.
  • By squaring errors, it penalizes larger mistakes more strongly, making it sensitive to outliers.
  • Formula: mean((y_true - y_pred)²)
  • When to Use: Penalizes large errors more heavily.
  • Interpretation example: MSE=2900.19: An MSE of 2900.19 shows that the average of the squared prediction errors is 2900.19, meaning the model incurs some large errors that are heavily penalized.

6.2.3 Root Mean Squared Error (RMSE)

  • Root Mean Squared Error is the square root of MSE.
  • It maintains the strong penalty for large errors while converting the result back to the original unit of the target variable, improving interpretability.
  • Formula: sqrt(MSE)
  • When to Use: Same units as the target.
  • Interpretation example: RMSE=53.85: An RMSE of 53.85 suggests that the model’s predictions typically differ from the actual values by about 53.85 units, in the same scale as the target variable.

6.2.4 R-Squared (R²)

  • R-squared represents the proportion of variance in the target variable that is explained by the regression model.
  • It provides insight into how well the model captures underlying data patterns.
  • Formula: 1 - (SS_res / SS_tot)
  • When to Use: Compare models, measure explanatory power.
  • Interpretation example: R²=0.45: An R² value of 0.45 indicates that the model explains approximately 45% of the variance in the target variable, reflecting moderate predictive capability.

6.2.5 Code Example: Regression Metrics

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression

X, y = make_regression(n_samples=100, n_features=1, noise=10, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

model = LinearRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)

print(f"MAE: {mean_absolute_error(y_test, y_pred):.2f}")
print(f"MSE: {mean_squared_error(y_test, y_pred):.2f}")
print(f"RMSE: {mean_squared_error(y_test, y_pred, squared=False):.2f}")
print(f"R²: {r2_score(y_test, y_pred):.2f}")

6.3 Evaluation Metrics for Clustering

6.3.1 Silhouette Score

  • Formula: (b - a) / max(a, b)
    • a = Average distance between a sample and all other points in the same cluster.
    • b = Average distance between a sample and all other points in the nearest cluster.
  • When to Use: Evaluate clustering quality.

6.3.2 Davies-Bouldin Index

  • Formula: Average ratio of intra-cluster distance to inter-cluster distance.
  • When to Use: Compare clustering algorithms.

6.3.3 Code Example: Clustering Metrics

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from sklearn.metrics import silhouette_score, davies_bouldin_score
from sklearn.cluster import KMeans
from sklearn.datasets import load_iris

data = load_iris()
X = data.data

kmeans = KMeans(n_clusters=3, random_state=42)
kmeans.fit(X)
y_pred = kmeans.predict(X)

print(f"Silhouette Score: {silhouette_score(X, y_pred):.2f}")
print(f"Davies-Bouldin Index: {davies_bouldin_score(X, y_pred):.2f}")

7. Focus on P-value

7.1 Core concept

A p-value (probability value) is a statistical measure used in hypothesis testing to help decide whether the results of an experiment are meaningful or likely due to random chance.

  • It represents the probability of observing results as extreme as the ones obtained assuming the null hypothesis $H_0$ is true.
  • In simple terms it answers the question “If nothing unusual is happening, how surprising are these results?”
  • A small p-value means the observed results are unlikely to occur by chance alone providing strong evidence against the null hypothesis.
  • A large p-value suggests the results are consistent with random variation and do not provide enough evidence to reject the null hypothesis.

If we have a target variable (the case in regression for example), the p-value helps answer the question: “Is this predictor truly associated with the target, or could the observed relationship be due to random chance?”

Where to use: P-value can be used in several cases

  • Pre-processing and Feature Selection in Modeling: During model training, p-values help identify variables that significantly impact predictions.
  • Medical Research: P-values are widely used to determine whether a drug or treatment has a significant effect on patients.
  • Business Decision Making: In business analytics p-values help evaluate whether observed trends or patterns are statistically significant aiding informed decisions.
  • Quality Control: P-values are used in manufacturing to check if variations in production are due to random chance or indicate a real problem.
  • Social Science Research: Researchers use p-values to confirm if observed effects are significant.

7.2 Interpretation example

Suppose a regression output is:

p-value p-value

Age (p = 0.001)

  • Very strong evidence against $H_0$
  • Age is statistically associated with the target.

Salary (p = 0.42)

  • Weak evidence against $H_0$
  • We fail to reject $H_0$ .
  • There is insufficient evidence that Salary contributes after accounting for the other variables.
  • Note: “Fail to reject” is not the same as proving there is no effect. It means the data do not provide enough evidence for a non-zero effect.

Experience (p = 0.03)

  • Significant at the 5% level.
  • Experience likely has a real association with the target.

7.3 Common significance levels

p-value Interpretation
$p < 0.001$ Very strong evidence against $H_0$
$p < 0.01$ Strong evidence
$p < 0.05$ Statistically significant (common threshold)
$p > 0.05$ Not statistically significant

7.4 Important misconceptions

“A p-value of 0.03 means there’s a 97% chance the variable is important.”

  • Incorrect.
  • A p-value is not the probability that the null hypothesis is true.
  • It is:“Assuming the coefficient is truly zero, how surprising would the observed data be?”

“A small p-value means the effect is large.”

  • Incorrect.
  • The p-value measures evidence, not effect size.

Example: coefficient versus p-value

Coefficient p-value Interpretation
0.02 0.0001 coefficient is tiny but estimated very precisely
50 0.20 coefficient large but uncertain

7.5 Interpret p-values alongside the coefficient estimate

A p-value tells you whether there is statistical evidence of an effect, but it does not tell you how large or important that effect is. That’s why you should always interpret it together with the coefficient estimate.

Consider the linear regression model: $y = \beta_0 + \beta_1 x_1 + \cdots + \beta_n x_n$

For each predictor, the regression output provides:

  • Coefficient estimate $\hat{\beta}$: the estimated effect size.
  • p-value: the evidence that the true coefficient differs from zero. These answer different questions:
metric answered question
Coefficient How much does the target change?
p-value Is there evidence that this effect is non-zero?

Example

Variable Coefficient p-value Interpretation
Age 0.002 0.0001 Significant but tiny effect: Every additional year of age increases the prediction by only 0.002 units. The p-value is extremely small, so the effect is statistically significant. However, the effect is practically negligible.
Income 25 0.15 Large effect but not significant: The estimated effect is large. However, the estimate is uncertain. There is insufficient evidence to conclude the true effect differs from zero. Possible reasons include a small sample size, high variability, or multicollinearity.
Years of Experience 3.8 0.002 Significant and meaningful: Each additional year of experience increases the predicted outcome by 3.8 units. The p-value indicates strong evidence that this effect is real. The effect is both statistically significant and practically meaningful.

7.6 When can p-values be misleading?

A statistically significant p-value does not guarantee that the result is practically meaningful. Be cautious when:

  • Sample size is very large: Even tiny, practically irrelevant effects can produce very small p-values.
  • Predictors are highly correlated (multicollinearity): Standard errors increase, making truly relevant variables appear non-significant.
  • Model assumptions are violated: Ordinary least squares inference assumes linearity, independent errors, constant error variance (homoscedasticity), and approximately normal residuals for small samples. Violations can make p-values unreliable.
  • Many predictors are tested simultaneously: Some small p-values can occur by chance (multiple testing).

8. Conclusion and Next Steps

8.1 Key Takeaways

Topic Key Concepts Tools/Algorithms Metrics
Supervised Learning Predicts labels from labeled data. Logistic Regression, Random Forest, SVM Accuracy, Precision, Recall, F1, ROC-AUC
Unsupervised Learning Finds hidden patterns in unlabeled data. K-Means, DBSCAN, PCA Silhouette Score, Davies-Bouldin Index
Cross-Validation Evaluates model generalization by partitioning data. k-Fold, Stratified k-Fold Mean Accuracy, Std Dev
Hyperparameter Tuning Optimizes model performance by tuning external parameters. Grid Search, Random Search, Bayesian Optimization Best Accuracy, Best Parameters
Evaluation Metrics Measures model performance using statistical metrics. Confusion Matrix, ROC-AUC, MAE, RMSE, R² Varies by problem type

8.2 Resources for Further Learning

Books

Online courses

Tools and libraries

Datasets for practice

8.3 Next Steps

  • Learn about advanced fields: Deep Learning, NLP, or Reinforcement Learning.
  • Learn Cloud Tools: Google Vertex AI, AWS SageMaker, or Azure Machine Learning.
  • Contribute to Open Source: Scikit-learn, TensorFlow, or PyTorch on GitHub.

9. Appendix: Cheat Sheets

9.1 Supervised Learning Cheat Sheet

Algorithm Type Pros Cons Use Case
Logistic Regression Classification Interpretable, fast, simple Linear assumption, sensitive to outliers Binary classification
Random Forest Classification/Regression Handles non-linearity, robust to outliers Less interpretable, can overfit High-dimensional data
SVM Classification/Regression Effective in high dimensions, works with small data Sensitive to scaling, slow for large data Text classification, image recognition
Linear Regression Regression Simple, interpretable, fast Linear assumption, sensitive to outliers Predicting continuous values
XGBoost Classification/Regression High performance, handles missing values Complex to tune, less interpretable Structured/tabular data

9.2 Unsupervised Learning Cheat Sheet

Algorithm Type Pros Cons Use Case
K-Means Clustering Simple, fast, scalable Requires choosing k, sensitive to outliers Customer segmentation
DBSCAN Clustering No need to choose k, handles arbitrary shapes Sensitive to parameters, struggles with varying densities Anomaly detection
Apriori Association Simple, interpretable Computationally expensive, requires thresholds Market basket analysis
PCA Dimensionality Reduction Reduces noise, speeds up computation Loss of interpretability Visualization, feature selection

9.3 Cross-Validation Cheat Sheet

Technique Description Pros Cons Use Case
Holdout Method Split into train/test sets Simple, fast High variance, underutilizes data Quick evaluation
k-Fold CV Split into k folds, train on k-1, test on 1 Lower variance, maximizes data usage Computationally expensive Small to medium datasets
Stratified k-Fold CV k-Fold with preserved class distribution Handles imbalanced data Slightly more complex Classification with imbalanced classes

9.4 Hyperparameter Tuning Cheat Sheet

Technique Description Pros Cons Use Case
Grid Search Exhaustive search over predefined values Guarantees best combination Computationally expensive Small hyperparameter spaces
Random Search Random sampling of hyperparameters Faster, often finds good combinations No guarantee of optimality Large hyperparameter spaces
Bayesian Optimization Probabilistic model-guided search More efficient, handles complex spaces Complex to implement Expensive-to-train models

9.5 Evaluation Metrics Cheat Sheet

Metric Type Formula When to Use Range
Accuracy Classification $\frac{TP + TN}{TP + TN + FP + FN}$ Balanced datasets $[0, 1]$
Precision Classification $\frac{TP}{TP + FP}$ High cost of false positives $[0, 1]$
Recall Classification $\frac{TP}{TP + FN}$ High cost of false negatives $[0, 1]$
F1-Score Classification $\frac{2 \times \mathrm{Precision} \times \mathrm{Recall}}{\mathrm{Precision} + \mathrm{Recall}}$ Balance between precision and recall $[0, 1]$
ROC-AUC Classification Area under ROC curve Binary classification with imbalanced classes $[0, 1]$
MAE Regression $\frac{1}{n}\sum_{i=1}^{n}\lvert y_i - \hat{y}_i \rvert$ Simple interpretable metric $[0, +\infty)$
MSE Regression $\frac{1}{n}\sum_{i=1}^{n}(y_i - \hat{y}_i)^2$ Penalizes large errors $[0, +\infty)$
RMSE Regression $\sqrt{\mathrm{MSE}}$ Same units as target $[0, +\infty)$
Regression $1 - \frac{SS_{\mathrm{res}}}{SS_{\mathrm{tot}}}$ Compare models, measure explanatory power $(-\infty, 1]$
Silhouette Score Clustering $\frac{b - a}{\max(a, b)}$ Evaluate clustering quality $[-1, 1]$
Davies-Bouldin Index Clustering Average ratio of intra-cluster to inter-cluster distances Compare clustering algorithms $[0, +\infty)$

Knowledge Check

Answer these questions after reading the lesson. Use the feedback to identify which concepts you should review before starting the practical activities.

--- primary_color: steelblue secondary_color: lightgray text_color: black shuffle_questions: false shuffle_answers: true --- # Which situation is an unsupervised learning problem? 1. [ ] Predicting a house price from previously labelled sales 2. [x] Grouping customers by similarity without predefined categories 3. [ ] Classifying an email as spam using labelled examples 4. [ ] Forecasting tomorrow's temperature from historical targets # What is the main purpose of PCA? 1. [ ] Assign predefined class labels to observations 2. [ ] Select the optimal number of trees in a Random Forest 3. [x] Reduce dimensionality while preserving as much variance as possible 4. [ ] Detect causal relationships between variables # When is DBSCAN generally more appropriate than K-Means? 1. [x] When clusters may have irregular shapes and the data contain noise 2. [ ] When every cluster is known to be spherical and equally sized 3. [ ] When the number of clusters must always be specified in advance 4. [ ] When the task is supervised classification # Why use stratified k-fold cross-validation for an imbalanced classification problem? 1. [ ] It removes minority-class observations before training 2. [ ] It guarantees identical predictions in every fold 3. [ ] It replaces the need for an evaluation metric 4. [x] It preserves the class proportions in each fold # What is an advantage of Random Search over Grid Search in a large hyperparameter space? 1. [ ] It evaluates every possible combination 2. [x] It can explore the space more efficiently by sampling combinations 3. [ ] It guarantees the global optimum 4. [ ] It does not require a model evaluation metric # Which classification metric should receive particular attention when false positives are especially costly? 1. [ ] Recall 2. [ ] R-squared 3. [x] Precision 4. [ ] Mean Squared Error # What does an R-squared value of 0.45 indicate? 1. [ ] The model makes correct predictions for exactly 45% of observations 2. [ ] Every predictor has a p-value below 0.45 3. [ ] The model's average error is 0.45 units 4. [x] The model explains approximately 45% of the variance in the target # How should a p-value of 0.03 for a regression coefficient be interpreted at the 5% significance level? 1. [ ] There is a 97% probability that the predictor is important 2. [x] The result is statistically significant at the 5% level 3. [ ] The predictor necessarily has a large practical effect 4. [ ] The null hypothesis has been proven false