更新 function.py
Gitea Actions Experiment Test / Experiment-Actions (push) Successful in 12s

This commit is contained in:
2024-10-31 16:42:49 +08:00
parent 4accd08471
commit 51f7c063f5
+30 -31
View File
@@ -1,21 +1,14 @@
import numpy as np
"""
实验说明:
- 自行编写实现数据加载与预测病马死亡的对率回归算法。
注意事项:
- 在 function.py 中实现对率回归算法。
- 可以自行导入其他库,需要在 requirements.txt 添加依赖。
自动评测脚本仅会加载 train_logreg、predict_logreg 和 load_data 函数,请勿修改函数名。
"""
id = "202210423213"
name = "熊浩燃"
# 对率回归训练函数,包含调参和优化
def train_logreg(X, y, learning_rate=0.01, iterations=1000, regularization=0.01, early_stopping_rounds=100):
# 对率回归训练函数,包含加权损失、正则化、学习率调度和提前停止
def train_logreg(X, y, learning_rate=0.01, iterations=1000, regularization=0.01, early_stopping_rounds=100, class_weights={0: 1, 1: 1}):
"""
对率回归训练函数,带有学习率调度和正则化。
对率回归训练函数,带有加权损失和正则化。
Args:
- X (numpy array): 特征数据。
- y (numpy array): 标签数据。
@@ -23,6 +16,7 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000, regularization=0.01,
- iterations (int): 最大迭代次数。
- regularization (float): 正则化参数。
- early_stopping_rounds (int): 提前停止轮数。
- class_weights (dict): 类别权重字典,控制类别不平衡。
Returns:
- numpy array: 训练得到的权重。
"""
@@ -31,12 +25,13 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000, regularization=0.01,
stopping_step = 0
for i in range(iterations):
# 预测值:通过对率回归模型得到
# 预测值
z = np.dot(X, weights)
predictions = 1 / (1 + np.exp(-z))
# 计算损失(含正则化项)
loss = -np.mean(y * np.log(predictions) + (1 - y) * np.log(1 - predictions)) + regularization * np.sum(weights**2)
# 加权损失
loss = -np.mean(class_weights[1] * y * np.log(predictions) +
class_weights[0] * (1 - y) * np.log(1 - predictions)) + regularization * np.sum(weights**2)
# 提前停止判断
if loss < min_loss:
@@ -48,7 +43,7 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000, regularization=0.01,
print("提前停止于迭代:", i)
break
# 计算梯度并更新权重(含正则化项)
# 计算梯度并更新权重
gradient = (np.dot(X.T, (predictions - y)) + regularization * weights) / y.size
weights -= learning_rate * gradient
@@ -59,18 +54,19 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000, regularization=0.01,
return weights
# 对率回归预测函数
def predict_logreg(inX, weights):
# 对率回归预测函数,带有自定义阈值
def predict_logreg(inX, weights, threshold=0.6):
"""
对率回归预测函数。
Args:
- inX (numpy array): 输入待分类的样本。
- weights (numpy array): 训练得到的权重。
- threshold (float): 分类决策阈值。
Returns:
- int: 预测的分类标签。
"""
probability = 1 / (1 + np.exp(-np.dot(inX, weights)))
return 1 if probability >= 0.5 else 0
return 1 if probability >= threshold else 0
# 数据加载函数,包含缺失值填充
def load_data(filepath):
@@ -107,7 +103,7 @@ def load_data(filepath):
return data, labels
# 网格搜索函数
def grid_search(X, y, learning_rates, iterations_list, regularizations):
def grid_search(X, y, learning_rates, iterations_list, regularizations, class_weights_list):
"""
网格搜索来优化对率回归模型。
Args:
@@ -116,6 +112,7 @@ def grid_search(X, y, learning_rates, iterations_list, regularizations):
- learning_rates (list of float): 学习率列表。
- iterations_list (list of int): 迭代次数列表。
- regularizations (list of float): 正则化参数列表。
- class_weights_list (list of dict): 类别权重列表。
Returns:
- dict: 包含最佳超参数及其对应的权重。
"""
@@ -126,17 +123,18 @@ def grid_search(X, y, learning_rates, iterations_list, regularizations):
for lr in learning_rates:
for iters in iterations_list:
for reg in regularizations:
print(f"尝试参数: 学习率={lr}, 迭代次数={iters}, 正则化={reg}")
weights = train_logreg(X, y, learning_rate=lr, iterations=iters, regularization=reg)
z = np.dot(X, weights)
predictions = 1 / (1 + np.exp(-z))
loss = -np.mean(y * np.log(predictions) + (1 - y) * np.log(1 - predictions))
print(f"损失: {loss}")
for cw in class_weights_list:
print(f"尝试参数: 学习率={lr}, 迭代次数={iters}, 正则化={reg}, 类别权重={cw}")
weights = train_logreg(X, y, learning_rate=lr, iterations=iters, regularization=reg, class_weights=cw)
z = np.dot(X, weights)
predictions = 1 / (1 + np.exp(-z))
loss = -np.mean(cw[1] * y * np.log(predictions) + cw[0] * (1 - y) * np.log(1 - predictions))
print(f"损失: {loss}")
if loss < best_loss:
best_loss = loss
best_weights = weights
best_params = {'learning_rate': lr, 'iterations': iters, 'regularization': reg}
if loss < best_loss:
best_loss = loss
best_weights = weights
best_params = {'learning_rate': lr, 'iterations': iters, 'regularization': reg, 'class_weights': cw}
print("最佳参数:", best_params)
return best_weights, best_params
@@ -149,13 +147,14 @@ if __name__ == "__main__":
learning_rates = [0.001, 0.01, 0.1]
iterations_list = [1000, 2000, 3000]
regularizations = [0.01, 0.1, 1.0]
class_weights_list = [{0: 1, 1: 1}, {0: 1, 1: 2}, {0: 1, 1: 3}]
# 进行网格搜索以找到最佳超参数组合
best_weights, best_params = grid_search(X_train, y_train, learning_rates, iterations_list, regularizations)
best_weights, best_params = grid_search(X_train, y_train, learning_rates, iterations_list, regularizations, class_weights_list)
print("最佳权重:", best_weights)
# 使用最佳权重进行预测
X_test, y_test = load_data("./data/horse-colic.test")
y_pred = [predict_logreg(x, best_weights) for x in X_test]
y_pred = [predict_logreg(x, best_weights, threshold=0.6) for x in X_test]
accuracy = np.mean(y_pred == y_test)
print(f"测试集准确率: {accuracy}")