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

This commit is contained in:
2024-10-31 15:45:00 +08:00
parent 43a7104511
commit be7d201625
+25 -23
View File
@@ -27,24 +27,24 @@ name = "邓龙树"
# user_code.py # user_code.py
import numpy as np import numpy as np
from sklearn.preprocessing import StandardScaler
def sigmoid(z): def sigmoid(z):
"""计算sigmoid函数""" """计算 sigmoid 函数"""
return 1 / (1 + np.exp(-z)) return 1 / (1 + np.exp(-z))
def train_logreg(X, y, learning_rate=0.01, iterations=1000, l2_penalty=0.01): def train_logreg(X, y, learning_rate=0.01, iterations=1000, l2_penalty=0.01):
"""对率回归训练函数,包括L2正则化。""" """对率回归训练函数,包括 L2 正则化。"""
weights = np.zeros(X.shape[1]) weights = np.zeros(X.shape[1])
for _ in range(iterations): for _ in range(iterations):
z = np.dot(X, weights) z = np.dot(X, weights)
predictions = sigmoid(z) predictions = sigmoid(z)
gradient = np.dot(X.T, (predictions - y)) / y.size + l2_penalty * weights # 添加L2正则化项 gradient = np.dot(X.T, (predictions - y)) / y.size + l2_penalty * weights # 添加 L2 正则化项
weights -= learning_rate * gradient weights -= learning_rate * gradient
return weights return weights
def predict_logreg(X, weights): def predict_logreg(X, weights):
"""使用训练好的权重预测标签""" """使用训练好的权重预测标签"""
z = np.dot(X, weights) z = np.dot(X, weights)
@@ -69,15 +69,15 @@ def load_data(filepath):
if len(data[i]) < max_columns: if len(data[i]) < max_columns:
data[i].extend(['0'] * (max_columns - len(data[i]))) # 使用'0'填充缺失列 data[i].extend(['0'] * (max_columns - len(data[i]))) # 使用'0'填充缺失列
# 转换成numpy数组,处理缺失值 # 转换成 numpy 数组,处理缺失值
data_array = [] data_array = []
for row in data: for row in data:
cleaned_row = [] cleaned_row = []
for value in row: for value in row:
if value == '?': # 替换无法转换的值 if value == '?': # 替换无法转换的值
cleaned_row.append(0) # 使用0填充 cleaned_row.append(0) # 使用 0 填充
else: else:
cleaned_row.append(float(value)) # 转换为float cleaned_row.append(float(value)) # 转换为 float
data_array.append(cleaned_row) data_array.append(cleaned_row)
data_array = np.array(data_array) data_array = np.array(data_array)
@@ -85,36 +85,39 @@ def load_data(filepath):
return data_array[:, :-1], data_array[:, -1] return data_array[:, :-1], data_array[:, -1]
def preprocess_data(X, y): def preprocess_data(X, y):
"""数据预处理,合并特征23的值,并删除特征3和特征24~28,以及特征缩放。""" """数据预处理,合并特征 23 的值,并删除特征 3 和特征 24~28,以及使用库函数进行特征缩放和归一化。"""
# 合并特征23的值和删除特征 # 合并特征 23 的值和删除特征
X, y = preprocess_data_base(X, y) X, y = preprocess_data_base(X, y)
# 特征缩放:标准化特征 # 使用库函数进行标准化处理
X_mean = np.mean(X, axis=0) scaler = StandardScaler()
X_std = np.std(X, axis=0) X = scaler.fit_transform(X)
X = (X - X_mean) / X_std
return X, y return X, y
def preprocess_data_base(X, y): def preprocess_data_base(X, y):
"""数据预处理,合并特征23的值,并删除特征3和特征24~28。""" """数据预处理,合并特征 23 的值,并删除特征 3 和特征 24~28。"""
# 合并特征23的值:将2(死亡)和3(安乐死)合并为0(死亡) # 合并特征 23 的值:将 2(死亡)和 3(安乐死)合并为 0(死亡)
y = np.where(y == 2, 0, y) # 将值2替换为0 y = np.where(y == 2, 0, y) # 将值 2 替换为 0
y = np.where(y == 3, 0, y) # 将值3替换为0 y = np.where(y == 3, 0, y) # 将值 3 替换为 0
# 删除特征3和特征24~28(假设特征索引从0开始) # 删除特征 3 和特征 24~28(假设特征索引从 0 开始)
X = np.delete(X, [2] + list(range(23, 28)), axis=1) X = np.delete(X, [2] + list(range(23, 28)), axis=1)
return X, y return X, y
def evaluate_model(X_test, y_test, weights):
"""评估模型准确率"""
predictions = predict_logreg(X_test, weights)
accuracy = np.mean(predictions == y_test)
return accuracy
if __name__ == "__main__": if __name__ == "__main__":
# 加载数据 # 加载数据
X, y = load_data("./data/horse-colic.data") X, y = load_data("./data/horse-colic.data")
# 数据预处理 # 数据预处理
X, y = preprocess_data(X, y) X, y = preprocess_data(X, y)
X_mean = np.mean(X, axis=0) # 保存训练数据的均值
X_std = np.std(X, axis=0) # 保存训练数据的标准差
# 尝试不同的正则化强度 # 尝试不同的正则化强度
l2_penalties = [0.01, 0.1, 1.0] l2_penalties = [0.01, 0.1, 1.0]
@@ -125,12 +128,11 @@ if __name__ == "__main__":
# 加载测试数据并进行预处理 # 加载测试数据并进行预处理
X_test, y_test = load_data("./data/horse-colic.test") X_test, y_test = load_data("./data/horse-colic.test")
X_test = (X_test - X_mean) / X_std # 使用训练数据的均值和标准差进行标准化 X_test, y_test = preprocess_data(X_test, y_test)
X_test, y_test = preprocess_data_base(X_test, y_test) # 应用其他预处理步骤
# 评估模型准确率 # 评估模型准确率
accuracy = evaluate_model(X_test, y_test, weights) accuracy = evaluate_model(X_test, y_test, weights)
print(f"L2正则化强度: {l2_penalty}, 准确率: {accuracy:.4f}") print(f"L2 正则化强度: {l2_penalty}, 准确率: {accuracy:.4f}")
# 打印预测结果和实际结果(以最后一个正则化强度为例) # 打印预测结果和实际结果(以最后一个正则化强度为例)
predictions = predict_logreg(X_test, weights) predictions = predict_logreg(X_test, weights)