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

This commit is contained in:
2024-11-12 02:59:03 +08:00
parent 1ef7ddabca
commit 0e6cbbb506
+18 -9
View File
@@ -31,14 +31,16 @@ import numpy as np
def sigmoid(z): def sigmoid(z):
return 1 / (1 + np.exp(-z)) return 1 / (1 + np.exp(-z))
def train_logreg(X, y, learning_rate=0.001, iterations=2000): def train_logreg(X, y, learning_rate=0.01, iterations=1000, batch_size=32):
""" """
对率回归训练函数。 对率回归训练函数。
Args: Args:
- X (numpy array): 特征数据。 - X (numpy array): 特征数据。
- y (numpy array): 标签数据。 - y (numpy array): 标签数据。
- other_params (str): 其它参数,可选设置。若需设置其它参数,必须设置其默认值,否则将**无法通过**自动评测,**成绩为零**。 - learning_rate (float): 学习率,默认为 0.01。
- iterations (int): 迭代次数,默认为 1000。
- batch_size (int): 小批量大小,默认为 32。
Returns: Returns:
- numpy array: 训练得到的权重。 - numpy array: 训练得到的权重。
@@ -47,12 +49,18 @@ def train_logreg(X, y, learning_rate=0.001, iterations=2000):
weights = np.zeros(n) weights = np.zeros(n)
bias = 0 bias = 0
for _ in range(iterations): for _ in range(iterations):
z = np.dot(X, weights) + bias indices = np.random.permutation(m)
h = sigmoid(z) X_shuffled = X[indices]
dw = (1/m) * np.dot(X.T, (h - y)) y_shuffled = y[indices]
db = (1/m) * np.sum(h - y) for i in range(0, m, batch_size):
weights -= learning_rate * dw X_batch = X_shuffled[i:i+batch_size]
bias -= learning_rate * db y_batch = y_shuffled[i:i+batch_size]
z = np.dot(X_batch, weights) + bias
h = sigmoid(z)
dw = (1/batch_size) * np.dot(X_batch.T, (h - y_batch))
db = (1/batch_size) * np.sum(h - y_batch)
weights -= learning_rate * dw
bias -= learning_rate * db
return weights return weights
def predict_logreg(inX, weights): def predict_logreg(inX, weights):
@@ -105,8 +113,9 @@ if __name__ == "__main__":
X_test, y_test = load_data("./data/horse-colic.test") X_test, y_test = load_data("./data/horse-colic.test")
# 训练对率回归模型 # 训练对率回归模型
weights = train_logreg(X_train, y_train, learning_rate=0.001, iterations=2000) # 修改迭代次数 weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000, batch_size=32) # 修改批量大小
# 测试单个数据 # 测试单个数据
y_pred = predict_logreg(X_test[11], weights) y_pred = predict_logreg(X_test[11], weights)
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}") print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")