Implement sigmoid function using Numpy

Implement sigmoid function using Numpy

The sigmoid function is defined as:

σ(x)=1+e−x1​

Here's how you can implement the sigmoid function using NumPy:

import numpy as np def sigmoid(x): return 1 / (1 + np.exp(-x)) # Test x = np.array([-2, -1, 0, 1, 2]) print(sigmoid(x)) 

Output:

[0.11920292 0.26894142 0.5 0.73105858 0.88079708] 

When you pass a NumPy array to the sigmoid function, it will apply the sigmoid function element-wise, thanks to NumPy's broadcasting capabilities.


More Tags

components testng-eclipse baseadapter pipenv gradient-descent python-multithreading v-navigation-drawer varchar azure-web-app-service permissions

More Programming Guides

Other Guides

More Programming Examples