更新 function.py
Gitea Actions Experiment Test / Experiment-Actions (push) Successful in 20s

This commit is contained in:
Wyy
2024-11-02 21:08:35 +08:00
parent 6c364dc100
commit 1308465b88
+9 -13
View File
@@ -14,21 +14,17 @@ def sigmoid(z):
return 1 / (1 + np.exp(-z))
def train_logreg(X, y, numIter=1000, learning_rate=0.06, batch_size=128, lambda_reg=0.01):
def train_logreg(X, y, numIter=1000, learning_rate=0.01, lambda_reg=0.01):
m, n = X.shape
weights = np.zeros(n)
for j in range(numIter):
dataIndex = np.random.permutation(m)
for i in range(0, m, batch_size):
end = min(i + batch_size, m)
batch_X = X[dataIndex[i:end]]
batch_y = y[dataIndex[i:end]]
z = np.dot(batch_X, weights)
h = 1 / (1 + np.exp(-z))
error = batch_y - h
gradient = np.dot(batch_X.T, error) / len(batch_y)
reg_term = 2 * lambda_reg * weights
gradient -= reg_term / len(batch_y)
for i in range(m):
z = np.dot(X[i], weights)
h = sigmoid(z)
error = y[i] - h
gradient = X[i] * error
reg_term = lambda_reg * weights
gradient += reg_term
weights += learning_rate * gradient
return weights
@@ -45,7 +41,7 @@ def predict_logreg(inX, weights):
- int: 预测的分类标签。
"""
z = np.dot(inX, weights)
prob = 1 / (1 + np.exp(-z))
prob = sigmoid(z)
return 1 if prob >= 0.5 else 0