Sets and Membership

A set is a collection of distinct objects.

For example:

A={1,2,3}A = \{1, 2, 3\}

This says that AA is the set containing 1, 2, and 3.

The order does not matter in a set:

{1,2,3}={3,2,1}\{1, 2, 3\} = \{3, 2, 1\}

That one fact prevents many mistakes later.

Membership

The symbol \in means "is in".

So

2A2 \in A

means:

2 is in the set A

The symbol \notin means "is not in".

5A5 \notin A

means:

5 is not in the set A
MATH-C01-T04-001Exercise: Read membership

Let A={1,2,3}A = \{1, 2, 3\}.

Enter 1 if 2A2 \in A is true.

Compute it first, then check your number.

HintRead the symbol

The symbol \in means "is in".

SolutionMembership

Enter 1. The value 2 is listed in A={1,2,3}A = \{1, 2, 3\}, so 2A2 \in A is true.

Why Sets Matter in ML

Sets let us name collections clearly.

For example:

  • a set of training examples
  • a set of labels
  • a set of possible tokens
  • a set of model parameters

If D\mathcal{D} is a dataset, then

(x,y)D(x, y) \in \mathcal{D}

can be read as:

the input-label pair (x,y)(x, y) is one item in the dataset D\mathcal{D}.

This is useful because ML often talks about one example selected from a larger collection.

MATH-C01-T04-002Exercise: Dataset membership

Let D\mathcal{D} be a dataset with 100 examples.

Enter 1 if (x,y)D(x, y) \in \mathcal{D} means the pair (x,y)(x, y) is one item in the dataset.

Compute it first, then check your number.

HintName the collection

D\mathcal{D} names the dataset. The pair is being checked for membership in that dataset.

SolutionDataset reading

Enter 1. The statement says the input-label pair (x,y)(x, y) belongs to the dataset D\mathcal{D}.

Set Versus Ordered Collection

A set does not care about order.

The set

{cat,dog}\{\text{cat}, \text{dog}\}

is the same set as:

{dog,cat}\{\text{dog}, \text{cat}\}

But the tuple

(cat,dog)(\text{cat}, \text{dog})

is not the same tuple as:

(dog,cat)(\text{dog}, \text{cat})

When order matters, use tuples, vectors, lists, or sequences rather than plain sets.

MATH-C01-T04-003Exercise: Order check

Enter 1 if {1,2,3}\{1, 2, 3\} and {3,2,1}\{3, 2, 1\} are the same set.

Compute it first, then check your number.

HintSet rule

Ask whether the same elements are present.

SolutionSame elements

Enter 1. Both sets contain exactly the elements 1, 2, and 3. The order changed, but ordinary set membership did not.

Common Doubt

Can a set contain repeated values?

In ordinary set notation, repeated values collapse. The set {1,1,2}\{1, 1, 2\} is the same as {1,2}\{1, 2\}. If repeats matter, we need another structure, such as a list, tuple, multiset, or sequence.

MATH-C01-T04-004Exercise: Repeated values

Enter 1 if {1,1,2}\{1, 1, 2\} and {1,2}\{1, 2\} represent the same ordinary set.

Compute it first, then check your number.

HintDistinct elements

List the distinct values only.

SolutionCollapsed duplicates

Enter 1. Both sets contain the distinct values 1 and 2. Repeating an element does not create a new element in an ordinary set.

Next, we look at ordered collections: tuples, indices, and coordinates.