0
0
NumPydata~20 mins

np.in1d() for membership testing in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
np.in1d() Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of np.in1d() with integer arrays
What is the output of the following code?
import numpy as np
arr1 = np.array([1, 2, 3, 4, 5])
arr2 = np.array([2, 4, 6])
result = np.in1d(arr1, arr2)
print(result)
NumPy
import numpy as np
arr1 = np.array([1, 2, 3, 4, 5])
arr2 = np.array([2, 4, 6])
result = np.in1d(arr1, arr2)
print(result)
A[ True False True False True]
B[False True False True False]
C[False False False False False]
D[ True True True True True]
Attempts:
2 left
💡 Hint
np.in1d() checks if each element of the first array is in the second array.
data_output
intermediate
2:00remaining
Count of elements in arr1 found in arr2 using np.in1d()
Given the arrays below, how many elements of arr1 are present in arr2?
import numpy as np
arr1 = np.array([10, 20, 30, 40, 50])
arr2 = np.array([15, 20, 35, 40, 55])
mask = np.in1d(arr1, arr2)
count = np.sum(mask)
print(count)
NumPy
import numpy as np
arr1 = np.array([10, 20, 30, 40, 50])
arr2 = np.array([15, 20, 35, 40, 55])
mask = np.in1d(arr1, arr2)
count = np.sum(mask)
print(count)
A1
B3
C2
D0
Attempts:
2 left
💡 Hint
Count how many True values are in the boolean mask from np.in1d().
🔧 Debug
advanced
2:00remaining
Identify the error in np.in1d() usage
What error will this code raise?
import numpy as np
arr1 = [1, 2, 3]
arr2 = 2
result = np.in1d(arr1, arr2)
print(result)
NumPy
import numpy as np
arr1 = [1, 2, 3]
arr2 = 2
result = np.in1d(arr1, arr2)
print(result)
ATypeError: 'int' object is not iterable
BValueError: operands could not be broadcast together
CNo error, outputs [False False True]
DSyntaxError: invalid syntax
Attempts:
2 left
💡 Hint
np.in1d() expects the second argument to be iterable.
🚀 Application
advanced
2:00remaining
Filter DataFrame rows using np.in1d()
You have a DataFrame of students and their grades. You want to select only students whose names are in a given list.
Which code snippet correctly filters the DataFrame?
import pandas as pd
import numpy as np

data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'], 'Grade': [85, 92, 78, 90]}
df = pd.DataFrame(data)
selected_names = ['Bob', 'David']

# Which line filters df to only rows with names in selected_names?
NumPy
import pandas as pd
import numpy as np

data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'], 'Grade': [85, 92, 78, 90]}
df = pd.DataFrame(data)
selected_names = ['Bob', 'David']
Afiltered_df = df[np.in1d(selected_names, df['Name'])]
Bfiltered_df = df[df['Name'] == selected_names]
Cfiltered_df = df[df['Name'].isin(selected_names)]
Dfiltered_df = df[np.in1d(df['Name'], selected_names)]
Attempts:
2 left
💡 Hint
np.in1d() returns a boolean array matching df['Name'] elements against selected_names.
🧠 Conceptual
expert
3:00remaining
Understanding np.in1d() behavior with repeated elements
Consider the arrays:
import numpy as np
arr1 = np.array([1, 2, 2, 3, 4, 4, 4])
arr2 = np.array([2, 4])
result = np.in1d(arr1, arr2)
print(result)

What is the shape and content of the output array?
NumPy
import numpy as np
arr1 = np.array([1, 2, 2, 3, 4, 4, 4])
arr2 = np.array([2, 4])
result = np.in1d(arr1, arr2)
print(result)
AShape (7,), [False, True, True, False, True, True, True]
BShape (2,), [True, True]
CShape (7,), [True, True, True, True, True, True, True]
DShape (4,), [True, True, True, True]
Attempts:
2 left
💡 Hint
np.in1d() returns a boolean array matching the first array's shape.