Sets and Membership
A set is a collection of distinct objects.
For example:
This says that is the set containing 1, 2, and 3.
The order does not matter in a set:
That one fact prevents many mistakes later.
Membership
The symbol means "is in".
So
means:
2 is in the set A
The symbol means "is not in".
means:
5 is not in the set A
Let .
Enter 1 if is true.
Compute it first, then check your number.
HintRead the symbol
The symbol means "is in".
SolutionMembership
Enter 1. The value 2 is listed in , so
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 is a dataset, then
can be read as:
the input-label pair is one item in the dataset .
This is useful because ML often talks about one example selected from a larger collection.
Let be a dataset with 100 examples.
Enter 1 if means the pair is one item
in the dataset.
Compute it first, then check your number.
HintName the collection
names the dataset. The pair is being checked for membership in that dataset.
SolutionDataset reading
Enter 1. The statement says the input-label pair belongs to
the dataset .
Set Versus Ordered Collection
A set does not care about order.
The set
is the same set as:
But the tuple
is not the same tuple as:
When order matters, use tuples, vectors, lists, or sequences rather than plain sets.
Enter 1 if and 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 is the same as . If repeats matter, we need another structure, such as a list, tuple, multiset, or sequence.
Enter 1 if and 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.