Bird
Raised Fist0
NumPydata~10 mins

Why set operations matter in NumPy - Test Your Understanding

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a NumPy array from a Python list.

NumPy
import numpy as np
arr = np.[1]([1, 2, 3, 4])
print(arr)
Drag options to blanks, or click blank then click option'
Aset
Blist
Carray
Dmatrix
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'list' instead of 'array' causes an error.
Using 'set' creates a Python set, not a NumPy array.
2fill in blank
medium

Complete the code to find unique elements in a NumPy array.

NumPy
import numpy as np
arr = np.array([1, 2, 2, 3, 4, 4, 5])
unique_elements = np.[1](arr)
print(unique_elements)
Drag options to blanks, or click blank then click option'
Aset
Bunique
Cdistinct
Dsort
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set' returns a Python set, not a NumPy array.
Using 'sort' only sorts but does not remove duplicates.
3fill in blank
hard

Fix the error in the code to find the intersection of two NumPy arrays.

NumPy
import numpy as np
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([3, 4, 5, 6])
common = np.[1](arr1, arr2)
print(common)
Drag options to blanks, or click blank then click option'
Aintersect1d
Bintersect
Cintersection1d
Dintersect_1d
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'intersect' causes an AttributeError.
Using 'intersection1d' is a common misspelling and causes an error.
4fill in blank
hard

Fill both blanks to create a dictionary of unique elements and their counts from a NumPy array.

NumPy
import numpy as np
arr = np.array([1, 2, 2, 3, 3, 3])
unique, counts = np.[1](arr, return_counts=True)
result = dict(zip(unique, [2]))
print(result)
Drag options to blanks, or click blank then click option'
Aunique
Bcounts
Ccount
Dfrequency
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'count' instead of 'counts' causes a NameError.
Not using return_counts=True in np.unique causes unpacking error.
5fill in blank
hard

Fill all three blanks to filter unique elements greater than 2 and create a dictionary with their counts.

NumPy
import numpy as np
arr = np.array([1, 2, 2, 3, 3, 3, 4])
unique, counts = np.unique(arr, return_counts=True)
filtered = unique[unique [1] 2]
filtered_counts = counts[unique [2] 2]
result = dict(zip(filtered, [3]))
print(result)
Drag options to blanks, or click blank then click option'
A>
Bcounts
C<
Dfiltered_counts
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' filters wrong elements.
Using 'counts' instead of 'filtered_counts' causes mismatch.

Practice

(1/5)
1. What is the main purpose of using set operations in NumPy arrays?
easy
A. To multiply elements of arrays
B. To sort arrays in ascending order
C. To reshape arrays into different dimensions
D. To find common or unique elements between arrays

Solution

  1. Step 1: Understand set operations

    Set operations are used to compare arrays to find common or unique elements.
  2. Step 2: Identify the main purpose

    Sorting, multiplication, and reshaping are different array operations, not set operations.
  3. Final Answer:

    To find common or unique elements between arrays -> Option D
  4. Quick Check:

    Set operations = find common/unique elements [OK]
Hint: Set operations = compare arrays for common or unique items [OK]
Common Mistakes:
  • Confusing set operations with sorting or reshaping
  • Thinking set operations multiply elements
  • Assuming set operations change array shape
2. Which NumPy function is used to find the intersection of two arrays?
easy
A. np.intersect1d()
B. np.concatenate()
C. np.setdiff1d()
D. np.union1d()

Solution

  1. Step 1: Recall function names for set operations

    np.intersect1d() finds common elements between arrays.
  2. Step 2: Differentiate from other functions

    np.union1d() finds all unique elements combined, np.setdiff1d() finds differences, np.concatenate() joins arrays without set logic.
  3. Final Answer:

    np.intersect1d() -> Option A
  4. Quick Check:

    Intersection = np.intersect1d() [OK]
Hint: Intersection means common elements, use np.intersect1d() [OK]
Common Mistakes:
  • Using np.union1d() for intersection
  • Confusing set difference with intersection
  • Using np.concatenate() which just joins arrays
3. What is the output of the following code?
import numpy as np
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([3, 4, 5, 6])
result = np.setdiff1d(arr1, arr2)
print(result)
medium
A. [1 2]
B. [3 4]
C. [5 6]
D. [1 2 3 4 5 6]

Solution

  1. Step 1: Understand np.setdiff1d()

    This function returns elements in the first array not in the second.
  2. Step 2: Apply to given arrays

    Elements in arr1 but not in arr2 are 1 and 2.
  3. Final Answer:

    [1 2] -> Option A
  4. Quick Check:

    Set difference arr1 - arr2 = [1 2] [OK]
Hint: Set difference = items in first array not in second [OK]
Common Mistakes:
  • Confusing set difference with intersection
  • Expecting union instead of difference
  • Misreading which array is first
4. The following code throws an error. What is the problem?
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = [2, 3, 4]
result = np.intersect1d(arr1, arr2)
print(result)
medium
A. arr2 is not a NumPy array
B. There is no error; code runs fine
C. np.intersect1d() cannot handle integers
D. np.intersect1d() requires both inputs to be lists

Solution

  1. Step 1: Check input types for np.intersect1d()

    np.intersect1d() accepts array-like inputs, including lists.
  2. Step 2: Verify code behavior

    arr2 is a list, which is valid input; code runs without error and outputs common elements.
  3. Final Answer:

    There is no error; code runs fine -> Option B
  4. Quick Check:

    np.intersect1d() accepts lists and arrays [OK]
Hint: np.intersect1d() accepts lists or arrays as input [OK]
Common Mistakes:
  • Assuming inputs must be NumPy arrays
  • Thinking np.intersect1d() only works with arrays
  • Expecting error due to mixed input types
5. You have two arrays:
arr1 = np.array([1, 2, 2, 3, 4])
arr2 = np.array([2, 3, 5])

How can you find all unique elements that appear in either array but not in both?
hard
A. Use np.intersect1d(arr1, arr2)
B. Use np.union1d(arr1, arr2)
C. Use np.setxor1d(arr1, arr2)
D. Use np.setdiff1d(arr1, arr2)

Solution

  1. Step 1: Understand the problem

    We want elements unique to each array, not shared by both.
  2. Step 2: Identify correct function

    np.setxor1d() returns elements in either array but not in both (exclusive or).
  3. Final Answer:

    Use np.setxor1d(arr1, arr2) -> Option C
  4. Quick Check:

    Unique elements in either array = np.setxor1d() [OK]
Hint: Exclusive elements = np.setxor1d() finds unique non-shared items [OK]
Common Mistakes:
  • Using union instead of exclusive or
  • Using intersection which finds common elements
  • Using set difference which is one-sided