Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on. You've got this! 🚀
Supervised Learning
Supervised learning is the most common type of machine learning. The AI is trained on labeled examples and learns to predict labels for new inputs.
How Supervised Learning Works
In supervised learning:
1. You collect data with correct answers (labeled data)
2. You give the algorithm many (input, correct answer) pairs
3. The algorithm learns the mapping from input to output
4. It then predicts outputs for inputs it has never seen
The 'supervision' comes from the correct labels you provide.
Labeled data → supervised, no labels → unsupervised, rewards → RL
Two Types: Classification and Regression
- Classification: predict a category. Output is one of a fixed set of options
- Classification examples: spam or not spam, cat or dog, disease or no disease
- Regression: predict a number. Output is a continuous value
- Regression examples: house price, temperature tomorrow, expected delivery time
- The algorithm you choose depends on whether your label is a category or a number
Supervised Learning in Code
# Supervised learning: classification example
# Predict if a student will pass or fail based on study time
# Training data: (study_hours, will_pass?)
training = [
(1, False),
(2, False),
(3, True),
(4, True),
(5, True),
(0, False),
(6, True),
(2.5, False),
]
# Learn the pattern: what study hours tend to lead to passing?
pass_hours = [h for h, passed in training if passed]
fail_hours = [h for h, passed in training if not passed]
avg_pass = sum(pass_hours) / len(pass_hours)
avg_fail = sum(fail_hours) / len(fail_hours)
threshold = (avg_pass + avg_fail) / 2
print(f"Average hours for passing students: {avg_pass:.1f}")
print(f"Average hours for failing students: {avg_fail:.1f}")
print(f"Learned threshold: {threshold:.1f} hours")
print()
# Predict for new students (supervised classifier)
def predict_pass(study_hours):
return study_hours >= threshold
test_students = [1.5, 3.5, 2.0, 4.5]
print("Predictions for new students:")
for hours in test_students:
prediction = predict_pass(hours)
print(f" {hours} hours study -> {'Will pass' if prediction else 'At risk'}")Common Mistake
Warning
Supervised learning requires labeled data. If you only have data without labels, you cannot use supervised learning directly. Labeling data is often time-consuming and expensive, which is why data scientists work hard to use labels efficiently.
Key Takeaways
- Supervised learning is the most common type of machine learning.
- Classification: predict a category. Output is one of a fixed set of options
- Classification examples: spam or not spam, cat or dog, disease or no disease
- Regression: predict a number. Output is a continuous value