Velamuvi AIAcademy

Lesson 11 of 12 · Reading Maths

Sets and Intervals

Learn set notation (∈, ⊆, ∪, ∩, ∅), the number sets ℕ, ℤ and ℝ, and interval notation with square and round brackets, with Python sets and range checks for every idea.

  • 7 min read
  • Beginner
  • Reviewed
In this lesson you will learn
  • What a set is and how to read set notation
  • Union, intersection and subsets
  • The number sets ℕ, ℤ and ℝ
  • How to read intervals such as [0, 1] and (0, 1)
  • How each idea looks in Python

Groups of things

Python has sets built in, and they behave exactly like maths sets:

Try it yourselfPython
A = {1, 2, 3, 3, 2}
print(A)                 # repeats disappear

print(2 in A)            # 2 ∈ A
print(7 not in A)        # 7 ∉ A
print(len(A))            # how many elements

Combining sets

Try it yourselfPython
python_students = {"Ada", "Ravi", "Mei", "Sara"}
sql_students = {"Ravi", "Sara", "Tom"}

print("Union:       ", sorted(python_students | sql_students))
print("Intersection:", sorted(python_students & sql_students))
print("Difference:  ", sorted(python_students - sql_students))
print("Empty set:   ", set())

sorted() is used only to print the names in a fixed order, because sets have no order of their own.

Subsets

Try it yourselfPython
labels_in_test = {"cat", "dog"}
all_labels = {"cat", "dog", "bird"}

print(labels_in_test <= all_labels)      # labels_in_test ⊆ all_labels
print(all_labels <= labels_in_test)

This is a real check in machine learning: every label in your test data should be one the model was trained on.

The number sets

Some sets are so important they have their own double-struck letters:

You will often see these in AI papers in phrases like x ∈ ℝ, meaning "x is a real number", or x ∈ ℝⁿ, meaning "x is a list of n real numbers". So "an input x ∈ ℝ⁷⁸⁴" means the input is a list of 784 real numbers, such as the pixels of a 28 × 28 image.

Intervals

NotationMeaningIn Python
[0, 1]from 0 to 1, including both ends0 <= x <= 1
(0, 1)between 0 and 1, not including either end0 < x < 1
[0, 1)including 0, but not 10 <= x < 1
(0, ∞)every number above 0 (∞ means infinity)x > 0
Try it yourselfPython
def in_closed(x, low, high):      # [low, high]
    return low <= x <= high


def in_open(x, low, high):        # (low, high)
    return low < x < high


for x in [0, 0.5, 1]:
    print(f"x = {x}: in [0, 1]? {in_closed(x, 0, 1)}   in (0, 1)? {in_open(x, 0, 1)}")

Intervals tell you the allowed values of things in AI: a probability is in [0, 1], the output of sigmoid is in (0, 1) because it never quite reaches 0 or 1, and a learning rate is in (0, ∞).

Common mistakes

Practice exercise

Use set operations to catch a data leak, and interval checks to catch impossible probabilities.

Your solutionPython
train_ids = {101, 102, 103, 104, 105}
test_ids = {104, 105, 106, 107}

# 1. Print the ids that appear in BOTH sets, sorted (these are a data leak!).
# 2. Print how many distinct ids there are in total.
# 3. For each probability below, print whether it is in the interval [0, 1].
probabilities = [0.3, 1.2, 0, -0.1]

# Output must be exactly:
# Leaked ids: [104, 105]
# Total distinct ids: 7
# 0.3 True
# 1.2 False
# 0 True
# -0.1 False
Knowledge check

Which numbers belong to the interval (2, 5]?

Summary

  • A set holds distinct elements. x ∈ A means x is in A.
  • Union ∪ is either, intersection ∩ is both, and ∅ is the empty set. In Python: |, & and set().
  • A ⊆ B means every element of A is also in B.
  • ℕ, ℤ and ℝ are the natural numbers, integers and real numbers. x ∈ ℝⁿ means a list of n real numbers.
  • In an interval, square brackets include the end point and round brackets leave it out.

Next: putting it all into practice with maths in Python.

Finished reading? Tick it off to track your progress.