diff --git a/function.py b/function.py index a427c16..b90c8a1 100644 --- a/function.py +++ b/function.py @@ -28,7 +28,7 @@ name = "程悦" def sigmoid(z): return 1.0 / (1 + np.exp(-z)) -def train_logreg(X, y, learning_rate=0.01, iterations=100): +def train_logreg(X, y, learning_rate=0.01, iterations=1000, batch_size=32, regularization=None, lambda_reg=0.1): """ 对率回归训练函数。 @@ -40,12 +40,31 @@ def train_logreg(X, y, learning_rate=0.01, iterations=100): - numpy array: 训练得到的权重。 """ m, n = X.shape - weights = np.zeros(n) + weights = np.random.randn(n) * 0.01 # 随机初始化权重 for i 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.random.permutation(m) # 随机打乱数据 + X_shuffled = X[indices] + y_shuffled = y[indices] + + for j in range(0, m, batch_size): + X_batch = X_shuffled[j:j + batch_size] + y_batch = y_shuffled[j:j + batch_size] + z = np.dot(X_batch, weights) + h = sigmoid(z) + gradient = np.dot(X_batch.T, (h - y_batch)) / len(y_batch) + + # 添加正则化项 + if regularization == 'L1': + gradient += lambda_reg * np.sign(weights) / len(y_batch) + elif regularization == 'L2': + gradient += lambda_reg * weights / len(y_batch) + + weights -= learning_rate * gradient + + # 可选:学习率衰减 + if i % 100 == 0: + learning_rate *= 0.95 # 每100次迭代衰减5% + return weights def predict_logreg(inX, weights): @@ -110,8 +129,8 @@ 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, re - ='l1') + weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000, batch_size=32, regularization='L2', + lambda_reg=0.1) # 测试单个数据 y_pred = predict_logreg(X_test[11], weights)