Review test: artificial intelligence
The test covers the whole course: what AI is and how it came about, how a machine learns, how AI is programmed, how it works on devices and what its ethical questions are. It has two parts: a knowledge test with 20 questions and two practical tasks in Python.
What you need to know
- the difference between an ordinary program and machine learning; what a model is and what training and test data are
- important moments in the history of AI and what the Turing test checks
- types of AI (narrow and general, generative) and how chatbots work (predicting the next word)
- how neural networks learn and why data can be biased
- the k-nearest neighbors algorithm, the perceptron and the decision tree
- why small models (TinyML) are used on the ESP32 and micro:bit
- consciousness, ethics, fake content, privacy and the future of jobs
What the test looks like
- Duration: two school lessons – the first for the knowledge test, the second for the practical tasks (or at home, as practice).
- Knowledge test: 20 multiple-choice questions, 2 points each.
- Practical tasks: 2 tasks, 10 points each. For full points the program must work and be clear, with comments.
| Points (out of 60) | 0–23 | 24–32 | 33–42 | 43–51 | 52–60 |
|---|---|---|---|---|---|
| Grade | 1 | 2 | 3 | 4 | 5 |
The scale is a suggestion for self-assessment; the teacher can adjust it. Students who did not do module 3 can do only the knowledge test (the scale is then halved: 40 points).
Knowledge test (20 questions)
For each question, choose one answer and click “Check”. Each correct answer is worth 2 points.
Open the test
Knowledge test: artificial intelligence (20 questions)
1. How is machine learning different from an ordinary program?
2. Who asked the question “Can machines think?” in 1950?
3. What does the Turing test check?
4. What is an “AI winter”?
5. Which example is narrow (specialized) AI?
6. What is a model in machine learning?
7. Why is the data split into a training part and a test part?
8. What happens if the training data has almost no examples of one group?
9. What does one artificial neuron do?
10. What changes while a neural network learns?
11. How does a chatbot create an answer?
12. Why does a chatbot sometimes “make things up”?
13. How does the k-NN algorithm decide?
14. What can a perceptron with one neuron NOT learn?
15. Why is a decision tree often chosen for the ESP32?
16. What is TinyML?
17. What is a deepfake?
18. Which of these should you not write to a chatbot?
19. Does a chatbot writing “I am happy” prove that it feels?
20. Who is responsible when an AI system makes a mistake?
Practice tasks
Scoring: a working program 6 points, a correct answer to the question with the task 2 points, clear code with comments 2 points.
- k nearest neighbors (without libraries). The following lists are given:
Each example is (hours of study per week, hours of sleep per night) and whether the student passed the test. Write a functiontrain = [ ((2, 6), "no"), ((1, 7), "no"), ((3, 5), "no"), ((2, 8), "no"), ((4, 6), "no"), ((6, 8), "yes"), ((7, 7), "yes"), ((5, 9), "yes"), ((8, 8), "yes"), ((6, 7), "yes"), ] test = [((3, 7), "no"), ((7, 9), "yes"), ((5, 6), "yes"), ((1, 9), "no")]predict(point, k=3)that returns the majority answer of theknearest examples, and calculate the accuracy on the listtest. Question: on which example does the model make a mistake, and why? - A decision tree for the classroom. From 13 measurements (temperature, humidity → cold, pleasant, warm, stuffy), train a decision tree with a depth of at most 3 and print its rules. Then:
- predict the state for the measurements (19.5 °C, 50%), (22 °C, 76%) and (26 °C, 40%);
- rewrite the trained tree as an ordinary function
state(temperature, humidity)withif–else(those who did module 4 can also write it as a C++ function for the ESP32).
X = [[17.5, 50], [18.2, 45], [19.0, 60], [20.5, 48], [21.0, 52], [22.3, 47], [23.0, 55], [21.8, 74], [22.5, 78], [20.9, 80], [25.2, 50], [26.8, 45], [27.5, 60]] y = ["cold", "cold", "cold", "pleasant", "pleasant", "pleasant", "pleasant", "stuffy", "stuffy", "stuffy", "warm", "warm", "warm"]
Solution 1: k nearest neighbors
# Review test, task 1: k-NN without libraries
# Data: (hours of study per week, hours of sleep per night) -> whether the student passed the test
import math
train = [
((2, 6), "no"), ((1, 7), "no"), ((3, 5), "no"),
((2, 8), "no"), ((4, 6), "no"),
((6, 8), "yes"), ((7, 7), "yes"), ((5, 9), "yes"),
((8, 8), "yes"), ((6, 7), "yes"),
]
test = [((3, 7), "no"), ((7, 9), "yes"),
((5, 6), "yes"), ((1, 9), "no")]
def distance(a, b):
"""Euclidean distance between two points."""
return math.sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2)
def predict(point, k=3):
"""Return the answer that the majority of the k nearest examples have."""
nearest = sorted(train, key=lambda p: distance(point, p[0]))[:k]
votes = {}
for _, answer in nearest:
votes[answer] = votes.get(answer, 0) + 1
return max(votes, key=votes.get)
correct = 0
for point, actual in test:
p = predict(point)
print(point, "predicted:", p, "| actual:", actual)
if p == actual:
correct += 1
print("Accuracy:", correct, "of", len(test))
Program output:
(3, 7) predicted: no | actual: no
(7, 9) predicted: yes | actual: yes
(5, 6) predicted: no | actual: yes
(1, 9) predicted: no | actual: no
Accuracy: 3 of 4
Answer to the question: the model makes a mistake for (5, 6). Among the three nearest examples, two are “no” ((4, 6) and (3, 5)) and only one is “yes” ((6, 7)), so the majority says “no”. The point lies on the border between the groups and there are few training examples – with more data or a different k the result could be different.
Solution 2: decision tree
# Review test, task 2: a decision tree for the classroom
from sklearn.tree import DecisionTreeClassifier, export_text
# (temperature in °C, humidity in %) -> state of the classroom
X = [[17.5, 50], [18.2, 45], [19.0, 60], [20.5, 48], [21.0, 52], [22.3, 47],
[23.0, 55], [21.8, 74], [22.5, 78], [20.9, 80], [25.2, 50], [26.8, 45], [27.5, 60]]
y = ["cold", "cold", "cold", "pleasant", "pleasant", "pleasant",
"pleasant", "stuffy", "stuffy", "stuffy", "warm", "warm", "warm"]
model = DecisionTreeClassifier(max_depth=3, random_state=0) # a small tree – easy to read
model.fit(X, y)
print(export_text(model, feature_names=["temperature", "humidity"]))
for measurement in [[19.5, 50], [22.0, 76], [26.0, 40]]:
print(measurement, "->", model.predict([measurement])[0])
Program output:
|--- humidity <= 67.00
| |--- temperature <= 19.75
| | |--- class: cold
| |--- temperature > 19.75
| | |--- temperature <= 24.10
| | | |--- class: pleasant
| | |--- temperature > 24.10
| | | |--- class: warm
|--- humidity > 67.00
| |--- class: stuffy
[19.5, 50] -> cold
[22.0, 76] -> stuffy
[26.0, 40] -> warm
The same tree, rewritten as a function:
# Review test, task 2b: the tree rewritten as an ordinary function (if – else)
def state(temperature, humidity):
if humidity <= 67:
if temperature <= 19.75:
return "cold"
elif temperature <= 24.1:
return "pleasant"
else:
return "warm"
else:
return "stuffy"
print(state(19.5, 50), state(22.0, 76), state(26.0, 40))
Program output:
cold stuffy warm
Version for the ESP32 (C++):
// Review test, task 2b (ESP32 version): the same rules as a C++ function
const char* state(float temperature, float humidity) {
if (humidity <= 67) {
if (temperature <= 19.75) return "cold";
else if (temperature <= 24.1) return "pleasant";
else return "warm";
}
return "stuffy";
}
Check: the function gives the same answer as the trained model on 3,250 measurements (temperatures from 15 to 30 °C, humidity from 30 to 94%).
Answer to the question: the tree has only a few comparisons, so it fits into the small memory of the ESP32 and works instantly; a person can read and check it. A large neural network has thousands of weights and needs much more memory and computation (see TinyML).
After the test
If you lost points somewhere, repeat that lesson:
- basics and history – What is artificial intelligence, History of AI, The Turing test
- learning and data – How a machine learns, Data – food for AI, Neural networks, How chatbots work
- programming – k-NN, Perceptron, A model from Python on the ESP32
- society – Is AI conscious?, Ethics and safety of AI, The future of AI
Congratulations – you have finished the course! The next step is the final project, if you haven't done it yet.
