Machine Learning Introduction
Machine learning is a branch of AI where computers learn from data instead of being given every rule.
What is Machine Learning?
In normal programming, you write the rules. The computer takes data, follows your rules, and gives an answer.
In machine learning, you give the computer examples with answers. It works out the rules by itself. Then it uses those rules to answer new questions.
| Normal programming | Machine learning |
|---|---|
| You write the rules | The computer learns the rules |
| Input: data + rules | Input: data + correct answers |
| Output: answers | Output: a model that can give answers |
| Good when rules are simple and clear | Good when rules are hard to write down |
A Rule Written by Hand
Imagine a teacher wants to predict if a student will pass. With normal programming, she writes a rule herself:
Example
def will_pass(hours_studied):
if hours_studied >= 4:
return "pass"
return "fail"
print(will_pass(6))
print(will_pass(2))pass fail
This works, but the teacher had to guess the number 4. What if sleep also matters? What if there are ten factors? Writing rules by hand quickly becomes impossible.
A Rule Learned from Data
Now let the computer learn from past students. Each student has two features (hours studied, hours slept) and one label (passed or failed):
Example
from sklearn.tree import DecisionTreeClassifier
# Hours studied and hours slept for 8 students
X = [[1, 5], [2, 6], [3, 4], [4, 7], [5, 6], [6, 8], [7, 7], [8, 6]]
# 0 = failed, 1 = passed
y = [0, 0, 0, 1, 1, 1, 1, 1]
model = DecisionTreeClassifier(random_state=0)
model.fit(X, y)
# Ali studied 6 hours and slept 7. Ayesha studied 2 hours and slept 8.
print(model.predict([[6, 7], [2, 8]]))[1 0]
The model predicts that Ali will pass (1) and Ayesha will fail (0). We never wrote an if statement. The model found the pattern itself.
The example explained
Xholds the features: the information we know about each student.yholds the labels: the correct answers we want to predict.fit()is the training step. The model studies the examples.predict()uses the trained model on new data.
Almost every scikit-learn model works the same way: create it, call fit(X, y), then call predict(new_X). Learn this pattern once and you can use dozens of models.
Where is Machine Learning Used?
- Email: spam filters decide which messages go to junk
- Banking: alerts for card payments that look like fraud
- Shopping: "customers also bought" recommendations on online stores
- Maps: travel time estimates based on traffic
- Health: helping doctors find signs of disease in scans
- Chat assistants: models like ChatGPT, Gemini and Claude are trained with machine learning on huge amounts of text
The Machine Learning Workflow
Most ML projects follow the same steps:
- Collect data about the problem
- Prepare the data: clean it and choose features
- Split the data into training and test sets
- Train a model on the training data
- Evaluate the model on the test data
- Improve it, then use it for real predictions
This tutorial follows these steps chapter by chapter.
Tools We Use
| Tool | What it does |
|---|---|
| Python | The programming language |
| scikit-learn | Ready-made ML models and tools |
| Pandas | Loading and cleaning tables of data |
| NumPy | Fast maths on lists of numbers |
Install them with pip install scikit-learn pandas. In code, scikit-learn is imported as sklearn.
Exercise
Exercise
In machine learning, what does the fit() method do?
