From 6cbd3c3886aaaac41fc25f92c96cd2a549aecd5d Mon Sep 17 00:00:00 2001 From: heart20003 <1789588961@qq.com> Date: Thu, 24 Oct 2024 17:17:59 +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 | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/function.py b/function.py index 031fabf..3cc5c5a 100644 --- a/function.py +++ b/function.py @@ -76,7 +76,14 @@ def load_data(filepath): - np.array: 特征数据。 - np.array: 标签数据。 """ - data = np.loadtxt(filepath, delimiter=',', dtype=float, skiprows=1) + # 使用 np.genfromtxt 来处理缺失值 + data = np.genfromtxt(filepath, delimiter=',', dtype=float, skip_header=1, + filling_values=np.nan, # 用 NaN 填充缺失值 + missing_values='?') # 将问号识别为缺失值 + + # 处理缺失值(例如将 NaN 替换为 0 或者其他值) + data = np.nan_to_num(data, nan=0.0) # 将 NaN 替换为 0.0 + X = data[:, :-1] # 所有列,除了最后一列 y = data[:, -1] # 最后一列作为标签 return X, y