0
0
NumPydata~20 mins

np.sign() for sign detection in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sign Detection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of np.sign() on mixed values
What is the output of the following code?
import numpy as np
arr = np.array([-3, 0, 4, -1, 2])
signs = np.sign(arr)
print(signs)
NumPy
import numpy as np
arr = np.array([-3, 0, 4, -1, 2])
signs = np.sign(arr)
print(signs)
A[-1 0 1 -1 1]
B[1 0 1 1 1]
C[-1 1 1 -1 1]
D[0 0 0 0 0]
Attempts:
2 left
💡 Hint
np.sign returns -1 for negative, 0 for zero, and 1 for positive values.
data_output
intermediate
2:00remaining
Count of positive, negative, and zero values using np.sign()
Given the array below, what is the count of positive, negative, and zero values using np.sign()?
import numpy as np
arr = np.array([5, -2, 0, 7, -8, 0, 3])
signs = np.sign(arr)
counts = {"positive": np.sum(signs == 1), "negative": np.sum(signs == -1), "zero": np.sum(signs == 0)}
print(counts)
NumPy
import numpy as np
arr = np.array([5, -2, 0, 7, -8, 0, 3])
signs = np.sign(arr)
counts = {"positive": np.sum(signs == 1), "negative": np.sum(signs == -1), "zero": np.sum(signs == 0)}
print(counts)
A{"positive": 3, "negative": 2, "zero": 2}
B{"positive": 4, "negative": 2, "zero": 1}
C{"positive": 3, "negative": 3, "zero": 1}
D{"positive": 2, "negative": 2, "zero": 3}
Attempts:
2 left
💡 Hint
Count how many elements are 1, -1, and 0 in the signs array.
🔧 Debug
advanced
2:00remaining
Identify the error in np.sign usage
What error will this code raise?
import numpy as np
arr = np.array([1, -1, 0])
signs = np.sign(arr, out=[0,0,0])
print(signs)
NumPy
import numpy as np
arr = np.array([1, -1, 0])
signs = np.sign(arr, out=[0,0,0])
print(signs)
AAttributeError: 'list' object has no attribute 'dtype'
BValueError: shape mismatch
CTypeError: 'out' must be a ndarray
DNo error, prints [1 -1 0]
Attempts:
2 left
💡 Hint
The 'out' parameter expects a numpy array, not a list.
🚀 Application
advanced
2:00remaining
Using np.sign to flip signs conditionally
You want to flip the sign of all negative numbers in an array but keep positive and zero values unchanged. Which code snippet achieves this using np.sign() and numpy operations?
A
arr = np.array([-3, 0, 4, -1, 2])
result = arr * np.sign(arr)
B
arr = np.array([-3, 0, 4, -1, 2])
result = arr * (np.sign(arr) == 1) + arr * (np.sign(arr) == 0)
C
arr = np.array([-3, 0, 4, -1, 2])
result = np.abs(arr)
D
arr = np.array([-3, 0, 4, -1, 2])
result = arr * (np.sign(arr) == -1) * -1 + arr * (np.sign(arr) != -1)
Attempts:
2 left
💡 Hint
Use np.sign to detect negatives and multiply by -1 to flip them.
🧠 Conceptual
expert
2:00remaining
Behavior of np.sign with floating point edge cases
What is the output of np.sign() when applied to the array containing special floating point values?
import numpy as np
arr = np.array([-0.0, 0.0, np.nan, np.inf, -np.inf])
signs = np.sign(arr)
print(signs)
NumPy
import numpy as np
arr = np.array([-0.0, 0.0, np.nan, np.inf, -np.inf])
signs = np.sign(arr)
print(signs)
A[-0. 0. nan 1. -1.]
B[0. 0. nan 1. -1.]
C[-1. 0. nan 1. -1.]
D[0. 0. 0. 1. -1.]
Attempts:
2 left
💡 Hint
np.sign returns 0 for both +0.0 and -0.0, and nan for np.nan.