27
27
# classification problems
28
28
29
29
30
- def sigmoid_function (z ) :
30
+ def sigmoid_function (z : float | np . ndarray ) -> float | np . ndarray :
31
31
"""
32
32
Also known as Logistic Function.
33
33
@@ -42,11 +42,63 @@ def sigmoid_function(z):
42
42
43
43
@param z: input to the function
44
44
@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])
45
61
"""
46
62
return 1 / (1 + np .exp (- z ))
47
63
48
64
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
+ """
50
102
return (- y * np .log (h ) - (1 - y ) * np .log (1 - h )).mean ()
51
103
52
104
@@ -75,6 +127,10 @@ def logistic_reg(alpha, x, y, max_iterations=70000):
75
127
# In[68]:
76
128
77
129
if __name__ == "__main__" :
130
+ import doctest
131
+
132
+ doctest .testmod ()
133
+
78
134
iris = datasets .load_iris ()
79
135
x = iris .data [:, :2 ]
80
136
y = (iris .target != 0 ) * 1
0 commit comments