Skip to content

Commit 154944c

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

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

machine_learning/ridge_regression.py

+26-16
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
"""
2-
Ridge Regression is a type of linear regression that includes an L2 regularization term
3-
to prevent overfitting and improve generalization. It is commonly used when multicollinearity
4-
occurs, as it helps to reduce the model's complexity by penalizing large coefficients,
2+
Ridge Regression is a type of linear regression that includes an L2 regularization term
3+
to prevent overfitting and improve generalization. It is commonly used when multicollinearity
4+
occurs, as it helps to reduce the model's complexity by penalizing large coefficients,
55
resulting in better prediction performance on unseen data.
66
7-
This implementation uses gradient descent to optimize the weights, with an L2 penalty to
8-
regularize the feature vector. The code reads a dataset with Average Damage per Round (ADR)
9-
and player ratings, processes the data, and applies ridge regression to predict ADR
7+
This implementation uses gradient descent to optimize the weights, with an L2 penalty to
8+
regularize the feature vector. The code reads a dataset with Average Damage per Round (ADR)
9+
and player ratings, processes the data, and applies ridge regression to predict ADR
1010
based on player ratings.
1111
1212
WIKI: https://en.wikipedia.org/wiki/Ridge_regression
@@ -16,6 +16,7 @@
1616
import pandas as pd
1717
from sklearn.metrics import mean_absolute_error
1818

19+
1920
class RidgeRegression:
2021
"""
2122
A Ridge Regression model with L2 regularization.
@@ -27,7 +28,10 @@ class RidgeRegression:
2728
weights (np.ndarray): Feature weights.
2829
bias (float): Bias term for the regression model.
2930
"""
30-
def __init__(self, learning_rate=0.01, regularization_param=0.1, num_iterations=1000):
31+
32+
def __init__(
33+
self, learning_rate=0.01, regularization_param=0.1, num_iterations=1000
34+
):
3135
self.learning_rate = learning_rate
3236
self.regularization_param = regularization_param
3337
self.num_iterations = num_iterations
@@ -57,7 +61,9 @@ def fit(self, X, y):
5761
error = y_pred - y
5862

5963
# Calculate gradients with L2 regularization
60-
dw = (1 / num_samples) * (X.T.dot(error) + self.regularization_param * self.weights)
64+
dw = (1 / num_samples) * (
65+
X.T.dot(error) + self.regularization_param * self.weights
66+
)
6167
db = (1 / num_samples) * np.sum(error)
6268

6369
# Update weights and bias
@@ -124,10 +130,11 @@ def calculate_mae(self, X, y):
124130
y_pred = self.predict(X)
125131
return mean_absolute_error(y, y_pred)
126132

133+
127134
# Load data
128135
def load_data(filepath):
129136
"""
130-
Loads data from a CSV file, extracting 'PlayerRating' as the feature
137+
Loads data from a CSV file, extracting 'PlayerRating' as the feature
131138
and 'ADR' as the target variable.
132139
133140
Args:
@@ -141,17 +148,18 @@ def load_data(filepath):
141148
True
142149
"""
143150
data = pd.read_csv(filepath)
144-
X = data[['PlayerRating']].values # Feature
145-
y = data['ADR'].values # Target
151+
X = data[["PlayerRating"]].values # Feature
152+
y = data["ADR"].values # Target
146153
return X, y
147154

155+
148156
# Example usage
149157
if __name__ == "__main__":
150158
"""
151159
Ridge Regression model for predicting Average Damage per Round (ADR) based on player ratings.
152160
153-
The model is initialized with a learning rate, regularization parameter, and a specified
154-
number of gradient descent iterations. After training, it outputs the optimized weights
161+
The model is initialized with a learning rate, regularization parameter, and a specified
162+
number of gradient descent iterations. After training, it outputs the optimized weights
155163
and bias, and displays the Mean Squared Error (MSE) and Mean Absolute Error (MAE).
156164
157165
>>> model = RidgeRegression(learning_rate=0.01, regularization_param=0.5, num_iterations=1000)
@@ -165,17 +173,19 @@ def load_data(filepath):
165173
doctest.testmod()
166174

167175
# Load and preprocess the data
168-
filepath = 'player_data.csv' # Replace with actual file path
176+
filepath = "player_data.csv" # Replace with actual file path
169177
X, y = load_data(filepath)
170178

171179
# Initialize and train the model
172-
model = RidgeRegression(learning_rate=0.01, regularization_param=0.5, num_iterations=1000)
180+
model = RidgeRegression(
181+
learning_rate=0.01, regularization_param=0.5, num_iterations=1000
182+
)
173183
model.fit(X, y)
174184

175185
# Calculate and display errors
176186
mse = model.calculate_error(X, y)
177187
mae = model.calculate_mae(X, y)
178-
188+
179189
print(f"Optimized weights: {model.weights}")
180190
print(f"Bias: {model.bias}")
181191
print(f"Mean Squared Error: {mse}")

0 commit comments

Comments
 (0)