Table of Contents
A sigmoid function is a mathematical function that maps any input value to a value between 0 and 1. It is commonly used in machine learning and neural networks to introduce non-linearity into the models. In order to calculate a sigmoid function in Python, you can use the “sigmoid” function from the “scipy.special” module. This function takes in an array of input values and returns an array of corresponding sigmoid values. Some examples of using the sigmoid function in Python are:
1. Calculating the sigmoid value for a single input number:
import scipy.special
sigmoid = scipy.special.expit(5)
print(sigmoid)
Output: 0.9933071490757153
2. Calculating the sigmoid values for an array of input numbers:
import numpy as np
import scipy.special
input_array = np.array([2, 3, 4])
sigmoid = scipy.special.expit(input_array)
print(sigmoid)
Output: [0.88079708 0.95257413 0.98201379]
Overall, the sigmoid function in Python can be easily calculated using the “scipy.special” module and can be applied to both single input values and arrays of input values.
Calculate a Sigmoid Function in Python (With Examples)
A is a mathematical function that has an “S” shaped curve when plotted.
The most common example of a sigmoid function is the logistic sigmoid function, which is calculated as:
F(x) = 1 / (1 + e-x)
The easiest way to calculate a sigmoid function in Python is to use the function from the SciPy library, which uses the following basic syntax:
from scipy.specialimport expit #calculate sigmoid function for x = 2.5 expit(2.5)
The following examples show how to use this function in practice.
Example 1: Calculate Sigmoid Function for One Value
The following code shows how to calculate the sigmoid function for the value x = 2.5:
from scipy.specialimport expit #calculate sigmoid function for x = 2.5 expit(2.5) 0.9241418199787566
The value of the sigmoid function for x = 2.5 is 0.924.
We can confirm this by calculating the value manually:
- F(x) = 1 / (1 + e-x)
- F(x) = 1 / (1 + e-2.5)
- F(x) = 1 / (1 + .082)
- F(x) = 0.924
Example 2: Calculate Sigmoid Function for Multiple Values
The following code shows how to calculate the sigmoid function for multiple x values at once:
from scipy.specialimport expit
#define list of values
values = [-2, -1, 0, 1, 2]
#calculate sigmoid function for each value in list
expit(values)
array([0.11920292, 0.26894142, 0.5, 0.73105858, 0.88079708])
Example 3: Plot Sigmoid Function for Range of Values
The following code shows how to plot the values of a sigmoid function for a range of x values using :
import matplotlib.pyplotas plt
from scipy.specialimport expit
import numpy as np
#define range of x-values
x = np.linspace(-10, 10, 100)
#calculate sigmoid function for each x-value
y = expit(x)
#create plot
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('F(x)')
#display plot
plt.show()

Notice that the plot exhibits the “S” shaped curve that is characteristic of a sigmoid function.
Additional Resources
The following tutorials explain how to perform other common operations in Python:
Cite this article
stats writer (2024). How can I calculate a sigmoid function in Python? Can you provide some examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-calculate-a-sigmoid-function-in-python-can-you-provide-some-examples/
stats writer. "How can I calculate a sigmoid function in Python? Can you provide some examples?." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-i-calculate-a-sigmoid-function-in-python-can-you-provide-some-examples/.
stats writer. "How can I calculate a sigmoid function in Python? Can you provide some examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-calculate-a-sigmoid-function-in-python-can-you-provide-some-examples/.
stats writer (2024) 'How can I calculate a sigmoid function in Python? Can you provide some examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-calculate-a-sigmoid-function-in-python-can-you-provide-some-examples/.
[1] stats writer, "How can I calculate a sigmoid function in Python? Can you provide some examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.
stats writer. How can I calculate a sigmoid function in Python? Can you provide some examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
