From dd50836d0699e10865d4e958a8cb4798b1c3426c Mon Sep 17 00:00:00 2001 From: luderby <2339127037@qq.com> Date: Wed, 13 Nov 2024 19:25:12 +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 | 99 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 57 insertions(+), 42 deletions(-) diff --git a/function.py b/function.py index 92c3b6f..f165935 100644 --- a/function.py +++ b/function.py @@ -25,59 +25,74 @@ def sigmoid(z): """ id = "202210423223" name = "李顺潮" -# 对率回归训练函数 -def train_logreg(X, y, learning_rate=0.01, iterations=1000): - X = np.hstack((np.ones((X.shape[0], 1)), X)) # 在特征前添加一列全为1的列 - m, n = X.shape - weights = np.zeros(n) - for _ in range(iterations): - z = np.dot(X, weights) - predictions = 1 / (1 + np.exp(-z)) - gradient = np.dot(X.T, (predictions - y)) / m - weights -= learning_rate * gradient +# 对率回归训练函数 +def train_logreg(X, y, learning_rate=0.01, iterations=1000, batch_size=32, init_weights=None): + X = np.hstack((np.ones((X.shape[0], 1)), X)) # 在特征前添加一列全为1的列 + m, n = X.shape + + # 初始化权重 + if init_weights is None: + weights = np.random.randn(n) * 0.01 # 使用标准差为0.01的正态分布初始化权重 + else: + weights = init_weights - return weights + for _ in range(iterations): + indices = np.random.permutation(m) # 打乱数据 + X_shuffled = X[indices] + y_shuffled = y[indices] -# 对率回归预测函数 -# 对率回归预测函数,支持多类分类 -def predict_logreg(inX, weights): - inX = np.insert(inX, 0, 1) # 在特征前添加1作为偏置项 - prob = 1 / (1 + np.exp(-np.dot(inX, weights))) + for i in range(0, m, batch_size): # 分批处理 + X_batch = X_shuffled[i:i + batch_size] + y_batch = y_shuffled[i:i + batch_size] + + predictions = sigmoid(np.dot(X_batch, weights)) + gradient = np.dot(X_batch.T, (predictions - y_batch)) / len(y_batch) + weights -= learning_rate * gradient - if prob < 0.5: - return 0 - elif prob >= 0.5 and prob < 1.0: - return 1 - else: - return 2 # 确保返回3个类别中的一个 + return weights + +# 逻辑回归预测函数 +def predict_logreg(inX, weights): + inX = np.insert(inX, 0, 1) # 在特征前添加1作为偏置项 + prob = sigmoid(np.dot(inX, weights)) + + return 1 if prob >= 0.5 else 0 # 二分类处理 # 从文件中加载数据 -def load_data(filepath): - data = pd.read_csv(filepath, header=None, na_values='?', delim_whitespace=True) - data.fillna(0, inplace=True) - X = data.iloc[:, :-1].to_numpy() # 特征 - y = data.iloc[:, -1].to_numpy() # 标签 - return X, y -# 准确率计算函数 -def calculate_accuracy(X, y, weights): - predictions = np.array([predict_logreg(x, weights) for x in X]) - accuracy = np.mean(predictions == y) - return accuracy +def load_data(filepath): + data = pd.read_csv(filepath, header=None, na_values='?', delim_whitespace=True) + data.fillna(0, inplace=True) + X = data.iloc[:, :-1].to_numpy() # 特征 + y = data.iloc[:, -1].to_numpy() # 标签 + return X, y + +# 准确率计算函数 +def calculate_accuracy(X, y, weights): + predictions = np.array([predict_logreg(x, weights) for x in X]) + accuracy = np.mean(predictions == y) + return accuracy if __name__ == "__main__": # 加载训练集和测试集 - X_train, y_train = load_data("E:\\Edge站内文件下载\\病马数据集\\horse-colic.data") - X_test, y_test = load_data( "E:\\Edge站内文件下载\\病马数据集\\horse-colic.test") + X_train, y_train = load_data("E:\\Edge站内文件下载\\病马数据集\\horse-colic.data") + X_test, y_test = load_data("E:\\Edge站内文件下载\\病马数据集\\horse-colic.test") - # 训练对率回归模型 - weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000) - + # 数据标准化 + scaler = StandardScaler() + X_train = scaler.fit_transform(X_train) + X_test = scaler.transform(X_test) + + # 训练对率回归模型 + learning_rate = 0.01 + iterations = 1000 + batch_size = 32 # 随机梯度下降的小批量大小 + weights = train_logreg(X_train, y_train, learning_rate=learning_rate, iterations=iterations, batch_size=batch_size) + # 测试单个数据 y_pred = predict_logreg(X_test[11], weights) print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}") - - # 计算并输出模型的准确率 - accuracy = calculate_accuracy(X_test, y_test, weights) - print(f"模型的准确率: {accuracy:.4f}") \ No newline at end of file + # 计算并输出模型的准确率 + accuracy = calculate_accuracy(X_test, y_test, weights) + print(f"模型的准确率: {accuracy:.4f}") \ No newline at end of file