{"id":125099,"date":"2022-05-24T20:25:40","date_gmt":"2022-05-24T20:25:40","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=386415"},"modified":"2022-05-24T20:25:40","modified_gmt":"2022-05-24T20:25:40","slug":"how-to-calculate-a-logistic-sigmoid-function-in-python","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/05\/24\/how-to-calculate-a-logistic-sigmoid-function-in-python\/","title":{"rendered":"How to Calculate a Logistic Sigmoid Function in Python?"},"content":{"rendered":"<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;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 0px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\"> <span class=\"kksr-muted\">Rate this post<\/span> <\/div>\n<\/div>\n<p><strong>Summary:<\/strong> You can caculate the logistic sigmoid function in Python using:<\/p>\n<ul class=\"has-global-color-8-background-color has-background\">\n<li>The Math Module: <code>1 \/ (1 + math.exp(-x))<\/code><\/li>\n<li>The Numpy Library: <code>1 \/ (1 + np.exp(-x))<\/code><\/li>\n<li>The Scipy Library: <code>scipy.special.expit(x)<\/code><\/li>\n<\/ul>\n<hr class=\"wp-block-separator\" \/>\n<p><strong>Problem: <\/strong>Given a logistic sigmoid function:<\/p>\n<figure class=\"wp-block-image is-style-default\"><img decoding=\"async\" src=\"https:\/\/i.stack.imgur.com\/SUuRi.png\" alt=\"enter image description here\" \/><\/figure>\n<p>If the value of <strong>x<\/strong> is given, how will you calculate <strong>F(x)<\/strong> in Python? Let&#8217;s say <code>x=0.458<\/code>.<\/p>\n<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>\n<h2>\u2752<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>\n<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>\n<p><strong>Code:<\/strong><\/p>\n<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>\n<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\u00a0<em>not<\/em> strictly\u00a0tested 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>\n<p>Here&#8217;s a more stable implementation of the above solution:<\/p>\n<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>\n<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>\n<h2>\u2752<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>\n<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>\n<p>Let&#8217;s have a look at an example to visualize how to implement the sigmoid function using <code>numpy.exp()<\/code><\/p>\n<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>\n<p>Probably a more numerically stable version of the above implementation is as follows:<\/p>\n<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>\n<p><strong>#Example 2:<\/strong> Let&#8217;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>\n<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\nimport 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)\nsigmoid_values = sigmoid(val)\nplt.plot(val, sigmoid_values)\nplt.xlabel(\"x\")\nplt.ylabel(\"sigmoid(X)\")\nplt.show()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<figure class=\"wp-block-image size-full is-style-default\"><img decoding=\"async\" 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\/uploads\/2022\/05\/image-310-300x214.png 300w\" sizes=\"auto, (max-width: 568px) 100vw, 568px\" \/><\/figure>\n<p><strong>Explanation:<\/strong><\/p>\n<ul>\n<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>\n<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>\n<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>\n<\/ul>\n<hr class=\"wp-block-separator\" \/>\n<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>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-medium\"><a href=\"https:\/\/amzn.to\/39dEykm\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" 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>\n<\/div>\n<h2>\u2752<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>\n<p>Another efficient way to calculate the sigmoid function in Python is to use the <strong>Scipy<\/strong> libraries <code>expit<\/code> function. <\/p>\n<p><strong>Example 1: Calculating logistic sigmoid for a given value<\/strong><\/p>\n<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\nprint(expit(0.458)) # OUTPUT: 0.6125396134409151<\/pre>\n<p><strong>Example 2: Calculating logistic sigmoid for multiple values<\/strong><\/p>\n<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\nx = [-2, -1, 0, 1, 2]\nfor value in expit(x): print(value)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<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\n0.2689414213699951\n0.5\n0.7310585786300049\n0.8807970779778823<\/pre>\n<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>\n<h2>\u2752<strong>Method 4: Transform the tanh function <\/strong><\/h2>\n<p>Another workaround to compute the sigmoid function is to transform the tanh function of the math module as shown below:<\/p>\n<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)\nprint(sigmoid(0.458)) # OUTPUT: 0.6125396134409151<\/pre>\n<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>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>Well, that&#8217;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>\n<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>\n<hr class=\"wp-block-separator\" \/>\n<p><strong><a href=\"https:\/\/academy.finxter.com\/university\/tensorflow\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/academy.finxter.com\/university\/tensorflow\/\">TensorFlow &#8211; A Hands-On Introduction to Deep Learning and Neural Networks for Beginners<\/a><\/strong><\/p>\n<p>This course gives you a charming introduction into deep learning and neural networks using Google&#8217;s TensorFlow library for Python beginners.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/academy.finxter.com\/university\/tensorflow\/\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" 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\/uploads\/2021\/09\/image-37-168x300.png 168w\" sizes=\"auto, (max-width: 363px) 100vw, 363px\" \/><\/a><\/figure>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Rate this post Summary: You can caculate the logistic sigmoid function in Python using: The Math Module: 1 \/ (1 + math.exp(-x)) The Numpy Library: 1 \/ (1 + np.exp(-x)) The Scipy Library: scipy.special.expit(x) Problem: Given a logistic sigmoid function: If the value of x is given, how will you calculate F(x) in Python? Let&#8217;s [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-125099","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/125099","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=125099"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/125099\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=125099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=125099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=125099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}