Sets


15 min.   |   Beginner   |   (Hammack 2013)

Set

A set is a collection of unique objects. The objects inside of a set are called elements.

CautionSet: Example 1

The set \mathcal{C} (denoted by the Latin letter C) consists of RGB colors; its elements are red, green, and blue.

\mathcal{C} = \{\text{Red}, \text{Green}, \text{Blue}\} \tag{1}

CautionSet: Example 2

The set \Omega (denoted by the Greek letter Omega) is composed of a sequence of numbers that starts at 1 and increases by 1; its elements are 1, 2, 3, and so on.

\Omega = \{1,2,3, \ldots \} \tag{2}

Which of the following is not a set?

The set \mathcal{A} is not a set because it contains the element 3 twice. In set theory, sets cannot have duplicate elements; each element must be unique.

The LaTeX command \mathcal{} is used to denote a set. For Greek letters, such as the set \Omega (2), you can use the corresponding LaTeX command for that letter, in this case it is \Omega. A full list of Greek letters and other math symbols can be found in Overleaf’s LaTeX documentation.

In Python, you can use the set() function to create a set from a data structure.

Cardinality of a Set

The number of elements in a set is called the cardinality. It is denoted by enclosing a set with \lvert \rvert bars.

CautionCardinality of a Set: Example 1

The set \mathcal{C} (1) has a cardinality of 3:

\lvert \mathcal{C} \rvert = 3;

this set is said to be finite since it contains a finite number of elements.

CautionCardinality of a Set: Example 2

The set \Omega (2) has a cardinality of \infty:

|\Omega| = \infty;

this set is said to be infinite since it does not contain a finite number of elements.

What is the cardinality of the following set:

\lvert \mathcal{A} \rvert = \{ \{ \} \}

The set \mathcal{A} contains one element, which is the empty set \{ \}. Therefore, the cardinality of the set \mathcal{A} is 1.

The LaTeX commands \lvert and \rvert are used to denote the cardinality of a set, \infty for the infinity symbol \infty, and \ldots for the ellipsis \ldots, often used to indicate an infinite set.

In Python, you can use the len() function to get the cardinality of a set.

Equal Sets

Two sets are said to be equal if they share exactly the same elements.

CautionEqual Sets: Example

Suppose the following sets \mathcal{A}, \mathcal{B}, and \mathcal{C}:

\mathcal{A} = \{2,4,6\}, \ \ \mathcal{B} = \{4,2,6\}, \ \ \mathcal{C} = \{4,2,7\} \tag{3}

The sets \mathcal{A} and \mathcal{B} are said to be equal because they share the same elements (2, 4, and 6):

\mathcal{A} = \mathcal{B}

but the sets \mathcal{A} and \mathcal{C} are not equal because they have at least one element that is different (6 and 7):

\mathcal{A} \neq \mathcal{C}

The following sets are equal:

\{ \ldots -2, -1, 0, 1, 2, \ldots \} = \{ 0, -1, 1, -2, 2, \ldots \}

The two sets are equal because they contain the same elements. The first set lists all integers, while the second set rearranges them. Therefore, the statement is true.

The LaTeX command \neq is used to denote not equal \neq.

In Python, you can check whether two sets are the same by using the == conditional operator.

Elements of a Set

To express that an element exists or is in a set we denote by using \in.

CautionElements of a Set: Example

To express that 2 is an element of the previously mentioned set \mathcal{A} (3), we denote:

2 \in \mathcal{A}

meaning that the element 2 exists, or is, in the set \mathcal{A}. Conversely,

5 \notin \mathcal{A}

as 5 is not an element of the set \mathcal{A}.

Does the following element belong to the set:

\{\} \in \{1, 2, 3, \ldots\}

The correct answer is False because the empty set \{\} is not one of the elements in the set \{1, 2, 3, \ldots\}. The elements of the set are the natural numbers starting from 1, and the empty set is not included among them.

The LaTeX command \in is used to denote whether an element exists in a set \in.

In Python, you can whether an element belongs to a set by using the in conditional operator.

Special Sets

Some sets are so significant that we reserve special symbols for them.

CautionSpecial Sets: Example

The empty set \emptyset or \{\} is the set that contains no elements at all.

\emptyset = \{\}

The set of natural numbers \mathbb{N} is the set of positive counting numbers starting from 1 and increasing without bound.

\mathbb{N} = \{1, 2, 3, \ldots \}

The set of integers \mathbb{Z} is the set of whole numbers including zero, positive numbers, and their negatives.

\mathbb{Z} = \{\ldots, -2, -1, 0, 1, 2, \ldots \}

The set of real numbers \mathbb{R} is the set of all numbers that can represent a point on the continuous number line, including both rational and irrational numbers.

\mathbb{R} = \{\ldots, -1.1,\ldots,0,\ldots,1,\ldots, 1.1, \ldots \}

Determine whether \pi is an element of the set of real numbers \mathbb{R}:

\pi \in \mathbb{R}

The expression \pi \in \mathbb{R} is True given that \pi (approximately 3.14159) is a real number. The set of real numbers, denoted by \mathbb{R}, includes all rational and irrational numbers, and since \pi is an irrational number, it belongs to the set of real numbers.

The LaTeX commands \emptyset and \{\} are used to denote the empty set \emptyset and \{\} (correspondingly), and \mathbb{} “blackboard bold” for the special sets such as \mathbb{N}.

In Python, natural numbers can be represented with range(1, n), integers with a negative lower bound range(-n, n+1) for any upper limit n, and real numbers can be approximated using floating-point values, such as with numpy.linspace().

Sets: Exercises

Tip Sets: Exercise 1

Suppose the following sets:

\mathcal{A} = \{\text{Python}, \text{SQL}, \text{R}\}; \ \mathcal{B} = \{\text{SQL}, \text{Python}, \text{R}\}

Is \mathcal{A} = \mathcal{B}?

Tip Sets: Exercise 2

Suppose the following sets:

\mathcal{A} = \{\text{Python}, \text{SQL}, \text{Python}\}; \ \mathcal{B} = \{\text{SQL}, \text{Python}\}

Is \mathcal{A} = \mathcal{B}?

Tip Sets: Exercise 3

You have data containing customer IDs \mathcal{C}:

\mathcal{C} = \{ 101, 105, 107, 110, 110, 112 \}

What is \lvert \mathcal{C} \rvert?

Tip Sets: Exercise 4

For the set of machine learning tasks \mathcal{T}_{\text{DATS} \ 6103} (covered in DATS 6103: Introduction to Data Mining):

\mathcal{T}_{\text{DATS} \ 6103} = \{ \text{regression}, \text{classification}, \text{clustering} \}

Is \text{forecasting} \in \mathcal{T}_{\text{DATS} \ 6103}?

Tip Sets: Exercise 5

For the following set of labels \mathcal{Y}, representing predicted classes in the CIFAR dataset for image classification task. Is \text{flower} \in \mathcal{Y}?

Note: \lvert \mathcal{Y} \rvert = 10