From ce7f269fcc758de9160c87ff3f6ca859167d438d Mon Sep 17 00:00:00 2001 From: zhangchenghong <1598349064@qq.com> Date: Sat, 26 Oct 2024 12:32:33 +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, 11 insertions(+), 3 deletions(-) diff --git a/function.py b/function.py index 0c39134..57734ac 100644 --- a/function.py +++ b/function.py @@ -26,8 +26,11 @@ name = "章程鸿" # 这里写自己的代码实现 +import numpy as np + def sigmoid(z): return 1 / (1 + np.exp(-z)) + def train_logreg(X, y, learning_rate=0.01, iterations=1000): """ 对率回归训练函数。 @@ -79,11 +82,16 @@ def load_data(filepath): - np.array: 特征数据。 - np.array: 标签数据。 """ - data = np.loadtxt(filepath, delimiter=',') + data = [] + with open(filepath, 'r') as file: + for line in file: + # 清理无效数据 + cleaned_line = line.strip().replace('?', '0') # 将问号替换为0 + data.append([float(x) for x in cleaned_line.split()]) # 转换为浮点数 + data = np.array(data) X = data[:, :-1] y = data[:, -1] - return X, y - + return X, y if __name__ == "__main__": # 以下代码可用于测试,可自行随意修改 # 加载训练集