From 8346632a7be60930861fe523c3a195318417cf2f Mon Sep 17 00:00:00 2001 From: luderby <2339127037@qq.com> Date: Tue, 5 Nov 2024 11:16:27 +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 | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/function.py b/function.py index d5e7d92..be15b75 100644 --- a/function.py +++ b/function.py @@ -82,21 +82,25 @@ def load_data(filepath): - np.array: 特征数据。 - np.array: 标签数据。 """ - # 使用 numpy.genfromtxt 加载数据,指定缺失值 + # 加载数据,使用适当的分隔符和设置 data = np.genfromtxt(filepath, delimiter=',', dtype=None, encoding=None, invalid_raise=False, filling_values=np.nan, autostrip=True) - # 将数据拆分为特征和标签 - X = data[:, :-1].astype(float) # 假设最后一列为标签,并转换为浮点数 + print("Loaded data shape:", data.shape) # 打印数据的形状 + + # 检查是否成功加载了二维数据 + if data.ndim == 1: + raise ValueError("Loaded data is one-dimensional, please check the file format.") + + X = data[:, :-1].astype(float) # 假设最后一列为标签 y = data[:, -1] # 最后一列为标签 - # 删除任何缺失值行(NaN) + # 检查有效索引 valid_indices = ~np.isnan(X).any(axis=1) & ~np.isnan(y) X = X[valid_indices] y = y[valid_indices] - # 处理 y,确保 y 是二进制的,1表示死亡,0表示存活 - y = np.where(y == 1, 1, 0) # 假设标签中1和其他值需要统一成0或1 - return X, y + y = np.where(y == 1, 1, 0) + return X, y if __name__ == "__main__":