From e40aaf15a4f094dd26314fc3ffa6e4252dbf618d Mon Sep 17 00:00:00 2001 From: 202210423105 <3374617300@qq.com> Date: Sun, 3 Nov 2024 16:28:24 +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 | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/function.py b/function.py index fd0c434..fe3c781 100644 --- a/function.py +++ b/function.py @@ -83,17 +83,15 @@ def load_data(filepath): - np.array: 特征数据。 - np.array: 标签数据。 """ + with open(filepath, 'r') as f: + lines = f.readlines() + X = [] y = [] - with open(filepath, 'r') as f: - for line in f: - values = line.strip().split() - # 检查是否存在 '?' 并跳过这些行 - if '?' in values: - continue - # 将标签和特征转换为浮点数 - y.append(int(values[-1])) - X.append([float(x) for x in values[:-1]]) + for line in lines: + values = line.strip().split() + y.append(int(values[-1])) + X.append([float(x) for x in values[:-1]]) return np.array(X), np.array(y)