Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Calculate a Logistic Sigmoid Function in Python?

#1
How to Calculate a Logistic Sigmoid Function in Python?

<div><div class="kk-star-ratings kksr-valign-top kksr-align-left " data-payload="{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;386415&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;count&quot;:&quot;0&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;0\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;0&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;}">
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 0px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend"> <span class="kksr-muted">Rate this post</span> </div>
</div>
<p><strong>Summary:</strong> You can caculate the logistic sigmoid function in Python using:</p>
<ul class="has-global-color-8-background-color has-background">
<li>The Math Module: <code>1 / (1 + math.exp(-x))</code></li>
<li>The Numpy Library: <code>1 / (1 + np.exp(-x))</code></li>
<li>The Scipy Library: <code>scipy.special.expit(x)</code></li>
</ul>
<hr class="wp-block-separator" />
<p><strong>Problem: </strong>Given a logistic sigmoid function:</p>
<figure class="wp-block-image is-style-default"><img src="https://i.stack.imgur.com/SUuRi.png" alt="enter image description here" /></figure>
<p>If the value of <strong>x</strong> is given, how will you calculate <strong>F(x)</strong> in Python? Let’s say <code>x=0.458</code>.</p>
<p><strong>Note: </strong>Logistic sigmoid function is defined as <strong>(1/(1 + e^-x))</strong> 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 <strong>S-shaped </strong>curve when plotted on a graph. </p>
<h2>❒<strong>Method 1: Sigmoid Function in Python Using <a rel="noreferrer noopener" href="https://blog.finxter.com/python-math-module/" target="_blank">Math</a> Module</strong></h2>
<p><strong> Approach: </strong>Define a function that accepts <em>x</em> as an input and returns <em>F(x)</em> as <strong>1/(1 + math.exp(-x))</strong>.</p>
<p><strong>Code:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import math def sigmoid(x): return 1 / (1 + math.exp(-x)) print(sigmoid(0.458)) # OUTPUT: 0.6125396134409151</pre>
<p><strong>Caution: </strong>The above solution is mainly intended as a simple one-to-one translation of the given sigmoid expression into Python code. It is <em>not</em> 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.</p>
<p>Here’s a more stable implementation of the above solution:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import math def sigmoid(x): if x &gt;= 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))</pre>
<p><strong>Note:</strong> <code>exp()</code> is a method of the math module in Python that returns the value of <strong>E</strong> raised to the power of <strong>x</strong>. Here, <strong>x</strong> is the input value passed to the <strong>exp()</strong> function, while <strong>E</strong> represents the base of the natural system of the logarithm (approximately 2.718282).</p>
<h2>❒<strong>Method 2: Sigmoid Function in Python Using <a rel="noreferrer noopener" href="https://blog.finxter.com/numpy-tutorial/" target="_blank">Numpy</a></strong></h2>
<p>The sigmoid function can also be implemented using the <code>exp()</code> method of the Numpy module. <code>numpy.exp()</code> works just like the <code>math.exp()</code> method, with the additional advantage of being able to handle arrays along with integers and float values. </p>
<p>Let’s have a look at an example to visualize how to implement the sigmoid function using <code>numpy.exp()</code></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np def sigmoid(x): return 1 / (1 + np.exp(-x)) print(sigmoid(0.458)) # OUTPUT: 0.6125396134409151</pre>
<p>Probably a more numerically stable version of the above implementation is as follows:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np def sigmoid(x): return np.where(x &lt; 0, np.exp(x) / (1 + np.exp(x)), 1 / (1 + np.exp(-x))) print(sigmoid(0.458)) # OUTPUT: 0.6125396134409151</pre>
<p><strong>#Example 2:</strong> 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.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np
import matplotlib.pyplot as plt def sigmoid(x): return np.where(x &lt; 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()</pre>
<p><strong>Output:</strong></p>
<figure class="wp-block-image size-full is-style-default"><img loading="lazy" width="568" height="406" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-310.png" alt="" class="wp-image-387625" srcset="https://blog.finxter.com/wp-content/uploads/2022/05/image-310.png 568w, https://blog.finxter.com/wp-content/uplo...00x214.png 300w" sizes="(max-width: 568px) 100vw, 568px" /></figure>
<p><strong>Explanation:</strong></p>
<ul>
<li>Initially, we created an array of evenly spaced values within the range of -10 and 10 with the help of the <code>linspace</code> method of the Numpy module, i.e., <strong>val</strong>.</li>
<li>We then used the sigmoid function on these values. If you print them out, you will find that they are either extremely close to 0 or very close to 1. This can also be visualized once the graph is plotted.</li>
<li>Finally, we plotted the sigmoid function graph that we previously computed with the help of the function. The <em>x-axis </em>maps the values contained in <strong>val, </strong>while the <em>y-axis </em>maps the values returned by the sigmoid function.</li>
</ul>
<hr class="wp-block-separator" />
<p><strong>Do you want to become a NumPy master?</strong> Check out our interactive puzzle book <a href="https://amzn.to/39dEykm" target="_blank" rel="noreferrer noopener" title="https://amzn.to/39dEykm"><strong>Coffee Break NumPy</strong></a> and boost your data science skills! <em>(Amazon link opens in new tab.)</em></p>
<div class="wp-block-image">
<figure class="aligncenter size-medium"><a href="https://amzn.to/39dEykm" target="_blank" rel="noopener noreferrer"><img loading="lazy" width="200" height="300" src="https://blog.finxter.com/wp-content/uploads/2019/04/Cover_Coffee_Break_NumPy-200x300.jpg" alt="Coffee Break NumPy" class="wp-image-2766"/></a></figure>
</div>
<h2>❒<strong>Method 3: Sigmoid Function in Python Using the <a href="https://blog.finxter.com/best-10-scipy-cheat-sheets/" target="_blank" rel="noreferrer noopener">Scipy</a> Library</strong></h2>
<p>Another efficient way to calculate the sigmoid function in Python is to use the <strong>Scipy</strong> libraries <code>expit</code> function. </p>
<p><strong>Example 1: Calculating logistic sigmoid for a given value</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from scipy.special import expit
print(expit(0.458)) # OUTPUT: 0.6125396134409151</pre>
<p><strong>Example 2: Calculating logistic sigmoid for multiple values</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from scipy.special import expit
x = [-2, -1, 0, 1, 2]
for value in expit(x): print(value)</pre>
<p><strong>Output:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">0.11920292202211755
0.2689414213699951
0.5
0.7310585786300049
0.8807970779778823</pre>
<p class="has-background" style="background-color:#edc6f6"><strong>Recommended Read: <a href="https://blog.finxter.com/logistic-regression-in-one-line-python/" target="_blank" rel="noreferrer noopener">Logistic Regression in Python Scikit-Learn</a></strong></p>
<h2>❒<strong>Method 4: Transform the tanh function </strong></h2>
<p>Another workaround to compute the sigmoid function is to transform the tanh function of the math module as shown below:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import math sigmoid = lambda x: .5 * (math.tanh(.5 * x) + 1)
print(sigmoid(0.458)) # OUTPUT: 0.6125396134409151</pre>
<p>Since, mathematically <code>sigmoid(x) == (1 + tanh(x/2))/2</code>. Hence, the above implementation should work and is a valid solution. However, the methods mentioned earlier are undoubtedly more stable numerically and superior to this solution. </p>
<h2><strong>Conclusion</strong></h2>
<p>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. </p>
<p>I hope this article has helped you. Please <strong><a rel="noreferrer noopener" href="https://blog.finxter.com/subscribe/" target="_blank">subscribe</a></strong> and stay tuned for more interesting solutions and tutorials. Happy learning!</p>
<hr class="wp-block-separator" />
<p><strong><a href="https://academy.finxter.com/university/tensorflow/" target="_blank" rel="noreferrer noopener" title="https://academy.finxter.com/university/tensorflow/">TensorFlow – A Hands-On Introduction to Deep Learning and Neural Networks for Beginners</a></strong></p>
<p>This course gives you a charming introduction into deep learning and neural networks using Google’s TensorFlow library for Python beginners.</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><a href="https://academy.finxter.com/university/tensorflow/" target="_blank" rel="noopener"><img loading="lazy" width="363" height="650" src="https://blog.finxter.com/wp-content/uploads/2021/09/image-37.png" alt="" class="wp-image-35113" srcset="https://blog.finxter.com/wp-content/uploads/2021/09/image-37.png 363w, https://blog.finxter.com/wp-content/uplo...68x300.png 168w" sizes="(max-width: 363px) 100vw, 363px" /></a></figure>
</div>
</div>


https://www.sickgaming.net/blog/2022/05/...in-python/
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016