1
1
"""
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,
5
5
resulting in better prediction performance on unseen data.
6
6
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
10
10
based on player ratings.
11
11
12
12
WIKI: https://en.wikipedia.org/wiki/Ridge_regression
16
16
import pandas as pd
17
17
from sklearn .metrics import mean_absolute_error
18
18
19
+
19
20
class RidgeRegression :
20
21
"""
21
22
A Ridge Regression model with L2 regularization.
@@ -27,7 +28,10 @@ class RidgeRegression:
27
28
weights (np.ndarray): Feature weights.
28
29
bias (float): Bias term for the regression model.
29
30
"""
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
+ ):
31
35
self .learning_rate = learning_rate
32
36
self .regularization_param = regularization_param
33
37
self .num_iterations = num_iterations
@@ -57,7 +61,9 @@ def fit(self, X, y):
57
61
error = y_pred - y
58
62
59
63
# 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
+ )
61
67
db = (1 / num_samples ) * np .sum (error )
62
68
63
69
# Update weights and bias
@@ -124,10 +130,11 @@ def calculate_mae(self, X, y):
124
130
y_pred = self .predict (X )
125
131
return mean_absolute_error (y , y_pred )
126
132
133
+
127
134
# Load data
128
135
def load_data (filepath ):
129
136
"""
130
- Loads data from a CSV file, extracting 'PlayerRating' as the feature
137
+ Loads data from a CSV file, extracting 'PlayerRating' as the feature
131
138
and 'ADR' as the target variable.
132
139
133
140
Args:
@@ -141,17 +148,18 @@ def load_data(filepath):
141
148
True
142
149
"""
143
150
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
146
153
return X , y
147
154
155
+
148
156
# Example usage
149
157
if __name__ == "__main__" :
150
158
"""
151
159
Ridge Regression model for predicting Average Damage per Round (ADR) based on player ratings.
152
160
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
155
163
and bias, and displays the Mean Squared Error (MSE) and Mean Absolute Error (MAE).
156
164
157
165
>>> model = RidgeRegression(learning_rate=0.01, regularization_param=0.5, num_iterations=1000)
@@ -165,17 +173,19 @@ def load_data(filepath):
165
173
doctest .testmod ()
166
174
167
175
# 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
169
177
X , y = load_data (filepath )
170
178
171
179
# 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
+ )
173
183
model .fit (X , y )
174
184
175
185
# Calculate and display errors
176
186
mse = model .calculate_error (X , y )
177
187
mae = model .calculate_mae (X , y )
178
-
188
+
179
189
print (f"Optimized weights: { model .weights } " )
180
190
print (f"Bias: { model .bias } " )
181
191
print (f"Mean Squared Error: { mse } " )
0 commit comments