Bird
Raised Fist0
NumPydata~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does the np.sign() function return when applied to a negative number?
easy
A. -1
B. 0
C. 1
D. The original number

Solution

  1. Step 1: Understand np.sign() behavior

    The function returns -1 for negative numbers, 0 for zero, and 1 for positive numbers.
  2. Step 2: Apply to a negative number

    Since the input is negative, np.sign() returns -1.
  3. Final Answer:

    -1 -> Option A
  4. Quick Check:

    Negative number sign = -1 [OK]
Hint: Negative input always gives -1 from np.sign() [OK]
Common Mistakes:
  • Confusing negative with zero
  • Expecting original number as output
  • Thinking it returns boolean
2. Which of the following is the correct syntax to get the sign of each element in a numpy array arr?
easy
A. np.sign(arr)
B. arr.sign()
C. sign(arr)
D. np.sign_of(arr)

Solution

  1. Step 1: Recall numpy function usage

    Functions in numpy are called with the syntax np.function_name(arguments).
  2. Step 2: Identify correct function call

    The correct function to get sign is np.sign(), so np.sign(arr) is correct.
  3. Final Answer:

    np.sign(arr) -> Option A
  4. Quick Check:

    Correct numpy function call = np.sign(arr) [OK]
Hint: Use np.sign(array) to get signs of all elements [OK]
Common Mistakes:
  • Using method on array like arr.sign()
  • Calling sign() without np prefix
  • Using non-existent np.sign_of()
3. What is the output of the following code?
import numpy as np
arr = np.array([-3, 0, 4])
sign_arr = np.sign(arr)
print(sign_arr)
medium
A. [-3 0 4]
B. [3 0 4]
C. [0 0 0]
D. [-1 0 1]

Solution

  1. Step 1: Understand input array values

    The array has values -3 (negative), 0 (zero), and 4 (positive).
  2. Step 2: Apply np.sign() to each element

    np.sign(-3) = -1, np.sign(0) = 0, np.sign(4) = 1, so the output array is [-1, 0, 1].
  3. Final Answer:

    [-1 0 1] -> Option D
  4. Quick Check:

    Signs of [-3,0,4] = [-1,0,1] [OK]
Hint: np.sign() maps negative to -1, zero to 0, positive to 1 [OK]
Common Mistakes:
  • Expecting original values
  • Confusing zero with positive
  • Outputting boolean instead of sign
4. The following code throws an error. What is the mistake?
import numpy as np
arr = [-1, 2, 0]
signs = np.sign arr
print(signs)
medium
A. print() syntax error
B. Using list instead of numpy array
C. Missing parentheses in function call
D. np.sign does not exist

Solution

  1. Step 1: Check function call syntax

    The code uses np.sign arr without parentheses, which is invalid syntax in Python.
  2. Step 2: Correct the syntax

    It should be np.sign(arr) with parentheses to call the function properly.
  3. Final Answer:

    Missing parentheses in function call -> Option C
  4. Quick Check:

    Function calls need parentheses [OK]
Hint: Always use parentheses when calling functions [OK]
Common Mistakes:
  • Omitting parentheses
  • Thinking lists cause error here
  • Assuming np.sign is undefined
5. You have a numpy array data = np.array([-5, 0, 3, -2, 7]). How can you create a new array that replaces all negative values with 0, using np.sign()?
hard
A. data * np.sign(data)
B. data * (np.sign(data) + 1) / 2
C. np.sign(data) * 2
D. data + np.sign(data)

Solution

  1. Step 1: Understand np.sign() output

    np.sign(data) gives -1 for negatives, 0 for zero, 1 for positives.
  2. Step 2: Transform sign to mask for positives and zero

    Adding 1 to sign gives 0 for -1, 1 for 0, 2 for 1. Dividing by 2 maps negatives to 0, zero to 0.5, positives to 1.
  3. Step 3: Multiply original data by this mask

    Multiplying data by this mask sets negative values to 0, keeps zero and positive values unchanged (zero times 0.5 is 0).
  4. Final Answer:

    data * (np.sign(data) + 1) / 2 -> Option B
  5. Quick Check:

    Mask negatives to zero using (sign+1)/2 [OK]
Hint: Use (sign+1)/2 as mask to zero negatives [OK]
Common Mistakes:
  • Using sign directly multiplies negatives
  • Adding sign to data changes values wrongly
  • Confusing mask calculation