From 153d68f1cee816926170773a8e7c6eb0d42fbfc1 Mon Sep 17 00:00:00 2001 From: 77 <3298722099@qq.com> Date: Fri, 27 Dec 2024 14:24:45 +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 | 85 +++++++++++++++++------------------------------------ 1 file changed, 27 insertions(+), 58 deletions(-) diff --git a/function.py b/function.py index 95bc88e..54a7095 100644 --- a/function.py +++ b/function.py @@ -3,63 +3,31 @@ import numpy as np id = "202210423201" name = "陈瑞琪祥" -def train_svm(X, y, learning_rate=0.01, iterations=1000): - """ - 支持向量机训练函数。 - - Args: - - X (numpy array): 特征数据。 - - y (numpy array): 标签数据。 - - learning_rate (float): 学习率。 - - iterations (int): 迭代次数。 - - Returns: - - numpy array: 训练得到的权重。 - """ - # 初始化权重 - weights = np.zeros(X.shape[1] + 1) # 加1是为了偏置项 - # 简单的梯度下降优化 - for _ in range(iterations): - for i in range(len(X)): - if y[i] * (np.dot(X[i], weights[:-1]) + weights[-1]) < 1: - weights[:-1] += learning_rate * (y[i] * X[i]) # 更新权重 - weights[-1] += learning_rate * y[i] # 更新偏置项 - else: - weights[:-1] -= learning_rate * (y[i] * X[i]) # 更新权重 - weights[-1] -= learning_rate * y[i] # 更新偏置项 - - return weights - -def predict_svm(inX, weights): - """ - 支持向量机预测函数。 - - Args: - - inX (numpy array): 输入待分类的样本。 - - weights (numpy array): 训练得到的权重。 - - Returns: - - int: 预测的分类标签。 - """ - # 预测标签 - prediction = np.sign(np.dot(inX, weights[:-1]) + weights[-1]) # 加上偏置项 - return 1 if prediction > 0 else -1 - def load_data(filepath): - """ - 从文件中加载数据。 - - Args: - - filepath (str): 数据文件路径。 - - Returns: - - np.array: 特征数据。 - - np.array: 标签数据。 - """ - data = np.loadtxt(filepath, dtype=np.float64) - X = data[:, :-1] - y = data[:, -1] - return X, y + data = np.loadtxt(filepath, delimiter=',') + return data[:, :-1], data[:, -1].astype(int) + +def train_svm(X, y, learning_rate=0.01, iterations=1000, regularization=1.0): + n_samples, n_features = X.shape + weights = np.zeros(n_features) + bias = 0 + + for _ in range(iterations): + for idx, x_i in enumerate(X): + condition = y[idx] * (np.dot(x_i, weights) - bias) >= 1 + if condition: + weights -= learning_rate * (2 * regularization * weights) + else: + weights -= learning_rate * (2 * regularization * weights - np.dot(x_i, y[idx])) + bias -= learning_rate * y[idx] + + # Update bias + bias -= learning_rate * np.sum(y * (1 - y * (np.dot(X, weights) - bias))) + + return weights, bias + +def predict_svm(inX, weights, bias): + return np.sign(np.dot(inX, weights) - bias) if __name__ == "__main__": # 加载训练集 @@ -69,8 +37,9 @@ if __name__ == "__main__": X_test, y_test = load_data("./data/horse-colic.test") # 训练支持向量机模型 - weights = train_svm(X_train, y_train, learning_rate=0.01, iterations=1000) + weights, bias = train_svm(X_train, y_train, learning_rate=0.01, iterations=1000) # 测试单个数据 - y_pred = predict_svm(X_test[11], weights) + y_pred = predict_svm(X_test[11], weights, bias) print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}") +