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

This commit is contained in:
wzy
2024-10-24 17:22:19 +08:00
parent 0ee6be36ef
commit b880be4230
+35 -8
View File
@@ -29,16 +29,29 @@ name = "万子玉"
def sigmoid(z):
return 1 / (1 + np.exp(-z))
def train_logreg(X, y, learning_rate=0.01, iterations=1000):
def train_logreg(X, y, learning_rate=0.01, iterations=1000, batch_size=32, initial_weights=None):
X = np.insert(X, 0, 1, axis=1) # 添加偏置项
weights = np.zeros(X.shape[1])
if initial_weights is None:
weights = np.zeros(X.shape[1])
else:
weights = initial_weights
m = X.shape[0]
for _ in range(iterations):
z = np.dot(X, weights)
h = sigmoid(z)
gradient = np.dot(X.T, (h - y)) / m
weights -= learning_rate * gradient
indices = np.arange(m)
np.random.shuffle(indices)
X_shuffled = X[indices]
y_shuffled = y[indices]
for i in range(0, m, batch_size):
end = i + batch_size
X_batch = X_shuffled[i:end]
y_batch = y_shuffled[i:end]
z = np.dot(X_batch, weights)
h = sigmoid(z)
gradient = np.dot(X_batch.T, (h - y_batch)) / batch_size
weights -= learning_rate * gradient
return weights
"""
@@ -105,15 +118,29 @@ def load_data(filepath):
if __name__ == "__main__":
# 以下代码可用于测试,可自行随意修改
# 加载训练集
X_train, y_train = load_data("./data/horse-colic.data")
# 加载测试集
X_test, y_test = load_data("./data/horse-colic.test")
# 模型调优参数
learning_rates = [0.01, 0.001, 0.0001]
iterations = [500, 1000, 2000]
batch_sizes = [16, 32, 64]
initial_weights = np.random.randn(X_train.shape[1] + 1) # 随机初始化权重
# 选择一组参数进行训练
chosen_learning_rate = learning_rates[1]
chosen_iterations = iterations[1]
chosen_batch_size = batch_sizes[1]
# 训练对率回归模型
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000)
weights = train_logreg(X_train, y_train,
learning_rate=chosen_learning_rate,
iterations=chosen_iterations,
batch_size=chosen_batch_size,
initial_weights=initial_weights)
# 测试单个数据
y_pred = predict_logreg(X_test[11], weights)