0
0
NumPydata~20 mins

np.exp() and np.log() in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Exponential and Logarithm Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of np.exp() on a numpy array
What is the output of the following code?
NumPy
import numpy as np
arr = np.array([0, 1, 2])
result = np.exp(arr)
print(result)
A[0 1 2]
B[1. 2.71828183 7.3890561 ]
C[1 1 1]
D[0. 1. 2.]
Attempts:
2 left
💡 Hint
Recall that np.exp(x) calculates e to the power of x for each element.
Predict Output
intermediate
2:00remaining
Output of np.log() on a numpy array
What is the output of this code snippet?
NumPy
import numpy as np
arr = np.array([1, np.e, np.e**2])
result = np.log(arr)
print(result)
A[1. 1. 1.]
B[1. 2. 3.]
C[0. 1. 2.]
D[0. 0. 0.]
Attempts:
2 left
💡 Hint
np.log() returns the natural logarithm (base e) of each element.
data_output
advanced
2:00remaining
Result of combining np.exp() and np.log()
What is the output of this code?
NumPy
import numpy as np
arr = np.array([1, 2, 3])
result = np.exp(np.log(arr))
print(result)
A[1. 2. 3.]
B[0. 0. 0.]
C[1 1 1]
D[2.71828183 7.3890561 20.08553692]
Attempts:
2 left
💡 Hint
Think about what happens when you apply exp to the log of a number.
🧠 Conceptual
advanced
2:00remaining
Behavior of np.log() with zero and negative inputs
What happens when you run np.log() on an array containing zero or negative numbers?
NumPy
import numpy as np
arr = np.array([0, -1, 1])
result = np.log(arr)
print(result)
AIt returns [-inf, nan, 0.] with a runtime warning.
BIt returns [0, 0, 0] without any warnings.
CIt raises a ValueError and stops execution.
DIt returns [inf, -inf, 1] without warnings.
Attempts:
2 left
💡 Hint
Logarithm of zero or negative numbers is not defined in real numbers.
🚀 Application
expert
3:00remaining
Calculate the geometric mean using np.log() and np.exp()
Given a numpy array of positive numbers, which code correctly calculates the geometric mean?
NumPy
import numpy as np
arr = np.array([1, 10, 100])
Anp.mean(np.exp(arr))
Bnp.exp(np.sum(arr))
Cnp.log(np.mean(arr))
Dnp.exp(np.mean(np.log(arr)))
Attempts:
2 left
💡 Hint
Geometric mean is the nth root of the product of n numbers, which can be calculated using logs and exponentials.