mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
psf/black code formatting (#1277)
This commit is contained in:
committed by
Christian Clauss
parent
07f04a2e55
commit
9eac17a408
@@ -4,21 +4,28 @@ Implementation of gradient descent algorithm for minimizing cost of a linear hyp
|
||||
import numpy
|
||||
|
||||
# List of input, output pairs
|
||||
train_data = (((5, 2, 3), 15), ((6, 5, 9), 25),
|
||||
((11, 12, 13), 41), ((1, 1, 1), 8), ((11, 12, 13), 41))
|
||||
train_data = (
|
||||
((5, 2, 3), 15),
|
||||
((6, 5, 9), 25),
|
||||
((11, 12, 13), 41),
|
||||
((1, 1, 1), 8),
|
||||
((11, 12, 13), 41),
|
||||
)
|
||||
test_data = (((515, 22, 13), 555), ((61, 35, 49), 150))
|
||||
parameter_vector = [2, 4, 1, 5]
|
||||
m = len(train_data)
|
||||
LEARNING_RATE = 0.009
|
||||
|
||||
|
||||
def _error(example_no, data_set='train'):
|
||||
def _error(example_no, data_set="train"):
|
||||
"""
|
||||
:param data_set: train data or test data
|
||||
:param example_no: example number whose error has to be checked
|
||||
:return: error in example pointed by example number.
|
||||
"""
|
||||
return calculate_hypothesis_value(example_no, data_set) - output(example_no, data_set)
|
||||
return calculate_hypothesis_value(example_no, data_set) - output(
|
||||
example_no, data_set
|
||||
)
|
||||
|
||||
|
||||
def _hypothesis_value(data_input_tuple):
|
||||
@@ -32,7 +39,7 @@ def _hypothesis_value(data_input_tuple):
|
||||
"""
|
||||
hyp_val = 0
|
||||
for i in range(len(parameter_vector) - 1):
|
||||
hyp_val += data_input_tuple[i]*parameter_vector[i+1]
|
||||
hyp_val += data_input_tuple[i] * parameter_vector[i + 1]
|
||||
hyp_val += parameter_vector[0]
|
||||
return hyp_val
|
||||
|
||||
@@ -43,9 +50,9 @@ def output(example_no, data_set):
|
||||
:param example_no: example whose output is to be fetched
|
||||
:return: output for that example
|
||||
"""
|
||||
if data_set == 'train':
|
||||
if data_set == "train":
|
||||
return train_data[example_no][1]
|
||||
elif data_set == 'test':
|
||||
elif data_set == "test":
|
||||
return test_data[example_no][1]
|
||||
|
||||
|
||||
@@ -75,7 +82,7 @@ def summation_of_cost_derivative(index, end=m):
|
||||
if index == -1:
|
||||
summation_value += _error(i)
|
||||
else:
|
||||
summation_value += _error(i)*train_data[i][0][index]
|
||||
summation_value += _error(i) * train_data[i][0][index]
|
||||
return summation_value
|
||||
|
||||
|
||||
@@ -85,7 +92,7 @@ def get_cost_derivative(index):
|
||||
:return: derivative wrt to that index
|
||||
Note: If index is -1, this means we are calculating summation wrt to biased parameter.
|
||||
"""
|
||||
cost_derivative_value = summation_of_cost_derivative(index, m)/m
|
||||
cost_derivative_value = summation_of_cost_derivative(index, m) / m
|
||||
return cost_derivative_value
|
||||
|
||||
|
||||
@@ -99,11 +106,16 @@ def run_gradient_descent():
|
||||
j += 1
|
||||
temp_parameter_vector = [0, 0, 0, 0]
|
||||
for i in range(0, len(parameter_vector)):
|
||||
cost_derivative = get_cost_derivative(i-1)
|
||||
temp_parameter_vector[i] = parameter_vector[i] - \
|
||||
LEARNING_RATE*cost_derivative
|
||||
if numpy.allclose(parameter_vector, temp_parameter_vector,
|
||||
atol=absolute_error_limit, rtol=relative_error_limit):
|
||||
cost_derivative = get_cost_derivative(i - 1)
|
||||
temp_parameter_vector[i] = (
|
||||
parameter_vector[i] - LEARNING_RATE * cost_derivative
|
||||
)
|
||||
if numpy.allclose(
|
||||
parameter_vector,
|
||||
temp_parameter_vector,
|
||||
atol=absolute_error_limit,
|
||||
rtol=relative_error_limit,
|
||||
):
|
||||
break
|
||||
parameter_vector = temp_parameter_vector
|
||||
print(("Number of iterations:", j))
|
||||
@@ -111,11 +123,11 @@ def run_gradient_descent():
|
||||
|
||||
def test_gradient_descent():
|
||||
for i in range(len(test_data)):
|
||||
print(("Actual output value:", output(i, 'test')))
|
||||
print(("Hypothesis output:", calculate_hypothesis_value(i, 'test')))
|
||||
print(("Actual output value:", output(i, "test")))
|
||||
print(("Hypothesis output:", calculate_hypothesis_value(i, "test")))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
run_gradient_descent()
|
||||
print("\nTesting gradient descent for a linear hypothesis function.\n")
|
||||
test_gradient_descent()
|
||||
|
||||
Reference in New Issue
Block a user