Machine learning is when a computer finds rules from example data instead of getting them from a person. You show the system inputs and the correct answers. It finds a mathematical model that describes the link between them. Then it uses the model to predict answers for inputs it has not seen. The simplest machine learning model is a straight line through a set of points.
This post builds that model in a few lines of Python, so you can see training, prediction, and error with real numbers.
How does machine learning work with an example?
Maya asked six classmates how many hours they studied for an exam and what score they got:
| Student | Hours | Score |
|---|---|---|
| 1 | 2 | 55 |
| 2 | 4 | 62 |
| 3 | 5 | 68 |
| 4 | 7 | 75 |
| 5 | 8 | 79 |
| 6 | 10 | 88 |
The question: can we predict the score of a new student who studied 6 hours?
Finding the line that fits the points best is what "training a model" means:
import numpy as np hours = np.array([2, 4, 5, 7, 8, 10]) score = np.array([55, 62, 68, 75, 79, 88]) slope, intercept = np.polyfit(hours, score, 1) print(round(slope, 2), round(intercept, 2))
The output is 4.12 46.45. The model learned this rule:
score ≈ 46.45 + 4.12 × hours
In this data, one more hour of study goes with about 4 more points. Nobody wrote that rule. It came from the data.
How does a model make a prediction?
You put a new input into the rule:
new_hours = 6 print(round(intercept + slope * new_hours, 1))
The prediction for 6 hours is about 71.2 points. Nobody in the data studied exactly 6 hours. The model used the examples it saw to answer for a case it did not see. This is what we expect from machine learning.
How do you measure how good a model is?
The line does not pass through every point. For each student, the difference between the real score and the predicted score is the error:
predicted = intercept + slope * hours print([round(float(e), 1) for e in score - predicted])
The output is [0.3, -0.9, 1.0, -0.3, -0.4, 0.4]. The first student scored 0.3 points above the prediction. The second scored 0.9 points below it. Training means finding the line that makes the sum of the squared errors as small as possible. Much bigger models follow the same idea: pick a measure of error, then adjust the model step by step to reduce it.
What are the key terms in machine learning?
| Term | In this example |
|---|---|
| Feature (input) | Hours studied |
| Target or label (output) | Exam score |
| Training data | The six student records |
| Model | score ≈ 46.45 + 4.12 × hours |
| Training | Finding the slope and intercept |
| Prediction | Computing a score for a new number of hours |
Real systems use hundreds of features instead of one. To predict the price of a home, you might use area, location, age, and floor. Straight lines are replaced by more flexible models such as decision trees and neural networks. The idea stays the same: learn from examples, reduce the error, predict new cases.
What is the difference between supervised and unsupervised learning?
- Supervised learning: the data includes the correct answers, like the scores above. Examples: price prediction, spam detection.
- Unsupervised learning: there are no correct answers. The system finds groups in the data by itself. Example: grouping similar customers.
- Reinforcement learning: the system learns by trial and error from rewards and penalties. Example: programs that learn to play games.
What should you watch out for?
- Going outside the data. The model saw 2 to 10 hours. For 30 hours, the line predicts about 170 points, which makes no sense because a score cannot pass 100.
- Too little data. A rule from six students may not hold in another class.
- Link is not cause. Students who study more may simply care more about the subject. The model only sees that two numbers move together. Read more in [correlation vs causation](/blog/correlation-vs-causation).
Common mistakes
- Thinking a model is error-free. Every prediction has some error.
- Trusting results on the training data only. Test a model on data it did not train on. The AI course lesson [test on unseen data](/learn/ai/test-on-unseen-data) shows how.
- Seeing machine learning as separate from statistics. The model in this post is simple linear regression, a standard statistics tool.
What to read next
- See the same line in a full course in [Artificial Intelligence Foundations](/courses/ai), for example the lesson [line and model choice](/learn/ai/line-and-model-choice).
- Get the big picture first in [what is artificial intelligence](/blog/what-is-artificial-intelligence).
- Learn how chatbots predict words in [how chatbots work](/blog/how-ai-chatbots-work).
- #machine learning
- #regression
- #python