generated from LufsX/Experiment-LogReg
This commit is contained in:
+27
-8
@@ -28,7 +28,7 @@ name = "程悦"
|
|||||||
def sigmoid(z):
|
def sigmoid(z):
|
||||||
return 1.0 / (1 + np.exp(-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: 训练得到的权重。
|
- numpy array: 训练得到的权重。
|
||||||
"""
|
"""
|
||||||
m, n = X.shape
|
m, n = X.shape
|
||||||
weights = np.zeros(n)
|
weights = np.random.randn(n) * 0.01 # 随机初始化权重
|
||||||
for i in range(iterations):
|
for i in range(iterations):
|
||||||
z = np.dot(X, weights)
|
indices = np.random.permutation(m) # 随机打乱数据
|
||||||
h = sigmoid(z)
|
X_shuffled = X[indices]
|
||||||
gradient = np.dot(X.T, (h - y)) / m
|
y_shuffled = y[indices]
|
||||||
weights -= learning_rate * gradient
|
|
||||||
|
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
|
return weights
|
||||||
|
|
||||||
def predict_logreg(inX, weights):
|
def predict_logreg(inX, weights):
|
||||||
@@ -110,8 +129,8 @@ 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.01, iterations=1000, re
|
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000, batch_size=32, regularization='L2',
|
||||||
='l1')
|
lambda_reg=0.1)
|
||||||
|
|
||||||
# 测试单个数据
|
# 测试单个数据
|
||||||
y_pred = predict_logreg(X_test[11], weights)
|
y_pred = predict_logreg(X_test[11], weights)
|
||||||
|
|||||||
Reference in New Issue
Block a user