0
0
NumPydata~5 mins

np.abs() for absolute values in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the function np.abs() do in numpy?

np.abs() returns the absolute value of each element in an array. It changes negative numbers to positive, keeping positive numbers the same.

Click to reveal answer
beginner
How does np.abs() handle zero and positive numbers?

Zero stays zero, and positive numbers remain unchanged because their absolute value is the same as the original number.

Click to reveal answer
intermediate
Can np.abs() be used on arrays with complex numbers? What does it return?

Yes, it returns the magnitude (distance from zero) of each complex number, which is always a non-negative real number.

Click to reveal answer
beginner
Write a simple example using np.abs() on a numpy array with negative and positive numbers.
<pre>import numpy as np
arr = np.array([-3, 0, 4])
result = np.abs(arr)
print(result)  # Output: [3 0 4]</pre>
Click to reveal answer
beginner
Why is np.abs() useful in data science?

It helps measure distances, errors, or differences without worrying about direction (sign). For example, it is used in calculating absolute errors or distances.

Click to reveal answer
What will np.abs(np.array([-5, 2, -1])) return?
A[5, 2, 1]
B[-5, 2, -1]
C[0, 2, 0]
D[-2, 5, 1]
If z = np.array([3+4j, 1-1j]), what does np.abs(z) return?
A[5.0, 1.414]
B[3, 1]
C[7+0j, 2+0j]
D[3+4j, 1-1j]
Which of these is NOT a valid input for np.abs()?
AA numpy array of floats
BA numpy array of strings
CA numpy array of integers
DA numpy array of complex numbers
What is the output of np.abs(0)?
AError
B-0
C0
D1
Why might a data scientist use np.abs() when calculating errors?
ATo sort errors alphabetically
BTo ignore the size of errors
CTo convert errors to negative values
DTo consider only the magnitude of errors, ignoring direction
Explain what np.abs() does and give a simple example with a numpy array.
Think about how negative numbers become positive.
You got /4 concepts.
    Describe how np.abs() works with complex numbers and why this might be useful.
    Remember the formula for magnitude of a complex number.
    You got /3 concepts.