diff --git a/function.py b/function.py index cde8fce..c36e73e 100644 --- a/function.py +++ b/function.py @@ -27,11 +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): +# 训练逻辑回归模型 +def train_logreg(X, y, learning_rate, iterations=1000): m, n = X.shape weights = np.zeros(n) bias = 0 @@ -42,13 +45,15 @@ def train_logreg(X, y, learning_rate, iterations): db = (1/m) * np.sum(h - y) weights -= learning_rate * dw bias -= learning_rate * db - return weights + return weights, bias -def predict_logreg(inX, weights): - z = np.dot(inX, weights) +# 使用逻辑回归模型进行预测 +def predict_logreg(inX, weights, bias): + z = np.dot(inX, weights) + bias h = sigmoid(z) return 1 if h >= 0.5 else 0 +# 加载数据 def load_data(filepath): data = [] with open(filepath, 'r') as file: @@ -64,24 +69,36 @@ def load_data(filepath): y = data[:, -1] return X, y +# 寻找最佳学习率 +def find_best_learning_rate(X, y, learning_rates, iterations=1000): + best_accuracy = 0 + best_learning_rate = 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) + if accuracy > best_accuracy: + best_accuracy = accuracy + best_learning_rate = rate + best_weights = weights + best_bias = bias + return best_learning_rate, best_accuracy, best_weights, best_bias + if __name__ == "__main__": X_train, y_train = load_data("./data/horse-colic.data") X_test, y_test = load_data("./data/horse-colic.test") - best_learning_rate = None - best_accuracy = 0 - learning_rates = [0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01] - iterations = 1000 - for lr in learning_rates: - weights = train_logreg(X_train, y_train, learning_rate=lr, iterations=iterations) - correct_count = 0 - for i in range(len(X_test)): - y_pred = predict_logreg(X_test[i], weights) - if y_pred == y_test[i]: - correct_count += 1 - accuracy = correct_count / len(X_test) - if accuracy > best_accuracy: - best_accuracy = accuracy - best_learning_rate = lr - best_weights = train_logreg(X_train, y_train, learning_rate=best_learning_rate, iterations=iterations) - y_pred = predict_logreg(X_test[11], best_weights) - print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}") \ No newline at end of file + + # 定义要尝试的学习率列表 + learning_rates = np.linspace(0.001, 0.1, 50) + + # 寻找最佳学习率 + 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) + print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")