Skip to content

Commit b9e4e5a

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 6df5956 commit b9e4e5a

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

machine_learning/linear_regression.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import numpy as np
22
import requests
33

4+
45
def collect_dataset():
56
"""Collect dataset of CSGO (ADR vs Rating of a Player)"""
67
response = requests.get(
@@ -10,35 +11,40 @@ def collect_dataset():
1011
data = np.loadtxt(response.text.splitlines()[1:], delimiter=",") # Skip the header
1112
return data
1213

14+
1315
def normalize_features(data):
1416
"""Normalize feature values to have mean 0 and variance 1"""
1517
means = np.mean(data[:, :-1], axis=0)
1618
stds = np.std(data[:, :-1], axis=0)
1719
data[:, :-1] = (data[:, :-1] - means) / stds
1820
return data
1921

22+
2023
def run_gradient_descent(data_x, data_y, alpha=0.01, iterations=1000, batch_size=32):
2124
"""Run gradient descent with mini-batch optimization"""
2225
len_data, no_features = data_x.shape
2326
theta = np.zeros(no_features)
2427

2528
for i in range(iterations):
26-
indices = np.random.choice(len_data, batch_size, replace=False) # Randomly sample indices
29+
indices = np.random.choice(
30+
len_data, batch_size, replace=False
31+
) # Randomly sample indices
2732
x_batch = data_x[indices]
2833
y_batch = data_y[indices]
2934

3035
predictions = x_batch @ theta # Vectorized predictions
3136
errors = predictions - y_batch
32-
37+
3338
gradient = (1 / batch_size) * (x_batch.T @ errors) # Vectorized gradient
3439
theta -= alpha * gradient # Update theta
35-
40+
3641
if i % 100 == 0: # Print error every 100 iterations
37-
error = np.mean(errors ** 2) # Mean Squared Error
42+
error = np.mean(errors**2) # Mean Squared Error
3843
print(f"Iteration {i}: MSE = {error:.5f}")
3944

4045
return theta
4146

47+
4248
def main():
4349
"""Driver function"""
4450
data = collect_dataset()
@@ -52,5 +58,6 @@ def main():
5258
print("Resultant Feature vector : ")
5359
print(theta)
5460

61+
5562
if __name__ == "__main__":
5663
main()

0 commit comments

Comments
 (0)