Challenge - 5 Problems
np.in1d() Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
np.in1d() checks if each element of the first array is in the second array.
✗ Incorrect
The function returns a boolean array where each position corresponds to whether the element in arr1 is found in arr2. Only 2 and 4 are in arr2, so positions 1 and 3 are True.
❓ data_output
intermediate2: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)
Attempts:
2 left
💡 Hint
Count how many True values are in the boolean mask from np.in1d().
✗ Incorrect
Only 20 and 40 from arr1 are found in arr2, so the count is 2.
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
np.in1d() expects the second argument to be iterable.
✗ Incorrect
Passing a single integer instead of an iterable causes a TypeError because np.in1d tries to iterate over arr2.
🚀 Application
advanced2: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?
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']
Attempts:
2 left
💡 Hint
np.in1d() returns a boolean array matching df['Name'] elements against selected_names.
✗ Incorrect
Option D uses np.in1d() correctly to create a boolean mask for filtering rows. Option D uses pandas isin(), which is valid but not np.in1d(). Options A and B are incorrect.
🧠 Conceptual
expert3:00remaining
Understanding np.in1d() behavior with repeated elements
Consider the arrays:
What is the shape and content of the output array?
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)
Attempts:
2 left
💡 Hint
np.in1d() returns a boolean array matching the first array's shape.
✗ Incorrect
The output array has the same length as arr1 (7). Each position is True if the element is in arr2 (2 or 4), otherwise False.