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

This commit is contained in:
2024-10-31 16:09:34 +08:00
parent 323ab2c1f7
commit 9c457a01db
+34 -97
View File
@@ -27,115 +27,52 @@ name = "邓龙树"
# user_code.py # user_code.py
import numpy as np import numpy as np
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import StandardScaler def train_logreg(X, y, learning_rate=0.01, iterations=1000):
num_samples, num_features = X.shape
def sigmoid(z): weights = np.zeros(num_features)
"""计算 sigmoid 函数"""
return 1 / (1 + np.exp(-z))
def train_logreg(X, y, learning_rate=0.01, iterations=1000, l2_penalty=0.01):
"""对率回归训练函数,包括 L2 正则化。"""
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) h = 1 / (1 + np.exp(-z))
gradient = np.dot(X.T, (predictions - y)) / y.size + l2_penalty * weights # 添加 L2 正则化项 gradient = np.dot(X.T, (h - y)) / num_samples
weights -= learning_rate * gradient weights -= learning_rate * gradient
return weights return weights
def predict_logreg(X, weights): def predict_logreg(inX, weights):
"""使用训练好的权重预测标签""" z = np.dot(inX, weights)
z = np.dot(X, weights) return 1 if 1 / (1 + np.exp(-z)) > 0.5 else 0
predictions = sigmoid(z)
return np.where(predictions >= 0.5, 1, 0)
def load_data(filepath): def load_data(filepath):
"""从文件中加载数据,处理不规则列数问题""" data = np.genfromtxt(filepath, delimiter=',', dtype=np.float64)
with open(filepath, 'r') as file: # 处理缺失值
lines = file.readlines() data[np.isnan(data)] = 0
X = data[:, :-1]
max_columns = 0 y = data[:, -1]
data = [] # 处理特征 23,合并取值 2 和 3 为 0
y[y == 2] = 0
for line in lines: y[y == 3] = 0
parts = line.strip().split() # 去掉特征 3、特征 24~28
max_columns = max(max_columns, len(parts)) X = np.delete(X, [2, 23], axis=1)
data.append(parts)
# 填充缺失的列
for i in range(len(data)):
if len(data[i]) < max_columns:
data[i].extend(['0'] * (max_columns - len(data[i]))) # 使用'0'填充缺失列
# 转换成 numpy 数组,处理缺失值
data_array = []
for row in data:
cleaned_row = []
for value in row:
if value == '?': # 替换无法转换的值
cleaned_row.append(0) # 使用 0 填充
else:
cleaned_row.append(float(value)) # 转换为 float
data_array.append(cleaned_row)
data_array = np.array(data_array)
return data_array[:, :-1], data_array[:, -1]
def preprocess_data(X, y):
"""数据预处理,合并特征 23 的值,并删除特征 3 和特征 24~28,以及使用库函数进行特征缩放和归一化。"""
# 合并特征 23 的值和删除特征
X, y = preprocess_data_base(X, y)
# 选择特征 23 作为目标变量,将其取值 2(死亡)和 3(安乐死)合并为一个值 0(死亡)
y = np.where(y == 2, 0, y)
y = np.where(y == 3, 0, y)
# 使用库函数进行标准化处理
scaler = StandardScaler()
X = scaler.fit_transform(X)
return X, y return X, y
def preprocess_data_base(X, y):
"""数据预处理,删除特征 3 和特征 24~28。"""
# 删除特征 3 和特征 24~28(假设特征索引从 0 开始)
X = np.delete(X, [2] + list(range(23, 28)), axis=1)
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_train, y_train = load_data("./data/horse-colic.data")
# 数据预处理 # 对训练集特征进行归一化
X, y = preprocess_data(X, y) scaler = MinMaxScaler()
X_train = scaler.fit_transform(X_train)
# 尝试不同的正则化强度 # 加载测试集
l2_penalties = [0.01, 0.1, 1.0] X_test, y_test = load_data("./data/horse-colic.test")
for l2_penalty in l2_penalties:
# 训练模型
weights = train_logreg(X, y, learning_rate=0.01, l2_penalty=l2_penalty)
# 加载测试数据并进行预处理 # 对测试集特征进行归一化
X_test, y_test = load_data("./data/horse-colic.test") X_test = scaler.transform(X_test)
X_test, y_test = preprocess_data(X_test, y_test)
# 评估模型准确率
accuracy = evaluate_model(X_test, y_test, weights)
print(f"L2 正则化强度: {l2_penalty}, 准确率: {accuracy:.4f}")
# 打印预测结果和实际结果(以最后一个正则化强度为例) # 训练对率回归模型
predictions = predict_logreg(X_test, weights) weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000)
for i in range(min(10, len(y_test))):
print(f"预测结果: {predictions[i]}, 实际结果: {y_test[i]}") # 测试单个数据
y_pred = predict_logreg(X_test[11], weights)
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")