diff --git a/function.py b/function.py index d9485ba..03a582a 100644 --- a/function.py +++ b/function.py @@ -31,14 +31,16 @@ import numpy as np def sigmoid(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: - X (numpy array): 特征数据。 - y (numpy array): 标签数据。 - - other_params (str): 其它参数,可选设置。若需设置其它参数,必须设置其默认值,否则将**无法通过**自动评测,**成绩为零**。 + - learning_rate (float): 学习率,默认为 0.01。 + - iterations (int): 迭代次数,默认为 1000。 + - batch_size (int): 小批量大小,默认为 32。 Returns: - numpy array: 训练得到的权重。 @@ -47,12 +49,18 @@ def train_logreg(X, y, learning_rate=0.001, iterations=2000): weights = np.zeros(n) bias = 0 for _ in range(iterations): - z = np.dot(X, weights) + bias - h = sigmoid(z) - dw = (1/m) * np.dot(X.T, (h - y)) - db = (1/m) * np.sum(h - y) - weights -= learning_rate * dw - bias -= learning_rate * db + indices = np.random.permutation(m) + X_shuffled = X[indices] + y_shuffled = y[indices] + for i in range(0, m, batch_size): + X_batch = X_shuffled[i:i+batch_size] + 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 def predict_logreg(inX, weights): @@ -105,8 +113,9 @@ if __name__ == "__main__": 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) print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}") +