0
0
NumPydata~10 mins

np.intersect1d() for intersection in NumPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to find the common elements between two arrays using numpy.

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
Bunion1d
Csetdiff1d
Dunique
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.union1d() which returns all unique elements from both arrays.
Using np.setdiff1d() which returns elements in one array but not the other.
2fill in blank
medium

Complete the code to find the intersection of two lists converted to numpy arrays.

NumPy
import numpy as np
list1 = [10, 20, 30, 40]
list2 = [30, 40, 50, 60]
arr1 = np.array(list1)
arr2 = np.array(list2)
result = np.intersect1d(arr1, [1])
print(result)
Drag options to blanks, or click blank then click option'
Alist2
Barr2
Cnp.array(list2)
Dnp.array(list1)
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a list instead of a numpy array as the second argument.
Passing the first array again instead of the second.
3fill in blank
hard

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

NumPy
import numpy as np
arr1 = np.array([7, 8, 9])
arr2 = np.array([8, 9, 10])
common = np.intersect1d(arr1, arr2[1])
print(common)
Drag options to blanks, or click blank then click option'
A, assume_unique=True
B, axis=0
C)
D, return_indices=True
Attempts:
3 left
💡 Hint
Common Mistakes
Adding unnecessary parameters that cause errors.
Forgetting to close the function call with a parenthesis.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths only if length is greater than 3.

NumPy
words = ['data', 'science', 'ai', 'ml']
lengths = {word: [1] for word in words if [2]
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
Clen(word) > 3
Dword > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Checking if the word string is greater than 3 instead of its length.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase keys and values only for items with values greater than 0.

NumPy
data = {'a': 1, 'b': -1, 'c': 3}
result = { [1]: [2] for k, v in data.items() if v [3] 0 }
print(result)
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Using original keys without uppercase.
Using keys as values or vice versa.
Using wrong comparison operators.