generated from LufsX/Experiment-LogReg
This commit is contained in:
+15
-16
@@ -6,13 +6,11 @@ from sklearn.model_selection import train_test_split
|
||||
id = "202210423130"
|
||||
name = "董秋"
|
||||
|
||||
|
||||
def sigmoid(z):
|
||||
"""Sigmoid 激活函数。"""
|
||||
return 1 / (1 + np.exp(-z))
|
||||
|
||||
|
||||
def train_logreg(X, y, learning_rate=0.01, iterations=1000, lambda_reg=0.01):
|
||||
def train_logreg(X, y, learning_rate=0.01, iterations=1000, lambda_reg=0.01, batch_size=32):
|
||||
"""逻辑回归训练函数,随机初始化权重,并在每次迭代前打乱数据。"""
|
||||
m, n = X.shape
|
||||
weights = np.random.randn(n) * 0.01 # 随机初始化权重,取较小值
|
||||
@@ -23,29 +21,31 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000, lambda_reg=0.01):
|
||||
X = X[indices]
|
||||
y = y[indices]
|
||||
|
||||
z = np.dot(X, weights)
|
||||
predictions = sigmoid(z)
|
||||
gradient = np.dot(X.T, (predictions - y)) / m
|
||||
for j in range(0, m, batch_size): # 按照 batch size 迭代
|
||||
X_batch = X[j:j + batch_size]
|
||||
y_batch = y[j:j + batch_size]
|
||||
|
||||
# L2正则化
|
||||
gradient += (lambda_reg / m) * weights
|
||||
z = np.dot(X_batch, weights)
|
||||
predictions = sigmoid(z)
|
||||
gradient = np.dot(X_batch.T, (predictions - y_batch)) / X_batch.shape[0]
|
||||
|
||||
# 更新权重
|
||||
weights -= learning_rate * gradient
|
||||
# L2正则化
|
||||
gradient += (lambda_reg / m) * weights
|
||||
|
||||
# 更新权重
|
||||
weights -= learning_rate * gradient
|
||||
|
||||
# 动态调整学习率,逐渐减小
|
||||
learning_rate *= 0.99
|
||||
|
||||
return weights
|
||||
|
||||
|
||||
def predict_logreg(inX, weights):
|
||||
"""逻辑回归预测函数。"""
|
||||
z = np.dot(inX, weights)
|
||||
prediction_prob = sigmoid(z)
|
||||
return 1 if prediction_prob >= 0.5 else 0
|
||||
|
||||
|
||||
def load_data(filepath):
|
||||
"""加载数据并处理缺失值。"""
|
||||
data = []
|
||||
@@ -76,13 +76,11 @@ def load_data(filepath):
|
||||
|
||||
return data, labels
|
||||
|
||||
|
||||
def preprocess_data(X):
|
||||
"""特征标准化。"""
|
||||
scaler = StandardScaler()
|
||||
return scaler.fit_transform(X)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 加载数据集
|
||||
X, y = load_data("./data/horse-colic.data")
|
||||
@@ -93,8 +91,9 @@ if __name__ == "__main__":
|
||||
# 使用 train_test_split 打乱并随机抽取数据
|
||||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
||||
|
||||
# 训练逻辑回归模型
|
||||
weights = train_logreg(X_train, y_train, learning_rate=0.1, iterations=1000, lambda_reg=0.01)
|
||||
# 训练逻辑回归模型,设置 batch size
|
||||
batch_size = 32
|
||||
weights = train_logreg(X_train, y_train, learning_rate=0.1, iterations=1000, lambda_reg=0.01, batch_size=batch_size)
|
||||
|
||||
# 测试单个数据
|
||||
y_pred = predict_logreg(X_test[11], weights)
|
||||
|
||||
Reference in New Issue
Block a user