Skip to content

Commit b72320b

Browse files
committed
added ridge regression
1 parent 1cb79bc commit b72320b

File tree

1 file changed

+20
-75
lines changed
  • machine_learning/ridge_regression

1 file changed

+20
-75
lines changed

machine_learning/ridge_regression/model.py

+20-75
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,57 @@
11
import numpy as np
2-
3-
"""# Ridge Regression Class
4-
class RidgeRegression:
5-
def __init__(self, learning_rate=0.01, num_iterations=1000, regularization_param=0.1):
6-
self.learning_rate = learning_rate
7-
self.num_iterations = num_iterations
8-
self.regularization_param = regularization_param
9-
self.weights = None
10-
self.bias = None
11-
12-
def fit(self, X, y):
13-
n_samples, n_features = X.shape
14-
15-
# initializing weights and bias
16-
self.weights = np.zeros(n_features)
17-
self.bias = 0
18-
19-
# gradient descent
20-
for _ in range(self.num_iterations):
21-
y_predicted = np.dot(X, self.weights) + self.bias
22-
23-
# gradients for weights and bias
24-
dw = (1/n_samples) * np.dot(X.T, (y_predicted - y)) + (self.regularization_param / n_samples) * self.weights
25-
db = (1/n_samples) * np.sum(y_predicted - y)
26-
27-
# updating weights and bias
28-
self.weights -= self.learning_rate * dw
29-
self.bias -= self.learning_rate * db
30-
31-
def predict(self, X):
32-
return np.dot(X, self.weights) + self.bias
33-
34-
def mean_absolute_error(self, y_true, y_pred):
35-
return np.mean(np.abs(y_true - y_pred))
36-
37-
# Load Data Function
38-
def load_data(file_path):
39-
data = []
40-
with open(file_path, 'r') as file:
41-
for line in file.readlines()[1:]:
42-
features = line.strip().split(',')
43-
data.append([float(f) for f in features])
44-
return np.array(data)
45-
46-
# Example usage
47-
if __name__ == "__main__":
48-
49-
data = load_data('ADRvsRating.csv')
50-
X = data[:, 0].reshape(-1, 1) # independent features
51-
y = data[:, 1] # dependent variable
52-
53-
# initializing and training Ridge Regression model
54-
model = RidgeRegression(learning_rate=0.001, num_iterations=1000, regularization_param=0.1)
55-
model.fit(X, y)
56-
57-
# predictions
58-
predictions = model.predict(X)
59-
60-
# mean absolute error
61-
mae = model.mean_absolute_error(y, predictions)
62-
print(f"Mean Absolute Error: {mae}")
63-
64-
# final output weights and bias
65-
print(f"Optimized Weights: {model.weights}")
66-
print(f"Bias: {model.bias}")"""
67-
682
import pandas as pd
3+
694
class RidgeRegression:
70-
def __init__(self, alpha=0.001, lambda_=0.1, iterations=1000):
5+
def __init__(self, alpha=0.001, regularization_param=0.1, num_iterations=1000):
716
self.alpha = alpha
72-
self.lambda_ = lambda_
73-
self.iterations = iterations
7+
self.regularization_param = regularization_param
8+
self.num_iterations = num_iterations
749
self.theta = None
7510

11+
7612
def feature_scaling(self, X):
7713
mean = np.mean(X, axis=0)
7814
std = np.std(X, axis=0)
15+
7916
# avoid division by zero for constant features (std = 0)
8017
std[std == 0] = 1 # set std=1 for constant features to avoid NaN
18+
8119
X_scaled = (X - mean) / std
8220
return X_scaled, mean, std
8321

22+
8423
def fit(self, X, y):
8524
X_scaled, mean, std = self.feature_scaling(X)
8625
m, n = X_scaled.shape
8726
self.theta = np.zeros(n) # initializing weights to zeros
88-
for i in range(self.iterations):
27+
28+
for i in range(self.num_iterations):
8929
predictions = X_scaled.dot(self.theta)
9030
error = predictions - y
31+
9132
# computing gradient with L2 regularization
92-
gradient = (X_scaled.T.dot(error) + self.lambda_ * self.theta) / m
33+
gradient = (X_scaled.T.dot(error) + self.regularization_param * self.theta) / m
9334
self.theta -= self.alpha * gradient # updating weights
9435

36+
9537
def predict(self, X):
9638
X_scaled, _, _ = self.feature_scaling(X)
9739
return X_scaled.dot(self.theta)
9840

41+
9942
def compute_cost(self, X, y):
10043
X_scaled, _, _ = self.feature_scaling(X)
10144
m = len(y)
45+
10246
predictions = X_scaled.dot(self.theta)
103-
cost = (1 / (2 * m)) * np.sum((predictions - y) ** 2) + (
104-
self.lambda_ / (2 * m)
105-
) * np.sum(self.theta**2)
47+
cost = (1 / (2 * m)) * np.sum((predictions - y) ** 2) + (self.regularization_param / (2 * m)) * np.sum(self.theta**2)
10648
return cost
10749

50+
10851
def mean_absolute_error(self, y_true, y_pred):
10952
return np.mean(np.abs(y_true - y_pred))
53+
54+
11055
# Example usage
11156
if __name__ == "__main__":
11257
df = pd.read_csv("ADRvsRating.csv")
@@ -118,7 +63,7 @@ def mean_absolute_error(self, y_true, y_pred):
11863
X = np.c_[np.ones(X.shape[0]), X]
11964

12065
# initialize and train the Ridge Regression model
121-
model = RidgeRegression(alpha=0.01, lambda_=0.1, iterations=1000)
66+
model = RidgeRegression(alpha=0.01, regularization_param=0.1, num_iterations=1000)
12267
model.fit(X, y)
12368

12469
# predictions

0 commit comments

Comments
 (0)