diff --git a/function.py b/function.py index 0d98f2b..ec1b695 100644 --- a/function.py +++ b/function.py @@ -1,6 +1,8 @@ # function.py import numpy as np from sklearn.preprocessing import StandardScaler +from sklearn.model_selection import GridSearchCV +from sklearn.linear_model import LogisticRegression """ 实验说明: @@ -102,17 +104,42 @@ def load_data(filepath): return data, target_variable if __name__ == "__main__": - # 以下代码可用于测试,可自行随意修改 - # 加载训练集 + # 加载训练集和测试集 X_train, y_train = load_data("./data/horse-colic.data") - - # 加载测试集 X_test, y_test = load_data("./data/horse-colic.test") - # 训练对率回归模型 - weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000, re - ='l1') + # 使用GridSearchCV进行超参数调优 + param_grid = { + 'learning_rate': [0.001, 0.01, 0.1], + 'iterations': [100, 500, 1000] + } - # 测试单个数据 - y_pred = predict_logreg(X_test[11], weights) - print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}") \ No newline at end of file + # 定义自定义逻辑回归模型 + class CustomLogisticRegression: + def __init__(self, learning_rate=0.01, iterations=1000): + self.learning_rate = learning_rate + self.iterations = iterations + self.weights = None + + def fit(self, X, y): + self.weights = train_logreg(X, y, self.learning_rate, self.iterations) + + def predict(self, X): + return np.array([predict_logreg(x, self.weights) for x in X]) + + # 网格搜索 + best_accuracy = 0 + best_params = {} + + for lr in param_grid['learning_rate']: + for it in param_grid['iterations']: + model = CustomLogisticRegression(learning_rate=lr, iterations=it) + model.fit(X_train, y_train) + y_pred = model.predict(X_test) + accuracy = np.mean(y_pred == y_test) + print(f"学习率: {lr}, 迭代次数: {it}, 准确率: {accuracy}") + if accuracy > best_accuracy: + best_accuracy = accuracy + best_params = {'learning_rate': lr, 'iterations': it} + + print(f"最佳参数: {best_params}, 最佳准确率: {best_accuracy}") \ No newline at end of file