Machine Learning cheatsheet
Syntax snippets and quick notes for revision.
Machine Learning Introduction
dataset = [
{"feature": 1.2, "label": "low"},
{"feature": 3.8, "label": "high"},
{"feature": 2.4, "label": "medium"},
]
features = [row["feature"] for row in dataset]
labels = [row["label"] for row in dataset]
print({"rows": len(dataset), "features": features, "labels": labels})Use this machine learning introduction pattern when a Machine Learning task needs a small, readable starting point.
AI vs ML vs Deep Learning
dataset = [
{"feature": 1.2, "label": "low"},
{"feature": 3.8, "label": "high"},
{"feature": 2.4, "label": "medium"},
]
features = [row["feature"] for row in dataset]
labels = [row["label"] for row in dataset]
print({"rows": len(dataset), "features": features, "labels": labels})Use this ai vs ml vs deep learning pattern when a Machine Learning task needs a small, readable starting point.
ML Problem Framing
dataset = [
{"feature": 1.2, "label": "low"},
{"feature": 3.8, "label": "high"},
{"feature": 2.4, "label": "medium"},
]
features = [row["feature"] for row in dataset]
labels = [row["label"] for row in dataset]
print({"rows": len(dataset), "features": features, "labels": labels})Use this ml problem framing pattern when a Machine Learning task needs a small, readable starting point.
Datasets and Features
dataset = [
{"feature": 1.2, "label": "low"},
{"feature": 3.8, "label": "high"},
{"feature": 2.4, "label": "medium"},
]
features = [row["feature"] for row in dataset]
labels = [row["label"] for row in dataset]
print({"rows": len(dataset), "features": features, "labels": labels})Use this datasets and features pattern when a Machine Learning task needs a small, readable starting point.
Labels and Targets
dataset = [
{"feature": 1.2, "label": "low"},
{"feature": 3.8, "label": "high"},
{"feature": 2.4, "label": "medium"},
]
features = [row["feature"] for row in dataset]
labels = [row["label"] for row in dataset]
print({"rows": len(dataset), "features": features, "labels": labels})Use this labels and targets pattern when a Machine Learning task needs a small, readable starting point.
Supervised Learning
dataset = [
{"feature": 1.2, "label": "low"},
{"feature": 3.8, "label": "high"},
{"feature": 2.4, "label": "medium"},
]
features = [row["feature"] for row in dataset]
labels = [row["label"] for row in dataset]
print({"rows": len(dataset), "features": features, "labels": labels})Use this supervised learning pattern when a Machine Learning task needs a small, readable starting point.
Unsupervised Learning
dataset = [
{"feature": 1.2, "label": "low"},
{"feature": 3.8, "label": "high"},
{"feature": 2.4, "label": "medium"},
]
features = [row["feature"] for row in dataset]
labels = [row["label"] for row in dataset]
print({"rows": len(dataset), "features": features, "labels": labels})Use this unsupervised learning pattern when a Machine Learning task needs a small, readable starting point.
Regression
hours = [1, 2, 3, 4]
scores = [42, 51, 63, 72]
slope = 10
intercept = 32
predictions = [slope * hour + intercept for hour in hours]
mae = sum(abs(actual - pred) for actual, pred in zip(scores, predictions)) / len(scores)
print({"predictions": predictions, "mae": mae})Use this regression pattern when a Machine Learning task needs a small, readable starting point.
Classification
actual = [1, 0, 1, 1, 0, 0]
predicted = [1, 0, 0, 1, 1, 0]
true_positive = sum(a == 1 and p == 1 for a, p in zip(actual, predicted))
false_positive = sum(a == 0 and p == 1 for a, p in zip(actual, predicted))
false_negative = sum(a == 1 and p == 0 for a, p in zip(actual, predicted))
precision = true_positive / (true_positive + false_positive)
recall = true_positive / (true_positive + false_negative)
print({"precision": round(precision, 2), "recall": round(recall, 2)})Use this classification pattern when a Machine Learning task needs a small, readable starting point.
Clustering
points = [2, 3, 10, 11, 12, 25]
centers = [3, 11]
clusters = {center: [] for center in centers}
for point in points:
nearest = min(centers, key=lambda center: abs(point - center))
clusters[nearest].append(point)
print(clusters)Use this clustering pattern when a Machine Learning task needs a small, readable starting point.
Train Test Split
rows = list(range(1, 11))
train_rows = rows[:6]
validation_rows = rows[6:8]
test_rows = rows[8:]
print({"train": train_rows, "validation": validation_rows, "test": test_rows})Use this train test split pattern when a Machine Learning task needs a small, readable starting point.
Validation Set
rows = list(range(1, 11))
train_rows = rows[:6]
validation_rows = rows[6:8]
test_rows = rows[8:]
print({"train": train_rows, "validation": validation_rows, "test": test_rows})Use this validation set pattern when a Machine Learning task needs a small, readable starting point.