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
Using np.abs() to Find Absolute Values
📖 Scenario: Imagine you have a list of temperature changes recorded over a week. Some values are negative, showing a drop in temperature, and some are positive, showing a rise. You want to find out how big the changes were, regardless of direction.
🎯 Goal: You will create a NumPy array with temperature changes, then use np.abs() to find the absolute values of these changes. This will help you see the size of each change without worrying about whether it was up or down.
📋 What You'll Learn
Create a NumPy array called temp_changes with the exact values: -3, 5, -1, 7, -4
Create a variable called absolute_changes that stores the absolute values of temp_changes using np.abs()
Print the absolute_changes array
💡 Why This Matters
🌍 Real World
Absolute values are useful in many real-world cases like measuring distances, temperature changes, or financial gains and losses without worrying about direction.
💼 Career
Data scientists often use absolute values to analyze data magnitude, ignoring whether values are positive or negative, which helps in clearer insights and decision making.
Progress0 / 4 steps
1
Create the temperature changes array
Import NumPy as np and create a NumPy array called temp_changes with these exact values: -3, 5, -1, 7, -4.
NumPy
Hint
Use np.array() to create the array with the exact values.
2
Calculate absolute values
Create a variable called absolute_changes that stores the absolute values of the temp_changes array using np.abs().
NumPy
Hint
Use np.abs(temp_changes) to get the absolute values.
3
Print the absolute values
Print the absolute_changes array to see the absolute temperature changes.
NumPy
Hint
Use print(absolute_changes) to display the result.
4
Summary: Understanding absolute values with np.abs()
Review the code and output to understand how np.abs() converts negative numbers to positive, showing the size of changes without direction.
NumPy
Hint
Look at the printed array to see all positive values representing the size of changes.
Practice
(1/5)
1. What does the np.abs() function do in NumPy?
easy
A. Calculates the square of a number
B. Computes the negative of a number
C. Finds the maximum value in an array
D. Returns the absolute value of a number or each element in an array
Solution
Step 1: Understand the purpose of np.abs()
The function np.abs() returns the absolute value, which means it removes any negative sign from numbers.
Step 2: Apply to numbers or arrays
It works on single numbers or arrays, returning the size without sign for each element.
Final Answer:
Returns the absolute value of a number or each element in an array -> Option D
Quick Check:
np.abs(-5) = 5 [OK]
Hint: Think: absolute means distance from zero, no minus sign [OK]
Common Mistakes:
Confusing absolute value with square
Thinking it finds max value
Assuming it negates numbers
2. Which of the following is the correct syntax to get absolute values of a NumPy array arr?
easy
A. np.absolutevalue(arr)
B. abs(np.arr)
C. np.abs(arr)
D. arr.abs()
Solution
Step 1: Recall the correct function name
The correct NumPy function to get absolute values is np.abs().
Step 2: Check syntax correctness
Calling np.abs(arr) applies the function to the array correctly. Other options have wrong function names or syntax.
Final Answer:
np.abs(arr) -> Option C
Quick Check:
np.abs(array) is correct syntax [OK]
Hint: Use np.abs() exactly, no extra words or dot on array [OK]
np.abs() converts each element to its absolute value, removing negative signs.
Step 2: Apply to each element in arr
Elements: -3 -> 3, 0 -> 0, 4 -> 4, -7 -> 7.
Final Answer:
[3 0 4 7] -> Option B
Quick Check:
Absolute values remove negatives [OK]
Hint: Replace negatives with positive, keep zeros and positives same [OK]
Common Mistakes:
Leaving negative signs unchanged
Mixing element order
Confusing zero with negative
4. The code below throws an error. What is the mistake?
import numpy as np
arr = [-1, -2, 3]
print(np.abs[arr])
medium
A. Using square brackets [] instead of parentheses () with np.abs
B. Array must be a NumPy array, not a list
C. np.abs cannot handle negative numbers
D. Missing import statement
Solution
Step 1: Identify function call syntax
Functions in Python require parentheses () to call, not square brackets [].
Step 2: Check np.abs usage
np.abs[arr] tries to index np.abs, causing an error. Correct is np.abs(arr).
Final Answer:
Using square brackets [] instead of parentheses () with np.abs -> Option A
Quick Check:
Function calls need () not [] [OK]
Hint: Remember: functions use () to call, [] is for indexing [OK]
Common Mistakes:
Using [] instead of () for function calls
Thinking np.abs only works on NumPy arrays
Ignoring import statement errors
5. You have an array of temperature changes: temps = np.array([-5, 3, -2, 0, 4]). You want to find the total magnitude of change ignoring direction. Which code correctly calculates this?
hard
A. total_change = np.sum(np.abs(temps))
B. total_change = np.abs(np.sum(temps))
C. total_change = np.sum(temps)
D. total_change = np.abs(temps.sum())
Solution
Step 1: Understand the goal
We want the total magnitude, so sum of absolute values of each change.
Step 2: Compare options
total_change = np.sum(np.abs(temps)) sums absolute values element-wise, correct. total_change = np.abs(np.sum(temps)) sums first then abs, losing individual magnitudes. Options C and D ignore absolute values properly.
Final Answer:
total_change = np.sum(np.abs(temps)) -> Option A
Quick Check:
Sum of absolute values gives total magnitude [OK]
Hint: Sum absolute values, not absolute of sum [OK]
Common Mistakes:
Taking absolute after summing, losing individual sizes