Bird
Raised Fist0
NumPydata~20 mins

np.exp() and np.log() 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
🎖️
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.

Practice

(1/5)
1. What does the function np.exp(x) compute in NumPy?
easy
A. The square root of x
B. The value of e raised to the power x
C. The natural logarithm of x
D. The sine of x

Solution

  1. Step 1: Understand the purpose of np.exp()

    The function np.exp() calculates e (Euler's number, approximately 2.718) raised to the power of the input value.
  2. Step 2: Compare with other options

    Other options describe different functions: natural log is np.log(), square root is np.sqrt(), sine is np.sin().
  3. Final Answer:

    The value of e raised to the power x -> Option B
  4. Quick Check:

    np.exp(x) = e^x [OK]
Hint: Remember: exp means e to the power x [OK]
Common Mistakes:
  • Confusing np.exp() with np.log()
  • Thinking np.exp() calculates logarithm
  • Mixing up with square root or trigonometric functions
2. Which of the following is the correct syntax to compute the natural logarithm of a NumPy array arr?
easy
A. np.log(arr)
B. np.exp(arr)
C. np.ln(arr)
D. np.log10(arr)

Solution

  1. Step 1: Identify the function for natural logarithm

    The natural logarithm in NumPy is computed using np.log().
  2. Step 2: Check other options for correctness

    np.exp() calculates exponentials, np.ln() does not exist, and np.log10() calculates base-10 logarithm.
  3. Final Answer:

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

    Natural log = np.log() [OK]
Hint: Natural log uses np.log(), not np.ln() or np.log10() [OK]
Common Mistakes:
  • Using np.ln() which is not a valid NumPy function
  • Confusing natural log with base-10 log
  • Using np.exp() instead of np.log()
3. What is the output of the following code?
import numpy as np
arr = np.array([1, 2, 3])
result = np.log(np.exp(arr))
print(result)
medium
A. [1. 2. 3.]
B. [0. 0. 0.]
C. [2.718 7.389 20.086]
D. Error: invalid input

Solution

  1. Step 1: Understand the inner function np.exp(arr)

    Applying np.exp() to [1, 2, 3] gives [e^1, e^2, e^3] ≈ [2.718, 7.389, 20.086].
  2. Step 2: Apply np.log() to the result

    Taking the natural log of these values returns the original array [1, 2, 3] because log and exp are inverse functions.
  3. Final Answer:

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

    np.log(np.exp(x)) = x [OK]
Hint: log(exp(x)) returns x because they undo each other [OK]
Common Mistakes:
  • Expecting the exponential values instead of original
  • Confusing output with zeros
  • Thinking it causes an error
4. Identify the error in the following code snippet:
import numpy as np
arr = np.array([-1, 0, 1])
result = np.log(arr)
print(result)
medium
A. np.log() should be np.exp()
B. np.array() syntax is incorrect
C. np.log() cannot take zero or negative values
D. No error, code runs fine

Solution

  1. Step 1: Check input values for np.log()

    Natural logarithm is undefined for zero and negative numbers. The array contains -1 and 0, which cause errors or warnings.
  2. Step 2: Understand the error behavior

    NumPy will return -inf or NaN for zero or negative inputs, which is usually an error or warning in calculations.
  3. Final Answer:

    np.log() cannot take zero or negative values -> Option C
  4. Quick Check:

    Log input must be positive [OK]
Hint: Logarithm inputs must be positive numbers only [OK]
Common Mistakes:
  • Ignoring domain restrictions of log function
  • Confusing np.log() with np.exp()
  • Assuming code runs without warnings or errors
5. You have a dataset of positive values stored in a NumPy array data. You want to normalize it by applying the natural logarithm, then reverse the transformation after some processing. Which sequence of operations correctly achieves this?
hard
A. Apply np.exp(data) first, then np.log() on the result to reverse
B. Apply np.sqrt(data) first, then np.log() on the result to reverse
C. Apply np.log10(data) first, then np.exp() on the result to reverse
D. Apply np.log(data) first, then np.exp() on the result to reverse

Solution

  1. Step 1: Understand the normalization step

    Applying np.log(data) transforms data to a logarithmic scale, useful for normalization.
  2. Step 2: Reverse transformation

    To get back original data, apply np.exp() to the logged data because exp is the inverse of log.
  3. Step 3: Check other options

    Apply np.exp(data) first, then np.log() on the result to reverse reverses the order incorrectly, Apply np.log10(data) first, then np.exp() on the result to reverse mixes log base 10 with exp (base e), Apply np.sqrt(data) first, then np.log() on the result to reverse uses sqrt which is unrelated.
  4. Final Answer:

    Apply np.log(data) first, then np.exp() on the result to reverse -> Option D
  5. Quick Check:

    log then exp returns original data [OK]
Hint: Log then exp reverses; order matters [OK]
Common Mistakes:
  • Reversing the order of log and exp
  • Mixing log base 10 with exp
  • Using unrelated functions like sqrt