Traditional Programming vs Machine Learning
How would you teach a computer to price a house? Advitha's first idea is to write a rule: price equals the area times a fixed amount per square foot. It works for some houses and fails badly for others, because a rule like that ignores bedrooms, age and location. Her second idea is to show the computer many houses that already sold, and let it work out the pattern itself.
The first idea is traditional programming. The second is machine learning. This lesson shows the difference, with an example you can run.
Traditional Programming: People Write the Rules
In traditional programming, a person decides the rules and writes them as code. The computer follows them exactly. Here is a rule for a weather app:
temperature = 34
if temperature > 30:
print("Hot weather")
else:
print("Not hot")Hot weatherRules work well when a person can describe the task exactly, like the weather rule, a tax calculation or a fee due date. They are clear, predictable and easy to check. But many tasks are hard to describe with rules. Try writing rules that recognize a face in any light, or that price every house fairly: you would need thousands of rules, and they would still miss cases.
Machine Learning: The Computer Learns the Rules
In machine learning, you give the computer examples together with their answers, and it finds the pattern that connects them. That pattern is called a model. You then use the model on new examples it has never seen.
The House-Price Example in Python
Real machine learning finds many patterns at once, but you can see the core idea in a few lines. This program learns one number from past sales, the average price per square foot, and uses it to price a house it has never seen:
# Past sales: (area in square feet, price in US dollars)
sales = [(1100, 260_000), (2000, 475_000), (2400, 570_000)]
# Learn one number from the examples: the average price per square foot
per_sq_ft = sum(price / area for area, price in sales) / len(sales)
print(f"Learned: ${per_sq_ft:,.0f} per square foot")
# Use what was learned on a house it has never seen
new_area = 1200
print(f"Predicted price for {new_area} sq ft: ${per_sq_ft * new_area:,.0f}")Learned: $237 per square foot
Predicted price for 1200 sq ft: $284,545Notice what changed. Nobody typed $237 into the program; it came from the examples. Give it different sales, and it learns a different number. This is the simplest possible kind of learning: a real house-price model learns from thousands of sales and many features at once, such as size, bedrooms, location and age, and later chapters build one.
| Feature | What it tells the model |
|---|---|
| Size | Bigger houses usually cost more |
| Bedrooms | More bedrooms usually add value |
| Location | The same house costs more in some areas |
| Age | Newer houses often cost more |
Which Approach Fits?
| Use traditional programming when... | Use machine learning when... |
|---|---|
| The rules are known and stable, such as a tax rate | The rules are too many or too fuzzy to write, such as recognizing faces |
| Every answer must be exact, such as a bill total | A good best guess is useful, such as a price estimate |
| You must explain every step, such as a legal check | You have many examples with answers, such as past sales |
Common Mistakes
Thinking machine learning replaces programming. Machine learning systems are built with code, and they usually sit inside ordinary programs that use written rules too.
Thinking a model is always better than a rule. If the rule is known and exact, a rule is simpler, cheaper and easier to check.
Forgetting that a model only knows its examples. If every past sale came from one city, the model will price houses in another city badly.
Thinking a model gives exact answers. A model gives a best guess based on patterns, and it can be wrong.
Interview Corner
How does machine learning differ from traditional programming?
In traditional programming, a person writes the rules and the computer applies them to data to produce answers. In machine learning, the computer is given data with answers and learns the rules, called a model, which it then applies to new data.
When would you not use machine learning?
When the rules are known, stable and must be exact, such as calculating a tax or a bill. A written rule is simpler, cheaper, easier to test and easier to explain.
What is a model?
The pattern a machine learning system learns from examples, stored as numbers. It takes new input, such as a house's features, and produces an output, such as a predicted price.
Practice
Exercise: change the examples. Run the house-price program, then add a fourth sale, (1500, 420_000), to the sales list. Does the learned price per square foot go up or down? Why? Then write down one feature, other than area, that would make the prediction better, and explain why.
Check your understanding:
- Write the two "formulas" that compare traditional programming and machine learning.
- Give one task that suits written rules and one that suits machine learning.
- In the Python example, where did the price per square foot come from?
Key Takeaways
Quick reference
| Term | Meaning |
|---|---|
| Traditional programming | People write the rules; the computer follows them |
| Machine learning | The computer learns rules from examples with answers |
| Model | The learned pattern, used to make predictions |
| Feature | One piece of information about an example, such as a house's size |
| Prediction | The model's best guess for new data |
Was this tutorial helpful?