From d8359b4502d8f4f839bdf9173995514701f98f25 Mon Sep 17 00:00:00 2001 From: cy <939601402@qq.com> Date: Sat, 26 Oct 2024 18:21:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20function.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- function.py | 68 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 29 deletions(-) diff --git a/function.py b/function.py index ec1b695..f5a1b7d 100644 --- a/function.py +++ b/function.py @@ -30,7 +30,7 @@ name = "程悦" def sigmoid(z): return 1.0 / (1 + np.exp(-z)) -def train_logreg(X, y, learning_rate=0.01, iterations=1000): +def train_logreg(X, y, learning_rate=0.01, iterations=1000, regularization='l2', lambda_=0.01): """ 对率回归训练函数。 @@ -47,6 +47,12 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000): z = np.dot(X, weights) h = sigmoid(z) gradient = np.dot(X.T, (h - y)) / m + # 正则化 + if regularization == 'l2': + gradient += lambda_ * weights / m + elif regularization == 'l1': + gradient += lambda_ * np.sign(weights) / m + weights -= learning_rate * gradient return weights @@ -103,43 +109,47 @@ def load_data(filepath): return data, target_variable +class CustomLogisticRegression(BaseEstimator, ClassifierMixin): + def __init__(self, learning_rate=0.01, iterations=1000, regularization='l2', lambda_=0.01): + self.learning_rate = learning_rate + self.iterations = iterations + self.regularization = regularization + self.lambda_ = lambda_ + self.weights = None + + def fit(self, X, y): + self.weights = train_logreg(X, y, self.learning_rate, self.iterations, + self.regularization, self.lambda_) + + def predict(self, X): + return np.array([predict_logreg(x, self.weights) for x in X]) + if __name__ == "__main__": # 加载训练集和测试集 X_train, y_train = load_data("./data/horse-colic.data") X_test, y_test = load_data("./data/horse-colic.test") - # 使用GridSearchCV进行超参数调优 +# 定义网格搜索的参数范围 param_grid = { 'learning_rate': [0.001, 0.01, 0.1], - 'iterations': [100, 500, 1000] + 'iterations': [100, 500, 1000], + 'regularization': ['l1', 'l2'], + 'lambda_': [0.01, 0.1, 1.0] } - # 定义自定义逻辑回归模型 - 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]) + # 创建逻辑回归模型 + model = CustomLogisticRegression() # 网格搜索 - 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} + grid_search = GridSearchCV(model, param_grid, cv=5) + grid_search.fit(X_train, y_train) - print(f"最佳参数: {best_params}, 最佳准确率: {best_accuracy}") \ No newline at end of file + # 使用最佳模型进行预测 + best_model = grid_search.best_estimator_ + y_pred = best_model.predict(X_test) + + # 测试单个数据 + single_prediction = best_model.predict([X_test[11]]) + print(f"最佳参数组合: {grid_search.best_params_}") + print(f"最佳准确率: {grid_search.best_score_}") + print(f"预测结果: {single_prediction[0]}, 实际结果: {y_test[11]}") \ No newline at end of file