Skip to content
Learn Netverks

Lesson

Step 17/36 47% through track

set-operations

Set operations on arrays

Last reviewed Jun 1, 2026 Content v20260601
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Set operations on arrays: NumPy ndarray operations, vectorization, and numerical patterns used across the Python scientific stack.

Teams apply Set operations on arrays in every serious NumPy project—skipping it leaves blind spots in analysis and reviews.

You will apply Set operations on arrays in contexts like: Notebooks, feature engineering pipelines, and custom numerical code.

Read the narrative, run `import numpy as np` snippets in the playground (install NumPy with pip if the runner lacks it), tweak shapes and dtypes, and complete MCQs.

When you can explain the previous lesson's ideas in your own words.

NumPy provides set-like operations on 1D arrays: unique, intersect1d, union1d, in1d (membership), and setdiff1d.

unique

import numpy as np
a = np.array([3, 1, 2, 3, 1])
print(np.unique(a))
vals, counts = np.unique(a, return_counts=True)
print(vals, counts)

Membership and intersection

  • np.isin(a, test) — boolean membership
  • np.intersect1d(a, b) — sorted common elements
  • np.setdiff1d(a, b) — in a but not b

Use cases

Find label categories, detect duplicate IDs, align categorical features before encoding in ML pipelines.

Important interview questions and answers

  1. Q: unique sorted?
    A: Returns sorted unique values by default.
  2. Q: isin vs Python in?
    A: Vectorized membership for entire array at once.

Self-check

  1. Count occurrences per unique value.
  2. Find elements in A that are also in B.

Tip: unique(..., return_counts=True) replaces Counter for arrays.

Interview prep

unique?

Sorted distinct elements; return_counts gives frequencies.

isin?

Vectorized membership test.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • np.unique cost?
  • isin performance?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump