Tutorial to Visualize Sigmoid Function

Let us visualize the Sigmoid Function. We know the function for it and the range of the output.

So, let's get started.

In [2]:
# Import Dependencies

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

Now, let's create a dummy data. I will be using Numpy's inbuilt function with following format

np.arange(start, stop, step_size)

to generate input data. So, let's do it.

In [3]:
# Generate dummy data

X = np.arange(-10.0, 10.0, 0.2)
print('X: ',X)
X:  [ -1.00000000e+01  -9.80000000e+00  -9.60000000e+00  -9.40000000e+00
  -9.20000000e+00  -9.00000000e+00  -8.80000000e+00  -8.60000000e+00
  -8.40000000e+00  -8.20000000e+00  -8.00000000e+00  -7.80000000e+00
  -7.60000000e+00  -7.40000000e+00  -7.20000000e+00  -7.00000000e+00
  -6.80000000e+00  -6.60000000e+00  -6.40000000e+00  -6.20000000e+00
  -6.00000000e+00  -5.80000000e+00  -5.60000000e+00  -5.40000000e+00
  -5.20000000e+00  -5.00000000e+00  -4.80000000e+00  -4.60000000e+00
  -4.40000000e+00  -4.20000000e+00  -4.00000000e+00  -3.80000000e+00
  -3.60000000e+00  -3.40000000e+00  -3.20000000e+00  -3.00000000e+00
  -2.80000000e+00  -2.60000000e+00  -2.40000000e+00  -2.20000000e+00
  -2.00000000e+00  -1.80000000e+00  -1.60000000e+00  -1.40000000e+00
  -1.20000000e+00  -1.00000000e+00  -8.00000000e-01  -6.00000000e-01
  -4.00000000e-01  -2.00000000e-01  -3.55271368e-14   2.00000000e-01
   4.00000000e-01   6.00000000e-01   8.00000000e-01   1.00000000e+00
   1.20000000e+00   1.40000000e+00   1.60000000e+00   1.80000000e+00
   2.00000000e+00   2.20000000e+00   2.40000000e+00   2.60000000e+00
   2.80000000e+00   3.00000000e+00   3.20000000e+00   3.40000000e+00
   3.60000000e+00   3.80000000e+00   4.00000000e+00   4.20000000e+00
   4.40000000e+00   4.60000000e+00   4.80000000e+00   5.00000000e+00
   5.20000000e+00   5.40000000e+00   5.60000000e+00   5.80000000e+00
   6.00000000e+00   6.20000000e+00   6.40000000e+00   6.60000000e+00
   6.80000000e+00   7.00000000e+00   7.20000000e+00   7.40000000e+00
   7.60000000e+00   7.80000000e+00   8.00000000e+00   8.20000000e+00
   8.40000000e+00   8.60000000e+00   8.80000000e+00   9.00000000e+00
   9.20000000e+00   9.40000000e+00   9.60000000e+00   9.80000000e+00]

So, we got our values in the range -10 and +10 with a step size of 0.2. Now to define the sigmoid function.

In [4]:
# Sigmoid Function
# sigmoid(x) = 1 / (1 + exp(-x))

def sigmoid(x):
    return (1.0/(1 + np.exp(-x)))

So, we are all done. Let's plot our sigmoid function.

In [5]:
f_X = sigmoid(X)
In [8]:
# Plot Sigmoid Function

fig,ax = plt.subplots(figsize = (10,8))
ax.plot(X,f_X)
ax.set_xlabel('Z')
ax.set_ylabel('label = 0 or 1')
ax.set_title('Sigmoid Function')
Out[8]:
<matplotlib.text.Text at 0x22fedb7f320>

So, we can clearly see that for any values of input, this function converts the value to the range of 0 and 1.