0
0
NumPydata~10 mins

np.sign() for sign detection 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 sign of each number in the array using np.sign().

NumPy
import numpy as np
arr = np.array([-3, 0, 4])
signs = np.[1](arr)
print(signs)
Drag options to blanks, or click blank then click option'
Aabs
Bsign
Csqrt
Dlog
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.abs() which returns absolute values, not signs.
Using np.sqrt() which calculates square roots.
Using np.log() which calculates logarithms.
2fill in blank
medium

Complete the code to create a boolean mask for positive numbers using np.sign().

NumPy
import numpy as np
arr = np.array([-2, 5, 0, 3])
mask = np.sign(arr) [1] 1
print(mask)
Drag options to blanks, or click blank then click option'
A<
B!=
C>
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' which selects non-positive numbers.
Using '<' or '>' which do not correctly select only positive signs.
3fill in blank
hard

Fix the error in the code to correctly compute signs of values in the list.

NumPy
import numpy as np
values = [-1, 0, 2]
signs = np.sign([1])
print(signs)
Drag options to blanks, or click blank then click option'
Anp.array(values)
B[values]
Clist(values)
Dvalues
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the list directly without conversion.
Wrapping the list in another list which creates a nested list.
Using list() which does not convert to numpy array.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their sign of length comparison to 3 as values.

NumPy
import numpy as np
words = ['cat', 'dog', 'elephant']
sign_dict = {word: np.sign(len(word) [1] 3) for word in words if len(word) [2] 2}
print(sign_dict)
Drag options to blanks, or click blank then click option'
A>
B==
C>=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' in the first blank which returns 0 or 1 only.
Using '<' in the second blank which filters out longer words.
5fill in blank
hard

Fill both blanks to create a dictionary with uppercase words as keys, their lengths as values, and only include words with positive sign difference from 4.

NumPy
import numpy as np
words = ['apple', 'bat', 'carrot', 'dog']
result = {word[1]: len(word) for word in words if np.sign(len(word) [2] 4) == 1}
print(result)
Drag options to blanks, or click blank then click option'
A.upper()
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Not converting keys to uppercase.
Using '<=' instead of '>' in the sign comparison.
Adding an operator in the value part which is not needed.