0
0
NumPydata~5 mins

np.in1d() for membership testing in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does np.in1d() do in numpy?

np.in1d() checks if each element of one array is present in another array. It returns a boolean array showing membership.

Click to reveal answer
beginner
How does np.in1d() differ from Python's in keyword?

np.in1d() works on whole arrays at once and returns an array of booleans, while in checks membership for a single element only.

Click to reveal answer
beginner
What is the output type of np.in1d(array1, array2)?

The output is a numpy boolean array. Each position is True if the element in array1 is found in array2, otherwise False.

Click to reveal answer
beginner
Example: What does np.in1d([1, 2, 3], [2, 3, 4]) return?

It returns [False, True, True] because 1 is not in the second array, but 2 and 3 are.

Click to reveal answer
beginner
Can np.in1d() be used with arrays of strings?

Yes, np.in1d() works with any array type, including strings, to test membership.

Click to reveal answer
What does np.in1d([5, 6, 7], [6, 8, 9]) return?
A[True, False, True]
B[False, True, False]
C[False, False, False]
D[True, True, True]
Which of these is true about np.in1d()?
AIt returns a boolean array.
BIt modifies the original arrays.
CIt only works with numeric arrays.
DIt returns the common elements.
If np.in1d(a, b) returns all False, what does it mean?
ANo elements of <code>a</code> are in <code>b</code>.
BAll elements of <code>a</code> are in <code>b</code>.
CArrays <code>a</code> and <code>b</code> are equal.
DArrays <code>a</code> and <code>b</code> have the same length.
Can np.in1d() be used to check membership of strings?
AOnly with integers and floats.
BNo, only numbers are supported.
CYes, it works with any data type.
DOnly with boolean arrays.
What is the shape of the output of np.in1d(array1, array2)?
A2D array
BSame shape as <code>array2</code>
CScalar boolean
DSame shape as <code>array1</code>
Explain how np.in1d() helps in checking if elements of one array exist in another.
Think about how you find if items from your shopping list are in the store's stock list.
You got /4 concepts.
    Describe a real-life example where np.in1d() could be useful.
    Imagine you want to find which friends from your contact list are attending a party.
    You got /4 concepts.