diff --git a/function.py b/function.py index 7da91dd..ef0bc3d 100644 --- a/function.py +++ b/function.py @@ -1,4 +1,3 @@ -# function.py import numpy as np import os from sklearn.preprocessing import StandardScaler @@ -19,17 +18,13 @@ from sklearn.preprocessing import StandardScaler - numpy: 2.1.1 """ - """ 请在这里填写学号和姓名,不填写即成绩无效。 姓名请直接**填写中文** 例如: name = "张三" """ -id = "202210423211" -name = "梅晓东" - - -import numpy as np +id = "202210423110" +name = "罗浩" def sigmoid(z): @@ -79,76 +74,71 @@ def load_data(filepath): data, labels = [], [] print(f"加载数据中: {filepath}") - with open(filepath, 'r') - as f: - for line in f: - line = line.strip().split() - if len(line) >= 1 and line[-1].isdigit(): - line_data = [0 if x == '?' else float(x) for x in line[:-1]] - data.append(line_data) - labels.append(int(line[-1])) - data = np.array(data) - labels = np.array(labels) - num_features = data.shape[1] - indices_to_remove = [2] - if num_features > 23: - indices_to_remove.extend(range(23, min(28, num_features))) - data = np.delete(data, indices_to_remove, axis=1) - target_variable = np.where((labels == 2) | (labels == 3), 0, 1) - print(f"数据加载成功,数据形状: {data.shape}, 标签形状: {target_variable.shape}") - return data, target_variable - def k_fold_cross_validation(X, y, k=5, learning_rate=0.001, iterations=10000, batch_size=64, lambda_reg=0.1): - """K折交叉验证。""" - m = X.shape[0] - fold_size = m // k - accuracies = [] - for i in range(k): - val_start = i * fold_size - val_end = (i + 1) * fold_size if i != k - 1 else m - X_val = X[val_start:val_end] - y_val = y[val_start:val_end] - X_train = np.concatenate((X[:val_start], X[val_end:])) - y_train = np.concatenate((y[:val_start], y[val_end:])) - weights = train_logreg(X_train, y_train, learning_rate, iterations, batch_size, lambda_reg) - val_accuracy = np.mean([predict_logreg(X_val[j], weights) == y_val[j] for j in range(len(y_val))]) - accuracies.append(val_accuracy) - print(f"Fold {i + 1}/{k} - 验证准确率: {val_accuracy:.4f}") - return np.mean(accuracies) - if __name__ == "__main__": - # 加载训练集 - try: - X_train, y_train = load_data("./data/horse-colic.data") - except FileNotFoundError as e: - print(e) - exit() - # 数据归一化处理 - scaler = StandardScaler() - X_train = scaler.fit_transform(X_train) # 使用训练集进行标准化 - # k折交叉验证 - avg_accuracy = k_fold_cross_validation(X_train, y_train, k=5, learning_rate=0.01, iterations=1000) - print(f"K折交叉验证的平均准确率: {avg_accuracy:.4f}") - # 加载测试集 - try: - X_test, y_test = load_data("./data/horse-colic.test") - except FileNotFoundError - as e: - print(e) - exit() - # 测试集也需要进行相同的归一化 - X_test = scaler.transform(X_test) # 使用相同的Scaler进行测试集的标准化 - # 训练逻辑回归模型 - 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]}") + with open(filepath, 'r') as f: + for line in f: + line = line.strip().split() + if len(line) >= 1 and line[-1].isdigit(): + line_data = [0 if x == '?' else float(x) for x in line[:-1]] + data.append(line_data) + labels.append(int(line[-1])) + data = np.array(data) + labels = np.array(labels) + num_features = data.shape[1] + indices_to_remove = [2] + if num_features > 23: + indices_to_remove.extend(range(23, min(28, num_features))) + data = np.delete(data, indices_to_remove, axis=1) + target_variable = np.where((labels == 2) | (labels == 3), 0, 1) + print(f"数据加载成功,数据形状: {data.shape}, 标签形状: {target_variable.shape}") + return data, target_variable +def k_fold_cross_validation(X, y, k=5, learning_rate=0.001, iterations=10000, batch_size=64, lambda_reg=0.1): + """K折交叉验证。""" + m = X.shape[0] + fold_size = m // k + accuracies = [] + for i in range(k): + val_start = i * fold_size + val_end = (i + 1) * fold_size if i != k - 1 else m + X_val = X[val_start:val_end] + y_val = y[val_start:val_end] + X_train = np.concatenate((X[:val_start], X[val_end:])) + y_train = np.concatenate((y[:val_start], y[val_end:])) + weights = train_logreg(X_train, y_train, learning_rate, iterations, batch_size, lambda_reg) + val_accuracy = np.mean([predict_logreg(X_val[j], weights) == y_val[j] for j in range(len(y_val))]) + accuracies.append(val_accuracy) + print(f"Fold {i + 1}/{k} - 验证准确率: {val_accuracy:.4f}") + return np.mean(accuracies) - # 加载测试集 - X_test, y_test = load_data("./data/horse-colic.test") - - # 训练对率回归模型 - weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000) - - # 测试单个数据 - y_pred = predict_logreg(X_test[11], weights) +if __name__ == "__main__": + # 加载训练集 + try: + X_train, y_train = load_data("./data/horse-colic.data") + except FileNotFoundError as e: + print(e) + exit() + + # 数据归一化处理 + scaler = StandardScaler() + X_train = scaler.fit_transform(X_train) # 使用训练集进行标准化 + + # k折交叉验证 + avg_accuracy = k_fold_cross_validation(X_train, y_train, k=5, learning_rate=0.01, iterations=1000) + print(f"K折交叉验证的平均准确率: {avg_accuracy:.4f}") + + # 加载测试集 + try: + X_test, y_test = load_data("./data/horse-colic.test") + except FileNotFoundError as e: + print(e) + exit() + + # 测试集也需要进行相同的归一化 + X_test = scaler.transform(X_test) # 使用相同的Scaler进行测试集的标准化 + + # 训练逻辑回归模型 + 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]}")