1
1
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
-
68
2
import pandas as pd
3
+
69
4
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 ):
71
6
self .alpha = alpha
72
- self .lambda_ = lambda_
73
- self .iterations = iterations
7
+ self .regularization_param = regularization_param
8
+ self .num_iterations = num_iterations
74
9
self .theta = None
75
10
11
+
76
12
def feature_scaling (self , X ):
77
13
mean = np .mean (X , axis = 0 )
78
14
std = np .std (X , axis = 0 )
15
+
79
16
# avoid division by zero for constant features (std = 0)
80
17
std [std == 0 ] = 1 # set std=1 for constant features to avoid NaN
18
+
81
19
X_scaled = (X - mean ) / std
82
20
return X_scaled , mean , std
83
21
22
+
84
23
def fit (self , X , y ):
85
24
X_scaled , mean , std = self .feature_scaling (X )
86
25
m , n = X_scaled .shape
87
26
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 ):
89
29
predictions = X_scaled .dot (self .theta )
90
30
error = predictions - y
31
+
91
32
# 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
93
34
self .theta -= self .alpha * gradient # updating weights
94
35
36
+
95
37
def predict (self , X ):
96
38
X_scaled , _ , _ = self .feature_scaling (X )
97
39
return X_scaled .dot (self .theta )
98
40
41
+
99
42
def compute_cost (self , X , y ):
100
43
X_scaled , _ , _ = self .feature_scaling (X )
101
44
m = len (y )
45
+
102
46
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 )
106
48
return cost
107
49
50
+
108
51
def mean_absolute_error (self , y_true , y_pred ):
109
52
return np .mean (np .abs (y_true - y_pred ))
53
+
54
+
110
55
# Example usage
111
56
if __name__ == "__main__" :
112
57
df = pd .read_csv ("ADRvsRating.csv" )
@@ -118,7 +63,7 @@ def mean_absolute_error(self, y_true, y_pred):
118
63
X = np .c_ [np .ones (X .shape [0 ]), X ]
119
64
120
65
# 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 )
122
67
model .fit (X , y )
123
68
124
69
# predictions
0 commit comments