diff --git a/function.py b/function.py index c36e73e..c50b879 100644 --- a/function.py +++ b/function.py @@ -27,14 +27,14 @@ name = "章程鸿" # 这里写自己的代码实现 import numpy as np -from sklearn.metrics import accuracy_score -# 定义sigmoid函数 def sigmoid(z): return 1 / (1 + np.exp(-z)) -# 训练逻辑回归模型 -def train_logreg(X, y, learning_rate, iterations=1000): +def train_logreg(X, y, learning_rate=0.005, iterations=1000): + """ + 修改处:将学习率从 0.01 调整为 0.005。 + """ m, n = X.shape weights = np.zeros(n) bias = 0 @@ -45,15 +45,13 @@ def train_logreg(X, y, learning_rate, iterations=1000): db = (1/m) * np.sum(h - y) weights -= learning_rate * dw bias -= learning_rate * db - return weights, bias + return weights -# 使用逻辑回归模型进行预测 -def predict_logreg(inX, weights, bias): - z = np.dot(inX, weights) + bias +def predict_logreg(inX, weights): + z = np.dot(inX, weights) h = sigmoid(z) return 1 if h >= 0.5 else 0 -# 加载数据 def load_data(filepath): data = [] with open(filepath, 'r') as file: @@ -69,36 +67,38 @@ def load_data(filepath): y = data[:, -1] return X, y -# 寻找最佳学习率 -def find_best_learning_rate(X, y, learning_rates, iterations=1000): +def calculate_accuracy(X, y, weights): + predictions = [predict_logreg(x, weights) for x in X] + correct_predictions = sum([1 for pred, actual in zip(predictions, y) if pred == actual]) + accuracy = correct_predictions / len(y) + return accuracy + +def find_best_learning_rate(X_train, y_train, X_test, y_test, learning_rates, iterations=1000): best_accuracy = 0 - best_learning_rate = None + best_lr = None best_weights = None - best_bias = None - for rate in learning_rates: - weights, bias = train_logreg(X, y, rate, iterations) - y_pred = np.array([predict_logreg(x, weights, bias) for x in X]) - accuracy = accuracy_score(y, y_pred) + + for lr in learning_rates: + weights = train_logreg(X_train, y_train, learning_rate=lr, iterations=iterations) + accuracy = calculate_accuracy(X_test, y_test, weights) + print(f"学习率: {lr}, 测试集准确率: {accuracy}") + if accuracy > best_accuracy: best_accuracy = accuracy - best_learning_rate = rate + best_lr = lr best_weights = weights - best_bias = bias - return best_learning_rate, best_accuracy, best_weights, best_bias + + return best_lr, best_accuracy, best_weights if __name__ == "__main__": X_train, y_train = load_data("./data/horse-colic.data") X_test, y_test = load_data("./data/horse-colic.test") - # 定义要尝试的学习率列表 - learning_rates = np.linspace(0.001, 0.1, 50) + learning_rates = [0.0001, 0.001, 0.005, 0.01, 0.05, 0.1] # 不同的学习率 + best_lr, best_accuracy, best_weights = find_best_learning_rate(X_train, y_train, X_test, y_test, learning_rates) + + print(f"最佳学习率: {best_lr}, 测试集准确率: {best_accuracy}") - # 寻找最佳学习率 - best_learning_rate, best_accuracy, best_weights, best_bias = find_best_learning_rate(X_train, y_train, learning_rates) - - print(f"最佳学习率: {best_learning_rate}") - print(f"最佳准确率: {best_accuracy}") - - # 使用最佳权重和偏置进行预测 - y_pred = predict_logreg(X_test[11], best_weights, best_bias) + # 使用最佳学习率训练最终模型 + y_pred = predict_logreg(X_test[11], best_weights) print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")