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
Detecting Number Signs Using np.sign()
📖 Scenario: Imagine you have a list of daily temperature changes in degrees Celsius. Some days are warmer (positive change), some are colder (negative change), and some days have no change (zero). You want to quickly find out which days were warmer, colder, or stayed the same.
🎯 Goal: You will create a program that uses np.sign() to detect the sign of each temperature change and output the results as an array of -1, 0, or 1.
📋 What You'll Learn
Create a numpy array with exact temperature changes
Create a variable to hold the numpy array
Use np.sign() to find the sign of each number
Print the resulting sign array
💡 Why This Matters
🌍 Real World
Detecting signs of numbers is useful in finance to know if stock prices went up or down, in weather to track temperature changes, or in any data where direction matters.
💼 Career
Data scientists often need to quickly classify data points by their sign to make decisions or prepare data for further analysis.
Progress0 / 4 steps
1
Create the temperature changes array
Create a numpy array called temp_changes with these exact values: [-3, 0, 4, -1, 2, 0].
NumPy
Hint
Use np.array() to create the array with the exact values.
2
Prepare to detect signs
Create a variable called signs and set it to None as a placeholder before detecting signs.
NumPy
Hint
Just create the variable signs and assign None to it.
3
Detect the sign of each temperature change
Use np.sign() on the temp_changes array and assign the result to the variable signs.
NumPy
Hint
Call np.sign(temp_changes) and save it in signs.
4
Print the sign detection result
Print the variable signs to display the sign of each temperature change.
NumPy
Hint
Use print(signs) to show the output.
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
Step 1: Understand np.sign() behavior
The function returns -1 for negative numbers, 0 for zero, and 1 for positive numbers.
Step 2: Apply to a negative number
Since the input is negative, np.sign() returns -1.
Final Answer:
-1 -> Option A
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
Step 1: Recall numpy function usage
Functions in numpy are called with the syntax np.function_name(arguments).
Step 2: Identify correct function call
The correct function to get sign is np.sign(), so np.sign(arr) is correct.
Final Answer:
np.sign(arr) -> Option A
Quick Check:
Correct numpy function call = np.sign(arr) [OK]
Hint: Use np.sign(array) to get signs of all elements [OK]
The code uses np.sign arr without parentheses, which is invalid syntax in Python.
Step 2: Correct the syntax
It should be np.sign(arr) with parentheses to call the function properly.
Final Answer:
Missing parentheses in function call -> Option C
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
Step 1: Understand np.sign() output
np.sign(data) gives -1 for negatives, 0 for zero, 1 for positives.
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.
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).
Final Answer:
data * (np.sign(data) + 1) / 2 -> Option B
Quick Check:
Mask negatives to zero using (sign+1)/2 [OK]
Hint: Use (sign+1)/2 as mask to zero negatives [OK]