0
0
NumPydata~10 mins

np.exp() and np.log() in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - np.exp() and np.log()
Input array or value x
np.exp(x)
Calculate e^x
Output array or value
End
Start with input values, choose either exponential or natural log function, compute the result, and output the transformed values.
Execution Sample
NumPy
import numpy as np
x = np.array([1, 2, 3])
exp_x = np.exp(x)
log_x = np.log(exp_x)
print(exp_x)
print(log_x)
Compute exponential of array x, then compute natural log of the result, showing inverse operations.
Execution Table
StepVariableValueOperationResult
1x[1 2 3]Input array[1 2 3]
2exp_xnp.exp(x)Calculate e^x element-wise[2.71828183 7.3890561 20.08553692]
3log_xnp.log(exp_x)Calculate ln of exp_x element-wise[1. 2. 3.]
4print(exp_x)-Output exp_x[2.71828183 7.3890561 20.08553692]
5print(log_x)-Output log_x[1. 2. 3.]
6--End of executionAll operations complete
💡 All elements processed; np.log(np.exp(x)) returns original x values.
Variable Tracker
VariableStartAfter np.exp()After np.log()Final
x[1 2 3][1 2 3][1 2 3][1 2 3]
exp_xN/A[2.71828183 7.3890561 20.08553692][2.71828183 7.3890561 20.08553692][2.71828183 7.3890561 20.08553692]
log_xN/AN/A[1. 2. 3.][1. 2. 3.]
Key Moments - 2 Insights
Why does np.log(np.exp(x)) return the original x values?
Because np.exp(x) calculates e to the power of x, and np.log() calculates the natural logarithm, which is the inverse of the exponential function. This is shown in execution_table rows 2 and 3.
Can np.log() accept zero or negative values?
No, np.log() is only defined for positive numbers. Passing zero or negative values will cause errors or warnings. This is why input to np.log() in the example is always positive (exp_x).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of exp_x at step 2?
A[1 2 3]
B[1. 2. 3.]
C[2.71828183 7.3890561 20.08553692]
DError
💡 Hint
Check the 'Value' column at step 2 in the execution_table.
At which step does np.log() get applied to the array?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look for 'np.log(exp_x)' in the 'Operation' column in execution_table.
If x contained a zero, what would happen when np.log(exp_x) is computed?
Anp.log() would raise an error or warning
Bnp.exp() would fail first
CNo change, computation proceeds normally
DThe output would be zero
💡 Hint
Recall key_moments about domain restrictions of np.log() and the input values.
Concept Snapshot
np.exp(x): computes e^x element-wise on arrays or scalars
np.log(x): computes natural log (ln) element-wise, input must be positive
np.log(np.exp(x)) returns x because log and exp are inverse functions
Use np.exp() to scale values exponentially
Use np.log() to transform data to logarithmic scale
Full Transcript
This visual execution shows how numpy's np.exp() and np.log() functions work step-by-step. Starting with an input array x, np.exp(x) calculates the exponential e to the power of each element. Then np.log() takes the natural logarithm of those results, reversing the exponential operation. The execution table traces each step, showing variable values and operations. The variable tracker highlights how values change after each function call. Key moments clarify why np.log(np.exp(x)) returns the original x and warn that np.log() only accepts positive inputs. The quiz tests understanding of these steps and function behavior. This helps beginners see how these functions transform data and relate as inverses.