generated from LufsX/Experiment-LogReg
139 lines
3.6 KiB
Python
139 lines
3.6 KiB
Python
# function.py
|
|
import numpy as np
|
|
|
|
"""
|
|
实验说明:
|
|
- 自行编写实现数据加载与预测病马死亡的对率回归算法。
|
|
|
|
注意事项:
|
|
- 在 function.py 中实现对率回归算法。
|
|
- 可以自行导入其他库,需要在 requirements.txt 添加依赖,若未在 requirements.txt 中添加使用到的依赖,则无法自动评测,成绩为零。
|
|
- 自动评测脚本仅会加载 train_logreg、predict_logreg 和 load_data 函数,请勿修改函数名。
|
|
|
|
测评环境:
|
|
- CPU: Intel Xeon(R) CPU E3-1231 v3 @ 3.40GHz
|
|
- OS: Ubuntu 22.04.4 LTS
|
|
- Python: 3.10.12
|
|
- numpy: 2.1.1
|
|
"""
|
|
|
|
|
|
"""
|
|
请在这里填写学号和姓名,不填写即成绩无效。
|
|
姓名请直接**填写中文**
|
|
例如: name = "张三"
|
|
"""
|
|
id = "202210423629"
|
|
name = "tanjunfeng"
|
|
|
|
|
|
# 这里写自己的代码实现
|
|
import numpy as np
|
|
import pandas as pd
|
|
|
|
|
|
def load_data(train_path, test_path):
|
|
"""
|
|
加载训练数据和测试数据
|
|
|
|
Args:
|
|
train_path (str): 训练数据文件路径
|
|
test_path (str): 测试数据文件路径
|
|
|
|
Returns:
|
|
tuple: 包含训练数据特征、训练数据标签、测试数据特征、测试数据标签
|
|
"""
|
|
train_data = pd.read_csv("./data/horse-colic.data", header=None, na_values='?')
|
|
test_data = pd.read_csv("./data/horse-colic.test", header=None, na_values='?')
|
|
|
|
# 处理缺失值,这里简单删除含有缺失值的行
|
|
train_data = train_data.dropna()
|
|
test_data = test_data.dropna()
|
|
|
|
train_X = train_data.iloc[:, :-1].values
|
|
train_y = train_data.iloc[:, -1].values
|
|
test_X = test_data.iloc[:, :-1].values
|
|
test_y = test_data.iloc[:, -1].values
|
|
|
|
return train_X, train_y, test_X, test_y
|
|
|
|
|
|
def sigmoid(z):
|
|
"""
|
|
Sigmoid函数
|
|
|
|
Args:
|
|
z (numpy.ndarray): 输入值
|
|
|
|
Returns:
|
|
numpy.ndarray: Sigmoid函数值
|
|
"""
|
|
return 1 / (1 + np.exp(-z))
|
|
|
|
|
|
def train_logreg(train_X, train_y, learning_rate=0.01, num_iterations=1000):
|
|
"""
|
|
训练对率回归模型
|
|
|
|
Args:
|
|
train_X (numpy.ndarray): 训练数据特征
|
|
train_y (numpy.ndarray): 训练数据标签
|
|
learning_rate (float, optional): 学习率. Defaults to 0.01.
|
|
num_iterations (int, optional): 迭代次数. Defaults to 1000.
|
|
|
|
Returns:
|
|
numpy.ndarray: 训练好的模型权重
|
|
"""
|
|
num_features = train_X.shape[1]
|
|
num_samples = train_X.shape[0]
|
|
|
|
# 初始化权重
|
|
weights = np.zeros(num_features)
|
|
|
|
for i in range(num_iterations):
|
|
# 计算预测值
|
|
z = np.dot(train_X, weights)
|
|
h = sigmoid(z)
|
|
|
|
# 计算梯度
|
|
gradient = np.dot(train_X.T, (h - train_y)) / num_samples
|
|
|
|
# 更新权重
|
|
weights -= learning_rate * gradient
|
|
|
|
return weights
|
|
|
|
|
|
def predict_logreg(test_X, weights):
|
|
"""
|
|
使用训练好的模型进行预测
|
|
|
|
Args:
|
|
test_X (numpy.ndarray): 测试数据特征
|
|
weights (numpy.ndarray): 训练好的模型权重
|
|
|
|
Returns:
|
|
numpy.ndarray: 预测结果
|
|
"""
|
|
z = np.dot(test_X, weights)
|
|
h = sigmoid(z)
|
|
|
|
# 将概率值转换为类别预测0或1
|
|
predictions = (h >= 0.5).astype(int)
|
|
|
|
return predictions
|
|
|
|
if __name__ == "__main__":
|
|
train_path = "./data/horse-colic.data"
|
|
test_path = "./data/horse-colic.test"
|
|
|
|
train_X, train_y, test_X, test_y = load_data(train_path, test_path)
|
|
|
|
trained_weights = train_logreg(train_X, train_y)
|
|
|
|
predictions = predict_logreg(test_X, trained_weights)
|
|
|
|
# 可以在这里进一步评估预测结果的准确性等指标,例如:
|
|
accuracy = np.mean(predictions == test_y)
|
|
print("预测准确率:", accuracy)
|