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
Using np.exp() and np.log() for Data Transformation
📖 Scenario: Imagine you are analyzing daily growth rates of a small plant species. You have recorded the growth rates as percentages, and you want to understand the exponential growth and also convert back from exponential values to the original growth rates.
🎯 Goal: You will create a numpy array of growth rates, apply the exponential function to simulate growth, then use the natural logarithm to retrieve the original rates. This helps understand how exponential and logarithmic functions work in data science.
📋 What You'll Learn
Create a numpy array with exact growth rates
Create a variable to hold the base of natural logarithm (e)
Use np.exp() to calculate exponential growth
Use np.log() to convert exponential values back to original growth rates
Print the final results
💡 Why This Matters
🌍 Real World
Exponential and logarithmic functions are used in biology to model growth, in finance to calculate compound interest, and in many data science tasks to transform data for analysis.
💼 Career
Understanding how to apply np.exp() and np.log() is important for data scientists working with growth models, time series data, and feature engineering.
Progress0 / 4 steps
1
Create the growth rates array
Import numpy as np and create a numpy array called growth_rates with these exact values: 0.05, 0.10, 0.15, 0.20, 0.25.
NumPy
Hint
Use np.array() to create the array with the given values.
2
Create the constant for natural logarithm base
Create a variable called e and assign it the value of the mathematical constant e using np.e.
NumPy
Hint
Use np.e to get the value of e.
3
Calculate exponential growth and apply logarithm
Create a variable called exp_growth by applying np.exp() to growth_rates. Then create a variable called log_values by applying np.log() to exp_growth.
NumPy
Hint
Use np.exp() to get exponential values and np.log() to get back the original values.
4
Print the exponential and logarithmic results
Print the variables exp_growth and log_values each on a separate line.
NumPy
Hint
Use two print() statements to display the arrays.
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
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.
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().
Final Answer:
The value of e raised to the power x -> Option B
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
Step 1: Identify the function for natural logarithm
The natural logarithm in NumPy is computed using np.log().
Step 2: Check other options for correctness
np.exp() calculates exponentials, np.ln() does not exist, and np.log10() calculates base-10 logarithm.
Final Answer:
np.log(arr) -> Option A
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)
Taking the natural log of these values returns the original array [1, 2, 3] because log and exp are inverse functions.
Final Answer:
[1. 2. 3.] -> Option A
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
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.
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.
Final Answer:
np.log() cannot take zero or negative values -> Option C
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
Step 1: Understand the normalization step
Applying np.log(data) transforms data to a logarithmic scale, useful for normalization.
Step 2: Reverse transformation
To get back original data, apply np.exp() to the logged data because exp is the inverse of log.
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.
Final Answer:
Apply np.log(data) first, then np.exp() on the result to reverse -> Option D