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

This commit is contained in:
2024-11-12 03:06:40 +08:00
parent 0e6cbbb506
commit 5fcc553c83
+8 -4
View File
@@ -31,7 +31,7 @@ import numpy as np
def sigmoid(z):
return 1 / (1 + np.exp(-z))
def train_logreg(X, y, learning_rate=0.01, iterations=1000, batch_size=32):
def train_logreg(X, y, learning_rate=0.01, iterations=1000, batch_size=32, init_weights=None):
"""
对率回归训练函数。
@@ -41,12 +41,16 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000, batch_size=32):
- learning_rate (float): 学习率,默认为 0.01。
- iterations (int): 迭代次数,默认为 1000。
- batch_size (int): 小批量大小,默认为 32。
- init_weights (numpy array): 权重初始值,默认为 None(即全零初始化)。
Returns:
- numpy array: 训练得到的权重。
"""
m, n = X.shape
weights = np.zeros(n)
if init_weights is None:
weights = np.zeros(n)
else:
weights = init_weights
bias = 0
for _ in range(iterations):
indices = np.random.permutation(m)
@@ -113,9 +117,9 @@ if __name__ == "__main__":
X_test, y_test = load_data("./data/horse-colic.test")
# 训练对率回归模型
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000, batch_size=32) # 修改批量大小
init_weights = np.random.randn(X_train.shape[1]) # 随机初始化权重
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000, batch_size=32, init_weights=init_weights)
# 测试单个数据
y_pred = predict_logreg(X_test[11], weights)
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")