generated from LufsX/Experiment-LogReg
This commit is contained in:
+22
-20
@@ -17,38 +17,50 @@ import numpy as np
|
||||
- numpy: 2.1.1
|
||||
"""
|
||||
|
||||
|
||||
"""
|
||||
请在这里填写学号和姓名,不填写即成绩无效。
|
||||
"""
|
||||
id = "202210423213"
|
||||
name = "熊浩燃"
|
||||
id = "202210423xxx"
|
||||
name = "test_name"
|
||||
|
||||
|
||||
# 这里写自己的代码实现
|
||||
def sigmoid(z):
|
||||
return 1 / (1 + np.exp(-z))
|
||||
|
||||
def train_logreg(X, y, learning_rate=0.01, iterations=1000):
|
||||
def normalize(X):
|
||||
""" 数据归一化 """
|
||||
return (X - np.mean(X, axis=0)) / np.std(X, axis=0)
|
||||
|
||||
def train_logreg(X, y, learning_rate=0.01, iterations=1000, batch_size=32, lambda_=0.01):
|
||||
"""
|
||||
对率回归训练函数。
|
||||
对率回归训练函数,包含批量梯度下降和L2正则化。
|
||||
|
||||
Args:
|
||||
- X (numpy array): 特征数据。
|
||||
- y (numpy array): 标签数据。
|
||||
- learning_rate (float): 学习率。
|
||||
- iterations (int): 迭代次数。
|
||||
- batch_size (int): 每次更新的样本数。
|
||||
- lambda_ (float): L2正则化项的系数。
|
||||
|
||||
Returns:
|
||||
- numpy array: 训练得到的权重。
|
||||
"""
|
||||
X = normalize(X) # 归一化数据
|
||||
m, n = X.shape
|
||||
weights = np.zeros(n)
|
||||
|
||||
for _ in range(iterations):
|
||||
z = np.dot(X, weights)
|
||||
# 随机选择一个小批量
|
||||
indices = np.random.choice(m, batch_size, replace=False)
|
||||
X_batch = X[indices]
|
||||
y_batch = y[indices]
|
||||
|
||||
# 计算预测和梯度
|
||||
z = np.dot(X_batch, weights)
|
||||
h = sigmoid(z)
|
||||
gradient = np.dot(X.T, (h - y)) / m
|
||||
gradient = np.dot(X_batch.T, (h - y_batch)) / batch_size + lambda_ * weights
|
||||
weights -= learning_rate * gradient
|
||||
|
||||
return weights
|
||||
|
||||
def predict_logreg(inX, weights):
|
||||
@@ -65,7 +77,6 @@ def predict_logreg(inX, weights):
|
||||
probability = sigmoid(np.dot(inX, weights))
|
||||
return 1 if probability >= 0.5 else 0
|
||||
|
||||
# 这里也写自己的代码实现
|
||||
def load_data(filepath):
|
||||
"""
|
||||
从文件中加载数据,若不自行正确实现,则无法通过评测,**不记录成绩**。
|
||||
@@ -93,16 +104,7 @@ 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)
|
||||
|
||||
# 测试单个数据
|
||||
y_pred = predict_logreg(X_test[11], weights)
|
||||
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")
|
||||
# 加载测试集
|
||||
X_test, y_test = load_data("./data/horse-colic.test")
|
||||
|
||||
# 训练对率回归模型
|
||||
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000)
|
||||
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000, batch_size=32, lambda_=0.01)
|
||||
|
||||
# 测试单个数据
|
||||
y_pred = predict_logreg(X_test[11], weights)
|
||||
|
||||
Reference in New Issue
Block a user