Bird
Raised Fist0
NumPydata~20 mins

np.abs() for absolute values 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
🎖️
Absolute Value Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of np.abs() on mixed array
What is the output of this code?
import numpy as np
arr = np.array([-3, 0, 4, -7])
result = np.abs(arr)
print(result)
NumPy
import numpy as np
arr = np.array([-3, 0, 4, -7])
result = np.abs(arr)
print(result)
A[3 0 4 7]
B[-3 0 4 -7]
C[3 0 -4 7]
D[0 3 4 7]
Attempts:
2 left
💡 Hint
np.abs() returns the absolute value of each element in the array.
❓ data_output
intermediate
2:00remaining
Absolute values of a 2D numpy array
Given this 2D array, what is the output of np.abs() applied to it?
import numpy as np
arr = np.array([[1, -2], [-3, 4]])
result = np.abs(arr)
print(result)
NumPy
import numpy as np
arr = np.array([[1, -2], [-3, 4]])
result = np.abs(arr)
print(result)
A
[[-1 2]
 [3 -4]]
B
[[1 -2]
 [-3 4]]
C
[[1 2]
 [3 4]]
D
[[1 2]
 [-3 4]]
Attempts:
2 left
💡 Hint
np.abs() works element-wise on arrays of any shape.
🧠 Conceptual
advanced
2:00remaining
Effect of np.abs() on complex numbers
What does np.abs() return when applied to a numpy array of complex numbers?
import numpy as np
arr = np.array([3+4j, 1-1j])
result = np.abs(arr)
print(result)
NumPy
import numpy as np
arr = np.array([3+4j, 1-1j])
result = np.abs(arr)
print(result)
A[5. 1.41421356]
B[3.+4.j 1.-1.j]
C[7.+0.j 2.+0.j]
D[-3.-4.j -1.+1.j]
Attempts:
2 left
💡 Hint
np.abs() returns the magnitude (distance from zero) for complex numbers.
🔧 Debug
advanced
2:00remaining
Identify the error in np.abs() usage
What error will this code raise?
import numpy as np
arr = np.array(['-3', '4', '-5'])
result = np.abs(arr)
print(result)
NumPy
import numpy as np
arr = np.array(['-3', '4', '-5'])
result = np.abs(arr)
print(result)
ASyntaxError: invalid syntax
BValueError: invalid literal for int() with base 10
CNo error, output: [3 4 5]
DTypeError: bad operand type for abs(): 'str'
Attempts:
2 left
💡 Hint
np.abs() expects numeric types, not strings.
🚀 Application
expert
3:00remaining
Using np.abs() to find max absolute difference
Given two numpy arrays, which option correctly finds the maximum absolute difference between their elements?
import numpy as np
a = np.array([1, -3, 5])
b = np.array([4, -1, -6])
max_diff = ???
print(max_diff)
NumPy
import numpy as np
a = np.array([1, -3, 5])
b = np.array([4, -1, -6])
max_diff = ???
print(max_diff)
Anp.abs(np.max(a) - np.max(b))
Bnp.max(np.abs(a - b))
Cnp.max(a) - np.min(b)
Dnp.abs(np.min(a) - np.min(b))
Attempts:
2 left
💡 Hint
Find the absolute difference element-wise, then get the max.

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

  1. 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.
  2. Step 2: Apply to numbers or arrays

    It works on single numbers or arrays, returning the size without sign for each element.
  3. Final Answer:

    Returns the absolute value of a number or each element in an array -> Option D
  4. 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

  1. Step 1: Recall the correct function name

    The correct NumPy function to get absolute values is np.abs().
  2. Step 2: Check syntax correctness

    Calling np.abs(arr) applies the function to the array correctly. Other options have wrong function names or syntax.
  3. Final Answer:

    np.abs(arr) -> Option C
  4. Quick Check:

    np.abs(array) is correct syntax [OK]
Hint: Use np.abs() exactly, no extra words or dot on array [OK]
Common Mistakes:
  • Using abs(np.arr) which is invalid
  • Writing np.absolutevalue instead of np.abs
  • Trying to call abs() as a method on array
3. What is the output of the following code?
import numpy as np
arr = np.array([-3, 0, 4, -7])
print(np.abs(arr))
medium
A. [0 3 4 7]
B. [3 0 4 7]
C. [-3 0 4 -7]
D. [3 0 -4 7]

Solution

  1. Step 1: Understand np.abs on array elements

    np.abs() converts each element to its absolute value, removing negative signs.
  2. Step 2: Apply to each element in arr

    Elements: -3 -> 3, 0 -> 0, 4 -> 4, -7 -> 7.
  3. Final Answer:

    [3 0 4 7] -> Option B
  4. 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

  1. Step 1: Identify function call syntax

    Functions in Python require parentheses () to call, not square brackets [].
  2. Step 2: Check np.abs usage

    np.abs[arr] tries to index np.abs, causing an error. Correct is np.abs(arr).
  3. Final Answer:

    Using square brackets [] instead of parentheses () with np.abs -> Option A
  4. 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

  1. Step 1: Understand the goal

    We want the total magnitude, so sum of absolute values of each change.
  2. 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.
  3. Final Answer:

    total_change = np.sum(np.abs(temps)) -> Option A
  4. 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
  • Summing without absolute, cancelling negatives
  • Using wrong function names