Challenge - 5 Problems
Math Functions Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of numpy math function on array
What is the output of this code snippet using numpy's sqrt function on an array?
NumPy
import numpy as np arr = np.array([1, 4, 9, 16]) result = np.sqrt(arr) print(result)
Attempts:
2 left
💡 Hint
Remember that np.sqrt calculates the square root element-wise and returns floats.
✗ Incorrect
The numpy sqrt function returns the square root of each element as a float array. So sqrt(1)=1.0, sqrt(4)=2.0, etc.
❓ data_output
intermediate2:00remaining
Result of applying numpy log function
What is the output array when applying numpy's natural log function to this array?
NumPy
import numpy as np arr = np.array([1, np.e, np.e**2]) result = np.log(arr) print(result)
Attempts:
2 left
💡 Hint
Recall that log(e) = 1 and log(e^2) = 2.
✗ Incorrect
The natural log of 1 is 0, of e is 1, and of e squared is 2, so the output is [0. 1. 2.]
❓ visualization
advanced3:00remaining
Visualizing the effect of numpy sin function
Which plot correctly shows the sine values for angles from 0 to 2π using numpy?
NumPy
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2*np.pi, 100) y = np.sin(x) plt.plot(x, y) plt.title('Sine Wave') plt.show()
Attempts:
2 left
💡 Hint
The sine function starts at 0 and oscillates between -1 and 1 over 0 to 2π.
✗ Incorrect
The sine wave starts at 0, peaks at 1 at π/2, crosses 0 at π, troughs at -1 at 3π/2, and returns to 0 at 2π.
🧠 Conceptual
advanced2:00remaining
Why use numpy math functions over Python math module?
Which reason best explains why numpy math functions are preferred for arrays over Python's math module?
Attempts:
2 left
💡 Hint
Think about how numpy handles many numbers at once.
✗ Incorrect
Numpy functions are designed to work on whole arrays at once, making them faster and more convenient for data science tasks than math module functions which only handle one number at a time.
🔧 Debug
expert3:00remaining
Identify the error in numpy math function usage
What error will this code raise and why?
import numpy as np
arr = np.array([-1, 0, 1])
result = np.sqrt(arr)
print(result)
NumPy
import numpy as np arr = np.array([-1, 0, 1]) result = np.sqrt(arr) print(result)
Attempts:
2 left
💡 Hint
Square root of negative numbers is not defined for real numbers.
✗ Incorrect
Numpy sqrt on negative numbers returns nan and raises a RuntimeWarning about invalid value encountered. It does not raise ValueError or TypeError.