2
2
import pandas as pd
3
3
4
4
class RidgeRegression :
5
- def __init__ (self , alpha = 0.001 , regularization_param = 0.1 , num_iterations = 1000 ):
6
- self .alpha = alpha
7
- self .regularization_param = regularization_param
8
- self .num_iterations = num_iterations
9
- self .theta = None
5
+ def __init__ (self , alpha : float = 0.001 , regularization_param : float = 0.1 , num_iterations : int = 1000 ) -> None :
6
+ self .alpha : float = alpha
7
+ self .regularization_param : float = regularization_param
8
+ self .num_iterations : int = num_iterations
9
+ self .theta : np . ndarray = None
10
10
11
11
12
- def feature_scaling (self , X ) :
12
+ def feature_scaling (self , X : np . ndarray ) -> tuple [ np . ndarray , np . ndarray , np . ndarray ] :
13
13
mean = np .mean (X , axis = 0 )
14
14
std = np .std (X , axis = 0 )
15
15
@@ -20,7 +20,7 @@ def feature_scaling(self, X):
20
20
return X_scaled , mean , std
21
21
22
22
23
- def fit (self , X , y ) :
23
+ def fit (self , X : np . ndarray , y : np . ndarray ) -> None :
24
24
X_scaled , mean , std = self .feature_scaling (X )
25
25
m , n = X_scaled .shape
26
26
self .theta = np .zeros (n ) # initializing weights to zeros
@@ -34,12 +34,12 @@ def fit(self, X, y):
34
34
self .theta -= self .alpha * gradient # updating weights
35
35
36
36
37
- def predict (self , X ) :
37
+ def predict (self , X : np . ndarray ) -> np . ndarray :
38
38
X_scaled , _ , _ = self .feature_scaling (X )
39
39
return X_scaled .dot (self .theta )
40
40
41
41
42
- def compute_cost (self , X , y ) :
42
+ def compute_cost (self , X : np . ndarray , y : np . ndarray ) -> float :
43
43
X_scaled , _ , _ = self .feature_scaling (X )
44
44
m = len (y )
45
45
@@ -48,7 +48,7 @@ def compute_cost(self, X, y):
48
48
return cost
49
49
50
50
51
- def mean_absolute_error (self , y_true , y_pred ) :
51
+ def mean_absolute_error (self , y_true : np . ndarray , y_pred : np . ndarray ) -> float :
52
52
return np .mean (np .abs (y_true - y_pred ))
53
53
54
54
@@ -59,10 +59,10 @@ def mean_absolute_error(self, y_true, y_pred):
59
59
y = df ["ADR" ].values
60
60
y = (y - np .mean (y )) / np .std (y )
61
61
62
- # Add bias term (intercept) to the feature matrix
62
+ # added bias term to the feature matrix
63
63
X = np .c_ [np .ones (X .shape [0 ]), X ]
64
64
65
- # initialize and train the Ridge Regression model
65
+ # initialize and train the ridge regression model
66
66
model = RidgeRegression (alpha = 0.01 , regularization_param = 0.1 , num_iterations = 1000 )
67
67
model .fit (X , y )
68
68
0 commit comments