generated from LufsX/Experiment-LogReg
This commit is contained in:
+7
-11
@@ -26,11 +26,11 @@ name = "熊浩燃"
|
||||
def sigmoid(z):
|
||||
return 1 / (1 + np.exp(-z))
|
||||
|
||||
def min_max_normalize(X):
|
||||
""" 数据归一化:使用Min-Max Scaling """
|
||||
return (X - np.min(X, axis=0)) / (np.max(X, axis=0) - np.min(X, axis=0))
|
||||
def standardize(X):
|
||||
""" 数据标准化:均值为0,标准差为1 """
|
||||
return (X - np.mean(X, axis=0)) / (np.std(X, axis=0) + 1e-8) # 避免除零
|
||||
|
||||
def train_logreg(X, y, learning_rate=0.001, iterations=2000, batch_size=32, lambda_=0.1):
|
||||
def train_logreg(X, y, learning_rate=0.0005, iterations=3000, batch_size=32, lambda_=0.05):
|
||||
"""
|
||||
对率回归训练函数,包含批量梯度下降和L2正则化。
|
||||
|
||||
@@ -45,9 +45,9 @@ def train_logreg(X, y, learning_rate=0.001, iterations=2000, batch_size=32, lamb
|
||||
Returns:
|
||||
- numpy array: 训练得到的权重。
|
||||
"""
|
||||
X = min_max_normalize(X) # 使用Min-Max归一化数据
|
||||
X = standardize(X) # 使用标准化数据
|
||||
m, n = X.shape
|
||||
weights = np.zeros(n)
|
||||
weights = np.random.rand(n) * 0.01 # 初始化权重为小的随机数
|
||||
|
||||
for _ in range(iterations):
|
||||
# 确保 batch_size 不超过 m
|
||||
@@ -107,8 +107,4 @@ 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, batch_size=32, lambda_=0.1)
|
||||
|
||||
# 测试单个数据
|
||||
y_pred = predict_logreg(X_test[11], weights)
|
||||
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")
|
||||
weights = train_logreg(
|
||||
|
||||
Reference in New Issue
Block a user