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! 🚀
What is Machine Learning?
Machine Learning is a way to build AI systems by teaching computers to learn from examples rather than programming every rule by hand. It is the most widely used approach to AI today.
Machine Learning in Plain English
Machine Learning (ML) is a branch of AI where computers learn to make decisions or predictions by studying examples, rather than following rules a programmer wrote.
The key idea: instead of telling the computer exactly what to do in every situation, you show it thousands of examples of inputs and correct outputs. The computer finds the patterns and learns to make decisions on its own.
You already interact with ML every day:
- Netflix recommends shows based on what you have watched
- Your bank flags transactions that look unusual
- Your phone predicts the next word when you type a message
- Google Photos recognizes who is in your photos
Machine Learning follows a structured pipeline from data to deployment
A Simple ML Example: Flower Classification
# Classic example: classify flowers by petal measurements
# The famous Iris dataset (simplified)
# Each row: [petal_length_cm, petal_width_cm] -> flower_type
training_examples = [
([1.4, 0.2], "setosa"), # small petals
([1.3, 0.2], "setosa"),
([4.7, 1.4], "versicolor"), # medium petals
([4.5, 1.5], "versicolor"),
([6.0, 2.5], "virginica"), # large petals
([5.8, 2.2], "virginica"),
]
# A simple rule the ML algorithm might learn:
# petal_length < 2.5 -> setosa
# petal_length < 5.0 -> versicolor
# petal_length >= 5.0 -> virginica
def simple_ml_classifier(petal_length, petal_width):
# This represents what an ML model learns automatically
if petal_length < 2.5:
return "setosa"
elif petal_length < 5.0:
return "versicolor"
else:
return "virginica"
# Test the learned classifier
test_flowers = [(1.5, 0.3), (4.9, 1.8), (6.2, 2.4)]
print("Flower Classification Results:")
for length, width in test_flowers:
flower_type = simple_ml_classifier(length, width)
print(f" Petal: {length}cm x {width}cm -> {flower_type}")Try It Yourself
Line 3: Malformed tag: "< 2.5: return "setosa (small flower)" elif petal_length < 5.0: return "versicolor (medium flower)" else: return "virginica (large flower)" # Change these values and run: my_flowers = [ (1.2, 0.1), # very small (3.5, 1.0), # medium (7.0, 3.0), # very large ] print("Your flower classifications:") for length, width in my_flowers: result = classify_flower(length, width) print(f" Length={length}cm, Width={width}cm ->"
Tip: Tags must use valid HTML names and be properly formed. Example: <div>, <p class="text">
Tip
Tip
In real ML, you do not write the classification rules yourself. You use an ML library like scikit-learn. You give it the training data and it figures out the best rules automatically. We will explore scikit-learn in Module 11 when we build a real application.