From e9d8b95d6ca77034e4478c1ee08e56ed641929a2 Mon Sep 17 00:00:00 2001 From: zhangchenghong <1598349064@qq.com> Date: Fri, 1 Nov 2024 21:03: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 | 56 +++++++---------------------------------------------- 1 file changed, 7 insertions(+), 49 deletions(-) diff --git a/function.py b/function.py index 5573371..cae00fc 100644 --- a/function.py +++ b/function.py @@ -32,26 +32,9 @@ from sklearn.preprocessing import StandardScaler def sigmoid(z): return 1.0 / (1 + np.exp(-z)) -def train_logreg(X, y, learning_rate=0.05, iterations=1500, batch_size=64, regularization_strength=0.1): - """ - 对率回归训练函数。 - - Args: - - X (numpy array): 特征数据。 - - y (numpy array): 标签数据。 - - learning_rate (float): 学习率。 - - iterations (int): 迭代次数。 - - batch_size (int): 批量大小。 - - regularization_strength (float): L2正则化强度。 - - Returns: - - numpy array: 训练得到的权重。 - """ +def train_logreg(X, y, learning_rate=0.01, iterations=1000, batch_size=32, regularization_strength=0.1): m, n = X.shape weights = np.random.randn(n) * 0.01 - best_weights = weights.copy() - best_accuracy = 0 - wait = 0 for i in range(iterations): indices = np.random.permutation(m) @@ -61,48 +44,20 @@ def train_logreg(X, y, learning_rate=0.05, iterations=1500, batch_size=64, regul for j in range(0, m, batch_size): X_batch = X_shuffled[j:j + batch_size] y_batch = y_shuffled[j:j + batch_size] + z = np.dot(X_batch, weights) h = sigmoid(z) gradient = np.dot(X_batch.T, (h - y_batch)) / len(y_batch) + regularization_strength * weights weights -= learning_rate * gradient - # 计算训练集准确率 - train_accuracy = accuracy(X, y, weights) - if train_accuracy > best_accuracy: - best_accuracy = train_accuracy - best_weights = weights.copy() - wait = 0 - else: - wait += 1 - - return best_weights + return weights def predict_logreg(X, weights): - """ - 对率回归预测函数。 - - Args: - - X (numpy array): 输入待分类的样本。 - - weights (numpy array): 训练得到的权重。 - - Returns: - - numpy array: 预测的分类标签。 - """ z = np.dot(X, weights) h = sigmoid(z) - return np.where(h > 0.5, 1, 0) + return np.where(h >= 0.5, 1, 0) def load_data(filepath): - """ - 从文件中加载数据,若不自行正确实现,则无法通过评测,**不记录成绩**。 - - Args: - - filepath (str): 数据文件路径。 - - Returns: - - np.array: 特征数据。 - - np.array: 标签数据。 - """ data = [] labels = [] with open(filepath, 'r') as f: @@ -124,6 +79,7 @@ def accuracy(X, y, weights): return np.mean(predictions == y) if __name__ == "__main__": + # 读取数据 X_train, y_train = load_data("./horse-colic.data") X_test, y_test = load_data("./horse-colic.test") @@ -132,7 +88,9 @@ if __name__ == "__main__": X_train = scaler.fit_transform(X_train) X_test = scaler.transform(X_test) + # 训练模型 final_weights = train_logreg(X_train, y_train, learning_rate=0.001, iterations=5000, batch_size=64) + # 评估模型 final_accuracy = accuracy(X_test, y_test, final_weights) print(f"最终测试集的准确率: {final_accuracy:.4f}")