Machine Learning Tutorial menu

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 programmingMachine learning
You write the rulesThe computer learns the rules
Input: data + rulesInput: data + correct answers
Output: answersOutput: a model that can give answers
Good when rules are simple and clearGood 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

python
def will_pass(hours_studied):
    if hours_studied >= 4:
        return "pass"
    return "fail"

print(will_pass(6))
print(will_pass(2))
Output
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

python
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]]))
Output
[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

  • X holds the features: the information we know about each student.
  • y holds 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:

  1. Collect data about the problem
  2. Prepare the data: clean it and choose features
  3. Split the data into training and test sets
  4. Train a model on the training data
  5. Evaluate the model on the test data
  6. Improve it, then use it for real predictions

This tutorial follows these steps chapter by chapter.

Tools We Use

ToolWhat it does
PythonThe programming language
scikit-learnReady-made ML models and tools
PandasLoading and cleaning tables of data
NumPyFast 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?

Learn Machine Learning with a teacher

Join the AI & Prompt Engineering course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp