![]() |
|
[Tut] How to Calculate a Logistic Sigmoid Function in Python? - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Calculate a Logistic Sigmoid Function in Python? (/thread-99483.html) |
[Tut] How to Calculate a Logistic Sigmoid Function in Python? - xSicKxBot - 05-31-2022 How to Calculate a Logistic Sigmoid Function in Python? Rate this post Summary: You can caculate the logistic sigmoid function in Python using:
Problem: Given a logistic sigmoid function: ![]() If the value of x is given, how will you calculate F(x) in Python? Let’s say Code: x=0.458Note: Logistic sigmoid function is defined as (1/(1 + e^-x)) where x is the input variable and represents any real number. The function returns a value that lies within the range -1 and 1. It forms an S-shaped curve when plotted on a graph. ❒[b]Method 1: Sigmoid Function in Python Using Math Module[/b] Approach: Define a function that accepts x as an input and returns F(x) as 1/(1 + math.exp(-x)). Code: Code: import math def sigmoid(x): return 1 / (1 + math.exp(-x)) print(sigmoid(0.458)) # OUTPUT: 0.6125396134409151Caution: The above solution is mainly intended as a simple one-to-one translation of the given sigmoid expression into Python code. It is not strictly tested or considered to be a perfect and numerically sound implementation. In case you need a more robust implementation, some of the solutions to follow might prove to be more instrumental in solving your case. Here’s a more stable implementation of the above solution: Code: import math def sigmoid(x): if x >= 0: k = math.exp(-x) res = 1 / (1 + k) return res else: k = math.exp(x) res = k / (1 + k) return res print(sigmoid(0.458))Note: Code: exp()❒[b]Method 2: Sigmoid Function in Python Using Numpy[/b] The sigmoid function can also be implemented using the Code: exp()Code: numpy.exp()Code: math.exp()Let’s have a look at an example to visualize how to implement the sigmoid function using Code: numpy.exp()Code: import numpy as np def sigmoid(x): return 1 / (1 + np.exp(-x)) print(sigmoid(0.458)) # OUTPUT: 0.6125396134409151Probably a more numerically stable version of the above implementation is as follows: Code: import numpy as np def sigmoid(x): return np.where(x < 0, np.exp(x) / (1 + np.exp(x)), 1 / (1 + np.exp(-x))) print(sigmoid(0.458)) # OUTPUT: 0.6125396134409151#Example 2: Let’s have a look at an implementation of the sigmoid function upon an array of evenly spaced values with the help of a graph in the following example. Code: import numpy as np
import matplotlib.pyplot as plt def sigmoid(x): return np.where(x < 0, np.exp(x) / (1 + np.exp(x)), 1 / (1 + np.exp(-x))) val = np.linspace(start=-10, stop=10, num=200)
sigmoid_values = sigmoid(val)
plt.plot(val, sigmoid_values)
plt.xlabel("x")
plt.ylabel("sigmoid(X)")
plt.show()Output: ![]() Explanation:
Do you want to become a NumPy master? Check out our interactive puzzle book Coffee Break NumPy and boost your data science skills! (Amazon link opens in new tab.) ![]() ❒[b]Method 3: Sigmoid Function in Python Using the Scipy Library[/b] Another efficient way to calculate the sigmoid function in Python is to use the Scipy libraries Code: expitExample 1: Calculating logistic sigmoid for a given value Code: from scipy.special import expit
print(expit(0.458)) # OUTPUT: 0.6125396134409151Example 2: Calculating logistic sigmoid for multiple values Code: from scipy.special import expit
x = [-2, -1, 0, 1, 2]
for value in expit(x): print(value)Output: Code: 0.11920292202211755
0.2689414213699951
0.5
0.7310585786300049
0.8807970779778823Recommended Read: Logistic Regression in Python Scikit-Learn ❒[b]Method 4: Transform the tanh function [/b] Another workaround to compute the sigmoid function is to transform the tanh function of the math module as shown below: Code: import math sigmoid = lambda x: .5 * (math.tanh(.5 * x) + 1)
print(sigmoid(0.458)) # OUTPUT: 0.6125396134409151Since, mathematically Code: sigmoid(x) == (1 + tanh(x/2))/2[b]Conclusion[/b] Well, that’s it for this tutorial. We have discussed as many as four ways of calculating the logistic sigmoid function in Python. Feel free to use the one that suits your requirements. I hope this article has helped you. Please subscribe and stay tuned for more interesting solutions and tutorials. Happy learning! TensorFlow – A Hands-On Introduction to Deep Learning and Neural Networks for Beginners This course gives you a charming introduction into deep learning and neural networks using Google’s TensorFlow library for Python beginners.
|