Skip to content

Commit dd7d18d

Browse files
Added doctest, docstring and typehint for sigmoid_function & cost_function (TheAlgorithms#10828)
* Added doctest for sigmoid_function & cost_function * Update logistic_regression.py * Update logistic_regression.py * Minor formatting changes in doctests * Apply suggestions from code review * Made requested changes in logistic_regression.py * Apply suggestions from code review --------- Co-authored-by: Tianyi Zheng <[email protected]>
1 parent c71c280 commit dd7d18d

File tree

1 file changed

+58
-2
lines changed

1 file changed

+58
-2
lines changed

machine_learning/logistic_regression.py

+58-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
# classification problems
2828

2929

30-
def sigmoid_function(z):
30+
def sigmoid_function(z: float | np.ndarray) -> float | np.ndarray:
3131
"""
3232
Also known as Logistic Function.
3333
@@ -42,11 +42,63 @@ def sigmoid_function(z):
4242
4343
@param z: input to the function
4444
@returns: returns value in the range 0 to 1
45+
46+
Examples:
47+
>>> sigmoid_function(4)
48+
0.9820137900379085
49+
>>> sigmoid_function(np.array([-3, 3]))
50+
array([0.04742587, 0.95257413])
51+
>>> sigmoid_function(np.array([-3, 3, 1]))
52+
array([0.04742587, 0.95257413, 0.73105858])
53+
>>> sigmoid_function(np.array([-0.01, -2, -1.9]))
54+
array([0.49750002, 0.11920292, 0.13010847])
55+
>>> sigmoid_function(np.array([-1.3, 5.3, 12]))
56+
array([0.21416502, 0.9950332 , 0.99999386])
57+
>>> sigmoid_function(np.array([0.01, 0.02, 4.1]))
58+
array([0.50249998, 0.50499983, 0.9836975 ])
59+
>>> sigmoid_function(np.array([0.8]))
60+
array([0.68997448])
4561
"""
4662
return 1 / (1 + np.exp(-z))
4763

4864

49-
def cost_function(h, y):
65+
def cost_function(h: np.ndarray, y: np.ndarray) -> float:
66+
"""
67+
Cost function quantifies the error between predicted and expected values.
68+
The cost function used in Logistic Regression is called Log Loss
69+
or Cross Entropy Function.
70+
71+
J(θ) = (1/m) * Σ [ -y * log(hθ(x)) - (1 - y) * log(1 - hθ(x)) ]
72+
73+
Where:
74+
- J(θ) is the cost that we want to minimize during training
75+
- m is the number of training examples
76+
- Σ represents the summation over all training examples
77+
- y is the actual binary label (0 or 1) for a given example
78+
- hθ(x) is the predicted probability that x belongs to the positive class
79+
80+
@param h: the output of sigmoid function. It is the estimated probability
81+
that the input example 'x' belongs to the positive class
82+
83+
@param y: the actual binary label associated with input example 'x'
84+
85+
Examples:
86+
>>> estimations = sigmoid_function(np.array([0.3, -4.3, 8.1]))
87+
>>> cost_function(h=estimations,y=np.array([1, 0, 1]))
88+
0.18937868932131605
89+
>>> estimations = sigmoid_function(np.array([4, 3, 1]))
90+
>>> cost_function(h=estimations,y=np.array([1, 0, 0]))
91+
1.459999655669926
92+
>>> estimations = sigmoid_function(np.array([4, -3, -1]))
93+
>>> cost_function(h=estimations,y=np.array([1,0,0]))
94+
0.1266663223365915
95+
>>> estimations = sigmoid_function(0)
96+
>>> cost_function(h=estimations,y=np.array([1]))
97+
0.6931471805599453
98+
99+
References:
100+
- https://en.wikipedia.org/wiki/Logistic_regression
101+
"""
50102
return (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean()
51103

52104

@@ -75,6 +127,10 @@ def logistic_reg(alpha, x, y, max_iterations=70000):
75127
# In[68]:
76128

77129
if __name__ == "__main__":
130+
import doctest
131+
132+
doctest.testmod()
133+
78134
iris = datasets.load_iris()
79135
x = iris.data[:, :2]
80136
y = (iris.target != 0) * 1

0 commit comments

Comments
 (0)