Skip to content

Commit a194b38

Browse files
committed
style: auto-format gradient_descent.py via pre-commit
1 parent b928fb8 commit a194b38

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

machine_learning/gradient_descent.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"""
55

66
import numpy as np
7-
from typing import Tuple, List, Union
87

98
# List of input, output pairs
109
train_data = (
@@ -15,7 +14,7 @@
1514
((11, 12, 13), 41),
1615
)
1716
test_data = (((515, 22, 13), 555), ((61, 35, 49), 150))
18-
parameter_vector = [2, 4, 1, 5]
17+
parameter_vector = [2.0, 4.0, 1.0, 5.0]
1918
m = len(train_data)
2019
LEARNING_RATE = 0.009
2120

@@ -31,7 +30,7 @@ def _error(example_no: int, data_set: str = "train") -> float:
3130
)
3231

3332

34-
def _hypothesis_value(data_input_tuple: Tuple[float, ...]) -> float:
33+
def _hypothesis_value(data_input_tuple: tuple[float, ...]) -> float:
3534
"""
3635
Calculates hypothesis function value for a given input
3736
:param data_input_tuple: Input tuple of a particular example
@@ -40,7 +39,7 @@ def _hypothesis_value(data_input_tuple: Tuple[float, ...]) -> float:
4039
It is not explicitly mentioned in input data.. But, ML hypothesis functions use it.
4140
So, we have to take care of it separately. Line 36 takes care of it.
4241
"""
43-
hyp_val = 0
42+
hyp_val = 0.0
4443
for i in range(len(parameter_vector) - 1):
4544
hyp_val += data_input_tuple[i] * parameter_vector[i + 1]
4645
hyp_val += parameter_vector[0]
@@ -57,7 +56,7 @@ def output(example_no: int, data_set: str) -> float:
5756
return train_data[example_no][1]
5857
elif data_set == "test":
5958
return test_data[example_no][1]
60-
return None
59+
raise ValueError(f"Unknown data_set: {data_set}")
6160

6261

6362
def calculate_hypothesis_value(example_no: int, data_set: str) -> float:
@@ -71,7 +70,7 @@ def calculate_hypothesis_value(example_no: int, data_set: str) -> float:
7170
return _hypothesis_value(train_data[example_no][0])
7271
elif data_set == "test":
7372
return _hypothesis_value(test_data[example_no][0])
74-
return None
73+
raise ValueError(f"Unknown data_set: {data_set}")
7574

7675

7776
def summation_of_cost_derivative(index: int, end: int = m) -> float:
@@ -83,7 +82,7 @@ def summation_of_cost_derivative(index: int, end: int = m) -> float:
8382
Note: If index is -1, this means we are calculating summation wrt to biased
8483
parameter.
8584
"""
86-
summation_value = 0
85+
summation_value = 0.0
8786
for i in range(end):
8887
if index == -1:
8988
summation_value += _error(i)
@@ -111,7 +110,7 @@ def run_gradient_descent() -> None:
111110
j = 0
112111
while True:
113112
j += 1
114-
temp_parameter_vector = [0, 0, 0, 0]
113+
temp_parameter_vector = [0.0, 0.0, 0.0, 0.0]
115114
for i in range(len(parameter_vector)):
116115
cost_derivative = get_cost_derivative(i - 1)
117116
temp_parameter_vector[i] = (
@@ -142,11 +141,11 @@ def run_gradient_descent_vectorized() -> None:
142141
global parameter_vector
143142

144143
# Convert training data into NumPy arrays
145-
X = np.array([x for x, _ in train_data])
144+
x_train = np.array([x for x, _ in train_data])
146145
y = np.array([y for _, y in train_data])
147146

148147
# Add bias term (column of ones)
149-
X = np.hstack((np.ones((X.shape[0], 1)), X))
148+
x_train = np.hstack((np.ones((x_train.shape[0], 1)), x_train))
150149

151150
# Convert parameter vector to NumPy array
152151
theta = np.array(parameter_vector, dtype=float)
@@ -159,13 +158,13 @@ def run_gradient_descent_vectorized() -> None:
159158
j += 1
160159

161160
# Compute predictions
162-
predictions = X @ theta
161+
predictions = x_train @ theta
163162

164163
# Compute errors
165164
errors = predictions - y
166165

167166
# Compute gradient
168-
gradient = (X.T @ errors) / len(y)
167+
gradient = (x_train.T @ errors) / len(y)
169168

170169
# Update parameters
171170
new_theta = theta - LEARNING_RATE * gradient
@@ -190,14 +189,14 @@ def test_gradient_descent_vectorized() -> None:
190189
Tests the vectorized gradient descent implementation on test data
191190
and prints predicted vs actual outputs.
192191
"""
193-
X_test = np.array([x for x, _ in test_data])
192+
x_test = np.array([x for x, _ in test_data])
194193
y_test = np.array([y for _, y in test_data])
195194

196195
# Add bias term
197-
X_test = np.hstack((np.ones((X_test.shape[0], 1)), X_test))
196+
x_test = np.hstack((np.ones((x_test.shape[0], 1)), x_test))
198197

199198
theta = np.array(parameter_vector, dtype=float)
200-
predictions = X_test @ theta
199+
predictions = x_test @ theta
201200

202201
for i in range(len(test_data)):
203202
print(("Actual output value:", y_test[i]))

0 commit comments

Comments
 (0)