From 25209d4ae2d9e0b12cb1870755a512f53351db48 Mon Sep 17 00:00:00 2001 From: Frae19 <515745323@qq.com> Date: Tue, 5 Nov 2024 14:55:51 +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 | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/function.py b/function.py index eb58a61..435757a 100644 --- a/function.py +++ b/function.py @@ -27,6 +27,8 @@ id = "202210423629" name = "Tanjunfeng" # 这里写自己的代码实现 +import numpy as np + def sigmoid(z): return 1 / (1 + np.exp(-z)) @@ -83,9 +85,20 @@ def load_data(filepath): - np.array: 特征数据。 - np.array: 标签数据。 """ - data = np.loadtxt(filepath, delimiter=',') - X = data[:, :-1] - y = data[:, -1] + data = [] + with open(filepath, 'r') as f: + for line in f: + row = [] + values = line.strip().split() + for value in values: + try: + row.append(float(value)) + except ValueError: + row.append(-1) + data.append(row) + np_data = np.array(data) + X = np_data[:, :-1] + y = np_data[:, -1] return X, y if __name__ == "__main__": @@ -101,5 +114,4 @@ if __name__ == "__main__": # 测试单个数据 y_pred = predict_logreg(X_test[11], weights) - print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}") - + print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}") \ No newline at end of file