Reputation: 17
I'm trying to graph the Sigmoid Function used in machine learning by using the Matplotlib library. My problem is that I haven't visualized a mathematical function before so I'm humbly asking for your guidance.
I've tried to directly plot the following function:
def Sigmoid(x):
a=[]
for i in x:
a.append(1/(1+math.exp(-i)))
return a
using the command plt.plot(Sigmoid)
.
But that gave me the error:
TypeError: float() argument must be a string or a number, not 'function'
The final result should look something like this:
Upvotes: 1
Views: 4279
Reputation: 25023
Sigmoid
is a function, Matplotlib expects numerical values, i.e., the results of a function evaluation, e.g.
x = [i/50 - 1 for i in range(101)]
plt.plot(x, Sigmoid(x))
That said, you probably want to familiarize with the Numpy library
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-1, 1, 101)
plt.plot(x, 1/(1+np.exp(-x))
Upvotes: 3
Reputation: 5460
import numpy as np
import matplotlib.pyplot as plt
def sigmoid(arr, scale=1):
arr = np.asarray(arr)
result = 1/(1 + np.exp(-arr*scale))
return result
x = np.linspace(-5, 5)
y = sigmoid(x)
fig, ax = plt.subplots()
ax.plot(x, y)
Result:
The ax.plot
method takes a pair of 1-D array-likes that are of the same length to create the lines. Matplotlib is not like Mathematica in which you can give an analytic function and a domain of its arguments. You have to give (in this case) x-y pairs (or rather, lists/arrays that can be turned into x-y pairs) And in this case, order matters.
Upvotes: 3