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

This commit is contained in:
2024-11-02 12:04:57 +08:00
parent 3c0478ec60
commit 8ae1fec049
+50 -35
View File
@@ -1,27 +1,50 @@
# 学号和姓名
id = "202210423230"
name = "尹家立"
# function.py # function.py
import numpy as np import numpy as np
from sklearn.linear_model import LogisticRegression from sklearn.linear_model import LogisticRegression
from sklearn.impute import KNNImputer
def train_logreg(X, y, max_iter=1000):
""" """
使用liblinear求解器实现逻辑回归训练函数。 实验说明:
- 自行编写实现数据加载与预测病马死亡的对率回归算法。
注意事项:
- 在 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
"""
"""
请在这里填写学号和姓名,不填写即成绩无效。
"""
id = "202210423230"
name = "尹家立"
# Sigmoid function
def sigmoid(inX):
"""Sigmoid激活函数"""
return 1.0 / (1 + np.exp(-inX))
def train_logreg(X, y, learning_rate=0.01, iterations=5000, solver='liblinear'):
"""
对率回归训练函数。
Args: Args:
- X (numpy array): 特征数据。 - X (numpy array): 特征数据。
- y (numpy array): 标签数据。 - y (numpy array): 标签数据。
- max_iter (int): 最大迭代次数。 - learning_rate (float): 学习速率。
- iterations (int): 最大迭代次数。
- solver (str): 使用的求解器。默认值为'liblinear'。
Returns: Returns:
- numpy array: 训练得到的权重。 - numpy array: 训练得到的权重。
""" """
# 使用liblinear作为求解器进行对率回归训练 # 使用liblinear作为求解器进行对率回归训练
model = LogisticRegression(solver='liblinear', max_iter=max_iter) model = LogisticRegression(solver=solver, max_iter=iterations)
model.fit(X, y) model.fit(X, y)
# 返回模型的系数作为权重 # 返回模型的系数作为权重
@@ -38,19 +61,12 @@ def predict_logreg(inX, weights):
Returns: Returns:
- int: 预测的分类标签。 - int: 预测的分类标签。
""" """
# 计算预测概率 prob = sigmoid(np.dot(inX, weights)) # 计算预测概率
prob = sigmoid(np.dot(inX, weights)) return 1 if prob > 0.5 else 0 # 返回二分类标签
# 将概率转为二进制分类
return 1 if prob >= 0.5 else 0
def sigmoid(z):
"""Sigmoid activation function"""
return 1 / (1 + np.exp(-z))
def load_data(filepath): def load_data(filepath):
""" """
从文件中加载数据并使用KNNImputer对缺失数据进行预测填充。 从文件中加载数据,若不自行正确实现,则无法通过评测,**不记录成绩**。
Args: Args:
- filepath (str): 数据文件路径。 - filepath (str): 数据文件路径。
@@ -60,35 +76,34 @@ def load_data(filepath):
- np.array: 标签数据。 - np.array: 标签数据。
""" """
data = [] data = []
# 读取并解析数据
with open(filepath, 'r') as file: with open(filepath, 'r') as file:
for line in file: for line in file:
line_data = line.strip().split() currLine = line.strip().split('\t')
line_data = [float(val) if val != '?' else np.nan for val in line_data] lineData = [float(x) if x != '?' else np.nan for x in currLine]
data.append(line_data) data.append(lineData)
# 转换为 numpy 数组
data = np.array(data) data = np.array(data)
# 分离特征和标签 (假设最后一列是标签)
X = data[:, :-1] X = data[:, :-1]
y = data[:, -1] y = data[:, -1]
# 使用 KNNImputer 对缺失值进行预测填充 # 使用简单的列均值填补缺失值
imputer = KNNImputer(n_neighbors=5) col_mean = np.nanmean(X, axis=0)
X = imputer.fit_transform(X) inds = np.where(np.isnan(X))
X[inds] = np.take(col_mean, inds[1])
return X, y.astype(int) return X, y.astype(int)
if __name__ == "__main__": if __name__ == "__main__":
# 加载训练和测试数据 # 以下代码可用于测试,可自行随意修改
# 加载训练集
X_train, y_train = load_data("./data/horse-colic.data") X_train, y_train = load_data("./data/horse-colic.data")
# 加载测试集
X_test, y_test = load_data("./data/horse-colic.test") X_test, y_test = load_data("./data/horse-colic.test")
# 使用liblinear训练对率回归模型 # 训练对率回归模型
weights = train_logreg(X_train, y_train, max_iter=1000) weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000)
# 测试单个样本的预测 # 测试单个数据
y_pred = predict_logreg(X_test[11], weights) y_pred = predict_logreg(X_test[11], weights)
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}") print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")