From 78a0f61b1912206d7bfdc3f0604c1fba60bb605f Mon Sep 17 00:00:00 2001 From: sukyung99 <44187128+sukyung99@users.noreply.github.com> Date: Sun, 15 Nov 2020 12:44:40 +0900 Subject: [PATCH] Add Maths / Sigmoid Function (#3880) * Add Maths / Sigmoid Function * Update Sigmoid Function * Add doctest and type hints * Fix Trim Trailing Whitespace * Fix Error * Modified Black * Update sigmoid.py Co-authored-by: sukyung99 Co-authored-by: Christian Clauss --- maths/sigmoid.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 maths/sigmoid.py diff --git a/maths/sigmoid.py b/maths/sigmoid.py new file mode 100644 index 000000000..147588e88 --- /dev/null +++ b/maths/sigmoid.py @@ -0,0 +1,39 @@ +""" +This script demonstrates the implementation of the Sigmoid function. + +The function takes a vector of K real numbers as input and then 1 / (1 + exp(-x)). +After through Sigmoid, the element of the vector mostly 0 between 1. or 1 between -1. + +Script inspired from its corresponding Wikipedia article +https://en.wikipedia.org/wiki/Sigmoid_function +""" + +import numpy as np + + +def sigmoid(vector: np.array) -> np.array: + """ + Implements the sigmoid function + + Parameters: + vector (np.array): A numpy array of shape (1,n) + consisting of real values + + Returns: + sigmoid_vec (np.array): The input numpy array, after applying + sigmoid. + + Examples: + >>> sigmoid(np.array([-1.0, 1.0, 2.0])) + array([0.26894142, 0.73105858, 0.88079708]) + + >>> sigmoid(np.array([0.0])) + array([0.5]) + """ + return 1 / (1 + np.exp(-vector)) + + +if __name__ == "__main__": + import doctest + + doctest.testmod()