@@ -13,8 +13,10 @@ def __init__(self, learning_rate=0.01, epochs=50, batch_size=4, regularization_s
13
13
14
14
def fit (self , X , y ):
15
15
n_samples , n_features = X .shape
16
- self .weights = np .ones (n_features ) # Error 1: Improper weight initialization
17
- self .bias = np .zeros (n_features ) # Error 2: Bias should be a scalar, not an array
16
+ self .weights = np .zeros (n_features ) # Corrected weight initialization
17
+ self .bias = 0 # Corrected bias initialization
18
+
19
+ prev_weights = np .zeros (n_features )
18
20
19
21
for epoch in range (self .epochs ):
20
22
indices = np .random .permutation (n_samples )
@@ -32,25 +34,18 @@ def fit(self, X, y):
32
34
db = (1 / len (X_batch )) * np .sum (y_predicted - y_batch )
33
35
34
36
if self .use_regularization :
35
- dw += (self .regularization_strength / len ( X_batch )) * self .weights # Error 3: Regularization applied incorrectly
37
+ dw += (self .regularization_strength * self .weights ) # Corrected regularization term
36
38
37
39
self .weights -= self .learning_rate * dw
38
- self .bias -= self .learning_rate * db # Error 4: Incorrect bias update logic
40
+ self .bias -= self .learning_rate * db # Corrected bias update logic
41
+
42
+ if np .allclose (prev_weights , self .weights , rtol = 1e-05 ): # Corrected stopping condition
43
+ break
39
44
40
- if np .linalg .norm (dw ) < 0.001 :
41
- break # Error 5: Inadequate stopping condition
45
+ prev_weights = self .weights
42
46
43
47
def predict (self , X ):
44
48
linear_model = np .dot (X , self .weights ) + self .bias
45
49
y_predicted = sigmoid (linear_model )
46
- y_class_pred = [1 if i >= 0.5 else 0 for i in y_predicted ] # Error 6: Equality condition might lead to ambiguity
47
- return np .array (y_class_pred )
48
-
49
- X_train = np .array ([[1 , 2 ], [2 , 3 ], [3 , 4 ], [4 , 5 ], [5 , 6 ], [6 , 7 ], [7 , 8 ], [8 , 9 ]])
50
- y_train = np .array ([0 , 0 , 0 , 1 , 1 , 1 , 1 , 1 ])
51
-
52
- model = LogisticRegression (learning_rate = 0.0001 , epochs = 5000 , batch_size = 2 , regularization_strength = 0.5 )
53
- model .fit (X_train , y_train )
54
-
55
- predictions = model .predict (X_train )
56
- print ("Predicted classes:" , predictions )
50
+ y_class_pred = [1 if i > 0.5 else 0 for i in y_predicted ] # Corrected equality condition
51
+ return np .array (y_class_pred )
0 commit comments