Calculating Exponential Functions in Python: Using math.exp() and Implementing the Sigmoid Function

In fields such as scientific computing and machine learning, the exponential function y=ex, with the base e (Napier’s number, approx. 2.718), appears frequently.

The Python standard library math module provides the math.exp() function to perform this calculation with high speed and precision.

This article explains the basic usage of math.exp() and demonstrates its application through an implementation of the “Sigmoid function,” which is commonly used in machine learning.

目次

Basic Usage of math.exp()

math.exp(x) calculates and returns e raised to the power of x (ex).

Syntax:

import math

value = math.exp(number)

Specific Usage Example: Radioactive Decay

Here is an example of calculating exponential decay. This simulates how the amount of a substance decreases over time in proportion to e−t.

import math

def calculate_decay(initial_amount, rate, time):
    """
    Function to calculate exponential decay
    N(t) = N0 * e^(-λt)
    """
    # Calculate using -rate * time as the exponent
    decay_factor = math.exp(-rate * time)
    return initial_amount * decay_factor

# Initial amount: 100, Decay rate: 0.5, Time: 2
amount_at_t2 = calculate_decay(100, 0.5, 2)

# Time: 5
amount_at_t5 = calculate_decay(100, 0.5, 5)

print(f"Time 0: 100.0")
print(f"Time 2: {amount_at_t2:.4f}")
print(f"Time 5: {amount_at_t5:.4f}")

Execution Result:

Time 0: 100.0
Time 2: 36.7879
Time 5: 8.2085

Using math.exp(-rate * time) allows you to easily calculate natural decay curves.

Practical Example: Implementing the Sigmoid Function

One of the most common situations where math.exp() is used is in the implementation of the activation function known as the “Sigmoid function” in AI (Neural Networks).

The formula is as follows:

σ(x)=1+e−x1​

Let’s translate this into Python code.

import math

def sigmoid(x):
    """Sigmoid function"""
    return 1 / (1 + math.exp(-x))

# --- Operation check ---
values = [-5, 0, 5]

print("--- Sigmoid function calculation results ---")
for v in values:
    result = sigmoid(v)
    print(f"x={v:>2} : {result:.4f}")

Execution Result:

--- Sigmoid function calculation results ---
x=-5 : 0.0067
x= 0 : 0.5000
x= 5 : 0.9933

You can confirm the characteristics of the Sigmoid function: it returns 0.5 when the input is 0, approaches 1 for large positive values, and approaches 0 for small negative values.

Difference from math.e ** x

math.exp(x) may seem to perform the same calculation as math.e ** x (the constant e raised to the power of x), but using math.exp(x) is recommended for the following reasons:

  1. Precision: math.exp(x) is implemented with an algorithm specialized for exponential functions, which can be more precise than standard power calculations.
  2. Readability: It corresponds to the mathematical notation exp(x), making the meaning of the code clearer.
x = 10

# Both give almost the same result, but math.exp is preferred as a specialized function
print(math.e ** x)
print(math.exp(x))

Summary

  • math.exp(x): Calculates Napier’s number e raised to the power of x (ex).
  • It is used in a wide range of fields, including physical simulations (decay, growth) and statistics/machine learning (Sigmoid function, normal distribution).
  • It is generally better to use the specialized function math.exp(x) rather than simple exponentiation math.e ** x.
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次