Skip to content

Commit 611418c

Browse files
machine_learning: add numeric doctests to gradient_descent (#15286)
1 parent 12d0648 commit 611418c

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

machine_learning/gradient_descent.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ def _error(example_no, data_set="train"):
2424
:param data_set: train data or test data
2525
:param example_no: example number whose error has to be checked
2626
:return: error in example pointed by example number.
27+
28+
>>> _error(0) # hypothesis 39 minus train output 15
29+
24
2730
"""
2831
return calculate_hypothesis_value(example_no, data_set) - output(
2932
example_no, data_set
@@ -38,6 +41,9 @@ def _hypothesis_value(data_input_tuple):
3841
Note that there is an 'biased input' whose value is fixed as 1.
3942
It is not explicitly mentioned in input data.. But, ML hypothesis functions use it.
4043
So, we have to take care of it separately. Line 36 takes care of it.
44+
45+
>>> _hypothesis_value((5, 2, 3)) # 5*4 + 2*1 + 3*5 + bias 2
46+
39
4147
"""
4248
hyp_val = 0
4349
for i in range(len(parameter_vector) - 1):
@@ -51,6 +57,13 @@ def output(example_no, data_set):
5157
:param data_set: test data or train data
5258
:param example_no: example whose output is to be fetched
5359
:return: output for that example
60+
61+
>>> output(0, "train")
62+
15
63+
>>> output(1, "test")
64+
150
65+
>>> output(0, "unknown") is None
66+
True
5467
"""
5568
if data_set == "train":
5669
return train_data[example_no][1]
@@ -65,6 +78,13 @@ def calculate_hypothesis_value(example_no, data_set):
6578
:param data_set: test data or train_data
6679
:param example_no: example whose hypothesis value is to be calculated
6780
:return: hypothesis value for that example
81+
82+
>>> calculate_hypothesis_value(0, "train")
83+
39
84+
>>> calculate_hypothesis_value(0, "test")
85+
2149
86+
>>> calculate_hypothesis_value(0, "unknown") is None
87+
True
6888
"""
6989
if data_set == "train":
7090
return _hypothesis_value(train_data[example_no][0])
@@ -81,6 +101,11 @@ def summation_of_cost_derivative(index, end=m):
81101
:return: Returns the summation of cost derivative
82102
Note: If index is -1, this means we are calculating summation wrt to biased
83103
parameter.
104+
105+
>>> summation_of_cost_derivative(-1) # sum of errors over all examples
106+
243
107+
>>> summation_of_cost_derivative(0)
108+
2234
84109
"""
85110
summation_value = 0
86111
for i in range(end):
@@ -97,6 +122,11 @@ def get_cost_derivative(index):
97122
:return: derivative wrt to that index
98123
Note: If index is -1, this means we are calculating summation wrt to biased
99124
parameter.
125+
126+
>>> get_cost_derivative(-1) # 243 / 5 examples
127+
48.6
128+
>>> get_cost_derivative(0) # 2234 / 5 examples
129+
446.8
100130
"""
101131
cost_derivative_value = summation_of_cost_derivative(index, m) / m
102132
return cost_derivative_value

0 commit comments

Comments
 (0)