Skip to content

Commit 1ae59ed

Browse files
Merge pull request csivitu#8 from afif729/main
fixed issue csivitu#3
2 parents 67e6490 + 96e4bd1 commit 1ae59ed

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

neo.py

+12-17
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ def __init__(self, learning_rate=0.01, epochs=50, batch_size=4, regularization_s
1313

1414
def fit(self, X, y):
1515
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)
1820

1921
for epoch in range(self.epochs):
2022
indices = np.random.permutation(n_samples)
@@ -32,25 +34,18 @@ def fit(self, X, y):
3234
db = (1 / len(X_batch)) * np.sum(y_predicted - y_batch)
3335

3436
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
3638

3739
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
3944

40-
if np.linalg.norm(dw) < 0.001:
41-
break # Error 5: Inadequate stopping condition
45+
prev_weights = self.weights
4246

4347
def predict(self, X):
4448
linear_model = np.dot(X, self.weights) + self.bias
4549
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

Comments
 (0)