diff --git a/function.py b/function.py index cfa2046..c187c05 100644 --- a/function.py +++ b/function.py @@ -27,115 +27,52 @@ name = "邓龙树" # user_code.py import numpy as np +from sklearn.preprocessing import MinMaxScaler -from sklearn.preprocessing import StandardScaler - -def sigmoid(z): - """计算 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]) - +def train_logreg(X, y, learning_rate=0.01, iterations=1000): + num_samples, num_features = X.shape + weights = np.zeros(num_features) for _ in range(iterations): z = np.dot(X, weights) - predictions = sigmoid(z) - gradient = np.dot(X.T, (predictions - y)) / y.size + l2_penalty * weights # 添加 L2 正则化项 + h = 1 / (1 + np.exp(-z)) + gradient = np.dot(X.T, (h - y)) / num_samples weights -= learning_rate * gradient - return weights -def predict_logreg(X, weights): - """使用训练好的权重预测标签""" - z = np.dot(X, weights) - predictions = sigmoid(z) - return np.where(predictions >= 0.5, 1, 0) +def predict_logreg(inX, weights): + z = np.dot(inX, weights) + return 1 if 1 / (1 + np.exp(-z)) > 0.5 else 0 def load_data(filepath): - """从文件中加载数据,处理不规则列数问题""" - with open(filepath, 'r') as file: - lines = file.readlines() - - max_columns = 0 - data = [] - - for line in lines: - parts = line.strip().split() - max_columns = max(max_columns, len(parts)) - 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) - + data = np.genfromtxt(filepath, delimiter=',', dtype=np.float64) + # 处理缺失值 + data[np.isnan(data)] = 0 + X = data[:, :-1] + y = data[:, -1] + # 处理特征 23,合并取值 2 和 3 为 0 + y[y == 2] = 0 + y[y == 3] = 0 + # 去掉特征 3、特征 24~28 + X = np.delete(X, [2, 23], axis=1) 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__": - # 加载数据 - 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] - - 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, y_test = load_data("./data/horse-colic.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}") + # 对测试集特征进行归一化 + X_test = scaler.transform(X_test) - # 打印预测结果和实际结果(以最后一个正则化强度为例) - predictions = predict_logreg(X_test, weights) - for i in range(min(10, len(y_test))): - print(f"预测结果: {predictions[i]}, 实际结果: {y_test[i]}") \ No newline at end of file + # 训练对率回归模型 + weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000) + + # 测试单个数据 + y_pred = predict_logreg(X_test[11], weights) + print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}") \ No newline at end of file