Grade by Grade
D. Topics in Computer Science · Topics in Computer Science: Sustainability, Emerging Research and Careers

Artificial Intelligence, Machine Learning and Data Mining

D2.1About 75 minutes
Learning goals
  • I can explain what artificial intelligence and machine learning are and how machine learning differs from traditional programming.
  • I can describe training data, features, labels and a model, and run a nearest-neighbour classifier in Python.
  • I can explain what data mining is and give examples.
  • I can discuss research directions and ethical issues in AI, including bias, privacy and energy use.

Words to know

artificial intelligence (AI)
The field of computer science that builds systems able to do tasks that normally need human intelligence, such as recognizing images, understanding language or making decisions.
machine learning
A kind of AI in which a computer improves at a task by finding patterns in examples (data) rather than following rules written by a person.
training data
The examples a machine learning system learns from.
feature / label
A feature is a measured input (such as temperature); a label is the correct answer for an example (such as "open").
model
The pattern or rules a machine learning system has learned, used to make predictions on new data.
neural network
A machine learning model made of many connected layers of simple calculating units, loosely inspired by neurons in the brain.
data mining
Searching large data sets for useful patterns, such as items often bought together.
bias (in AI)
Systematic unfairness in a model's results, often because the training data did not fairly represent everyone.

Who taught your phone to recognize your face?

No programmer wrote a rule like "if the nose is 3.2 cm long, it's you". Instead, the system was trained on huge numbers of examples, and it found the patterns itself. That is machine learning, the part of artificial intelligence (AI) behind face unlock, spam filters, recommendations, translation apps and chatbots.

What is AI?

Artificial intelligence is the field that builds computer systems able to do tasks we normally think need human intelligence: seeing, understanding language, planning, deciding. Early AI systems were mostly rule-based: experts wrote long lists of if-then rules. These worked in narrow areas but broke down in messy real-world situations.

Machine learning flips the approach. Instead of writing the rules, we give the computer training data: many examples with the right answers. It builds a model that captures the pattern, then uses the model to make predictions about new examples it has never seen.

Traditional programmingMachine learningrulesdataprogramanswersdataanswerslearnrules(model)A person writes the rules.The computer finds the rules from examples.
In traditional programming a person writes the rules. In machine learning the computer is given examples (data with the right answers) and builds the rules, called a model, itself.

Features, labels and a simple learner

Suppose a town wants to predict whether its outdoor rink will be open on a given day. Each past day is an example with two features (the average temperature that week, and how many cold days in a row there have been) and a label (open or closed).

One of the simplest learners is k-nearest neighbours (k-NN). To classify a new day:

  1. Measure the distance from the new day to every training example (using the distance formula from math class: √((x₂ − x₁)² + (y₂ − y₁)²)).
  2. Find the k closest examples.
  3. Let them vote: the most common label wins.
-16-12-8-4040246810Average temperature this week (°C)Cold days in a rowopenclosednew day:−7 °C, 3 days
Training data for the rink model. The dashed circle around the new day (triangle) contains its 3 nearest neighbours: two open and one closed, so the model predicts open. On this graph 1 °C and 1 day are drawn the same length, so the circle shows true distance.
Worked example

Example 1: Classify a new day by hand

Use 3-NN with the rink data to classify a day with temperature −7 °C and 3 cold days in a row.

  1. Distance to (−8, 4, open): √(1² + 1²) = √2 ≈ 1.41.
  2. Distance to (−6, 5, open): √(1² + 2²) = √5 ≈ 2.24.
  3. Distance to (−3, 1, closed): √(4² + 2²) = √20 ≈ 4.47.
  4. Distance to (−12, 6, open): √(5² + 3²) = √34 ≈ 5.83. All other points are farther (7.00 or more).
  5. The 3 nearest are open, open, closed. Open wins 2 to 1.

The model predicts the rink will be open.

import math
data = [(-12, 6, "open"), (-8, 4, "open"), (-15, 10, "open"), (-6, 5, "open"),
(2, 0, "closed"), (-3, 1, "closed"), (0, 3, "closed"), (1, 5, "closed")]
def predict(temp, days, k):
dists = []
for t, d, label in data:
dist = math.sqrt((t - temp) ** 2 + (d - days) ** 2)
dists.append((dist, label))
dists.sort()
nearest = [label for dist, label in dists[:k]]
if nearest.count("open") > k / 2:
return "open"
return "closed"
print(predict(-7, 3, 3))
print(predict(-2, 4, 3))

A 3-nearest-neighbour classifier. The training data are invented for teaching. Output (one line each): open / closed

Neural networks and deep learning

Modern AI mostly uses neural networks: layers of simple calculating units connected by adjustable numbers called weights. Training changes the weights a tiny bit at a time to reduce the errors on the training data. Networks with many layers (deep learning) learn to recognize images, speech and text. Large language models, the systems behind AI chatbots, are trained on enormous amounts of text to predict what words come next, which lets them generate fluent writing. They can also produce confident but wrong answers, so their output must be checked.

Canada has played a major role in this research. Geoffrey Hinton, a long-time professor at the University of Toronto, shared the 2024 Nobel Prize in Physics with John Hopfield for foundational work on neural networks. Yoshua Bengio of the Université de Montréal shared the 2018 Turing Award (often called the Nobel Prize of computing) with Hinton and Yann LeCun. Canada's national AI strategy, launched in 2017, supports three AI institutes: Amii in Edmonton, Mila in Montréal and the Vector Institute in Toronto.

Data mining

Data mining means searching large collections of data for useful patterns. Examples:

  • A grocery chain finds products often bought together and places them nearby.
  • A bank flags unusual card purchases that might be fraud.
  • Health researchers look for patterns linking symptoms and outcomes in anonymized records.

The program below finds the pair of items that appears together in the most shopping baskets.

baskets = [["bread", "milk", "eggs"], ["bread", "butter"], ["milk", "eggs"],
["bread", "milk", "eggs", "butter"], ["milk", "eggs", "maple syrup"]]
counts = {}
for basket in baskets:
items = sorted(basket)
for i in range(len(items)):
for j in range(i + 1, len(items)):
pair = items[i] + " + " + items[j]
counts[pair] = counts.get(pair, 0) + 1
best = max(counts, key=counts.get)
print("Most common pair:", best, "in", counts[best], "of", len(baskets), "baskets")

A tiny piece of data mining: finding which items are bought together. A store has millions of baskets, not five. Output: Most common pair: eggs + milk in 4 of 5 baskets

Research questions and ethics

AI is one of the most active research areas in computer science. Some of the open questions:

  • Bias: if the training data under-represents some groups, the model can work worse for them. For example, a face-recognition system trained mostly on one group of people may make more mistakes for others.
  • Privacy: data mining can reveal more about people than they meant to share.
  • Explainability: why did the model make that decision? This matters for loans, hiring and health care.
  • Reliability: how do we know when a model is wrong?
  • Energy: training and running large models uses a lot of electricity in data centres (remember Lesson 1).

Researchers, governments and companies are developing laws, standards and testing methods to make AI safer and fairer.

Just for fun

I asked an AI model to tell me a joke about neural networks.

It said it was still in training, but it would get back to me after a few more epochs.

Hands-on activity

Be the learner

You need: paper, a pencil, a ruler, and a computer with Python.

  1. Collect your own small data set: for 10 objects at home (fruits, books, shoes...), measure two features, such as length and mass (a kitchen scale) or length and width, and give each a label (for example "apple" or "orange").
  2. Plot the points on graph paper, using a different shape for each label, as in the rink diagram.
  3. Pick a new object, measure it, and classify it by hand with 3-NN.
  4. Change the program's data list to your data and check your answer.
  5. Try k = 1 and k = 5. Does the answer change? Write one sentence about why.
  6. Write one way your data set is biased or too small (for example, all the apples came from one bag).

What to look for: a correct plot, correct distance calculations for the nearest points, and a thoughtful comment on how the size and variety of training data affects the model.

Your turn

1

In your own words, how does machine learning differ from traditional programming?

2

In the rink example, name the two features and the label.

3

Find the distance between (−8, 4) and (−5, 0).

4

What does the k in k-NN mean?

5

Give two everyday uses of machine learning.

6

What is data mining? Give an example.

7

Why can a model trained on biased data be unfair?

More practice: warm-up

1

What does AI stand for?

2

What is training data?

3

What is a model?

4

Which Canadian researcher shared the 2024 Nobel Prize in Physics for work on neural networks?

5

Name one of Canada's three national AI institutes.

More practice: on your own

1

Use 3-NN with the rink data to classify a day with −2 °C and 4 cold days. Show the three nearest distances.

2

Using 1-NN, classify (−7, 3). Is it the same as with 3-NN?

3

Why is k usually chosen to be odd when there are two labels?

4

A chatbot gives you a confident answer with a statistic. What should you do before using it in a report?

5

In the basket program, how many baskets contain both bread and milk? Check by reading the data.

Challenge

1

In the rink data, temperature ranges over about 17 degrees and cold days over about 10. Explain why this might make temperature matter more than cold days in the distance, and suggest a fix.

2

Modify predict so it also returns how many of the k neighbours agreed (for example ("open", 2)). Write the changed lines.

3

A hiring company wants to use a model trained on its past hiring decisions to screen applicants. Identify one serious risk and one safeguard.

Check your understanding

Quiz

1.

In the kind of machine learning used in this lesson, what does the computer receive to build its model?

2.

In k-NN with k = 3, the three nearest neighbours are labelled A, B, A. What is the prediction?

3.

What is the distance between (1, 2) and (4, 6)?

4.

Which is an example of data mining?

5.

Why might a face-recognition model make more errors for some people?

6.

What do large language models (behind AI chatbots) learn to do?

For parents and teachers

Big idea: Expectation D2.1 names artificial intelligence and data mining as emerging research areas. Students learn the core idea of machine learning (learning from examples), run a small classifier, and discuss ethical issues.

How to help: Ask your teen to explain the difference between writing rules and learning from examples. Talk about the AI tools your family uses (recommendations, voice assistants, chatbots): what data do they learn from, and when might they be wrong? Encourage checking chatbot answers against reliable sources.

Common difficulties:

  • Arithmetic with negative numbers in the distance formula: (−8) − (−7) = −1, and (−1)² = 1.
  • Thinking AI "understands" like a person. Models find statistical patterns.
  • Forgetting that more and fairer data usually matters more than a cleverer algorithm.