From 6936ea4ee58a592b9033655ada81a233782e8694 Mon Sep 17 00:00:00 2001 From: dong <2938690875@qq.com> Date: Fri, 1 Nov 2024 00:14:13 +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 | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/function.py b/function.py index 2219d25..9d768a6 100644 --- a/function.py +++ b/function.py @@ -1,14 +1,8 @@ import numpy as np import pandas as pd from sklearn.preprocessing import StandardScaler -from sklearn.model_selection import train_test_split -""" -实验说明: -- 自行编写实现数据加载与预测病马死亡的对率回归算法。 -""" - id = "202210423130" name = "董秋" @@ -27,7 +21,7 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000, lambda_reg=0.01): m, n = X.shape weights = np.zeros(n) - for _ in range(iterations): + for i in range(iterations): z = np.dot(X, weights) predictions = 1 / (1 + np.exp(-z)) gradient = np.dot(X.T, (predictions - y)) / m @@ -37,6 +31,9 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000, lambda_reg=0.01): weights -= learning_rate * gradient + # 动态调整学习率,逐渐减小 + learning_rate *= 0.99 # 每次迭代将学习率乘以0.99 + return weights @@ -92,7 +89,6 @@ def preprocess_data(X): if __name__ == "__main__": - # 以下代码可用于测试,可自行随意修改 # 加载训练集 X_train, y_train = load_data("./data/horse-colic.data") X_train = preprocess_data(X_train) @@ -102,7 +98,7 @@ if __name__ == "__main__": X_test = preprocess_data(X_test) # 训练对率回归模型 - weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000, lambda_reg=0.01) + weights = train_logreg(X_train, y_train, learning_rate=0.1, iterations=1000, lambda_reg=0.01) # 测试单个数据 y_pred = predict_logreg(X_test[11], weights)