Bird
Raised Fist0
NumPydata~20 mins

Universal functions (ufuncs) in NumPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Ufunc Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of numpy ufunc with broadcasting
What is the output of this code snippet using numpy universal functions and broadcasting?
NumPy
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([[1], [2], [3]])
result = np.add(arr1, arr2)
print(result)
A
[[2 3 4]
 [3 4 5]
 [4 5 6]]
B
[[2 3 4]
 [3 4 5]
 [4 5 5]]
C
[[2 3 4]
 [3 4 5]
 [4 5 7]]
D
[[2 3 4]
 [3 5 6]
 [4 6 7]]
Attempts:
2 left
💡 Hint
Remember numpy broadcasts arrays to compatible shapes before applying ufuncs.
❓ data_output
intermediate
2:00remaining
Result of numpy sqrt ufunc on negative values
What is the output array when applying numpy.sqrt to this array containing negative values?
NumPy
import numpy as np
arr = np.array([4, 9, -1, 16])
result = np.sqrt(arr)
print(result)
A[2. 3. -1. 4.]
B[2. 3. 1. 4.]
C[2. 3. nan 4.]
D[2. 3. 0. 4.]
Attempts:
2 left
💡 Hint
Square root of negative numbers is not defined in real numbers, numpy returns nan with a warning.
❓ visualization
advanced
3:00remaining
Visualizing numpy ufunc output with vectorize
Which plot correctly shows the output of applying a numpy ufunc to a range of values using np.vectorize?
NumPy
import numpy as np
import matplotlib.pyplot as plt

def custom_func(x):
    return np.sin(x) if x >= 0 else 0

vec_func = np.vectorize(custom_func)
x = np.linspace(-np.pi, np.pi, 100)
y = vec_func(x)
plt.plot(x, y)
plt.title('Custom function output')
plt.show()
AA sine wave mirrored about y-axis
BA full sine wave from -pi to pi
CA flat line at zero
DA plot showing sine wave only for x >= 0 and zero for x < 0
Attempts:
2 left
💡 Hint
The function returns sine only for non-negative x, zero otherwise.
🧠 Conceptual
advanced
2:00remaining
Understanding numpy ufunc reduce method
What is the output of this code using numpy's ufunc reduce method?
NumPy
import numpy as np
arr = np.array([1, 2, 3, 4])
result = np.add.reduce(arr)
print(result)
A[1 3 6 10]
B10
C24
D[1 2 3 4]
Attempts:
2 left
💡 Hint
ufunc reduce applies the operation cumulatively to the array elements.
🔧 Debug
expert
2:30remaining
Identify the error in this numpy ufunc usage
What error does this code raise when applying numpy's power ufunc with incompatible shapes?
NumPy
import numpy as np
base = np.array([1, 2, 3])
exponent = np.array([[1, 2], [3, 4]])
result = np.power(base, exponent)
print(result)
AValueError: operands could not be broadcast together with shapes (3,) (2,2)
BNo error, outputs a 2x3 array
CIndexError: index out of bounds
DTypeError: unsupported operand type(s) for ** or pow()
Attempts:
2 left
💡 Hint
Check if the shapes of base and exponent arrays can be broadcast together.

Practice

(1/5)
1. What is the main purpose of NumPy universal functions (ufuncs)?
easy
A. To convert arrays into lists
B. To perform element-wise operations on arrays efficiently
C. To sort arrays in ascending order
D. To create new arrays from scratch

Solution

  1. Step 1: Understand what ufuncs do

    Ufuncs apply mathematical operations element-wise on arrays, making calculations fast and simple.
  2. Step 2: Compare with other options

    Creating arrays, sorting, or converting to lists are not the main roles of ufuncs.
  3. Final Answer:

    To perform element-wise operations on arrays efficiently -> Option B
  4. Quick Check:

    Ufuncs = element-wise fast operations [OK]
Hint: Remember: ufuncs work element-wise on arrays fast [OK]
Common Mistakes:
  • Confusing ufuncs with array creation functions
  • Thinking ufuncs sort arrays
  • Assuming ufuncs convert arrays to lists
2. Which of the following is the correct syntax to use the NumPy universal function for square root on array arr?
easy
A. np.sqrt(arr)
B. np.square(arr)
C. np.root(arr)
D. np.sqrt_of(arr)

Solution

  1. Step 1: Identify the correct ufunc for square root

    The NumPy function for square root is np.sqrt().
  2. Step 2: Check the syntax with the array

    Calling np.sqrt(arr) applies square root element-wise on arr.
  3. Final Answer:

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

    Square root ufunc = np.sqrt() [OK]
Hint: Square root ufunc is always np.sqrt(array) [OK]
Common Mistakes:
  • Using np.square instead of np.sqrt
  • Trying non-existent functions like np.root
  • Adding extra words in function name
3. What is the output of the following code?
import numpy as np
arr = np.array([1, 4, 9, 16])
result = np.sqrt(arr)
print(result)
medium
A. [1. 2. 3. 4.]
B. [1 2 3 4]
C. [1. 4. 9. 16.]
D. [0. 2. 3. 4.]

Solution

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

    Applying np.sqrt on each element: sqrt(1)=1.0, sqrt(4)=2.0, sqrt(9)=3.0, sqrt(16)=4.0.
  2. Step 2: Check output format

    NumPy returns floats for sqrt, so output is [1. 2. 3. 4.].
  3. Final Answer:

    [1. 2. 3. 4.] -> Option A
  4. Quick Check:

    np.sqrt([1,4,9,16]) = [1. 2. 3. 4.] [OK]
Hint: Square root returns floats, watch decimal points [OK]
Common Mistakes:
  • Expecting integer output instead of float
  • Confusing np.sqrt with np.square
  • Misreading array output format
4. The following code throws an error. What is the problem?
import numpy as np
arr = np.array([1, 2, 3])
result = np.add(arr, 5, 10)
print(result)
medium
A. np.add cannot add a scalar to an array
B. The array must be of float type for np.add
C. np.add takes only two arguments, but three were given
D. The print statement is incorrect

Solution

  1. Step 1: Check np.add function signature

    np.add takes exactly two arguments: two arrays or an array and a scalar.
  2. Step 2: Identify the error in arguments

    Here, three arguments are passed: arr, 5, and 10, which is invalid.
  3. Final Answer:

    np.add takes only two arguments, but three were given -> Option C
  4. Quick Check:

    np.add = 2 args only [OK]
Hint: np.add needs exactly two inputs, no extras [OK]
Common Mistakes:
  • Passing more than two arguments to np.add
  • Thinking np.add requires float arrays
  • Blaming print instead of function call
5. You have two arrays:
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

Which code correctly computes the element-wise product and then applies the square root to each product?
hard
A. np.multiply(a, b) + np.sqrt(a)
B. np.multiply(np.sqrt(a), np.sqrt(b))
C. np.sqrt(a) * np.sqrt(b)
D. np.sqrt(np.multiply(a, b))

Solution

  1. Step 1: Understand element-wise product and sqrt

    First multiply elements of a and b, then apply square root on the result.
  2. Step 2: Check each option

    np.sqrt(np.multiply(a, b)) does np.multiply(a, b) then np.sqrt(), which is correct. Options B and C apply sqrt before multiply, which is different. np.multiply(a, b) + np.sqrt(a) adds sqrt(a), which is incorrect.
  3. Final Answer:

    np.sqrt(np.multiply(a, b)) -> Option D
  4. Quick Check:

    Multiply then sqrt = np.sqrt(np.multiply(a,b)) [OK]
Hint: Multiply first, then sqrt for correct order [OK]
Common Mistakes:
  • Applying sqrt before multiplication
  • Adding instead of multiplying
  • Mixing order of operations
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard