From b3b4564bd309701e2fa26c7de6425598e382b7a4 Mon Sep 17 00:00:00 2001 From: Ulysses-ui <3233559397@qq.com> Date: Sun, 3 Nov 2024 14:55:45 +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 | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/function.py b/function.py index a6438ae..191054d 100644 --- a/function.py +++ b/function.py @@ -81,15 +81,19 @@ def load_data(filepath): - np.array: 标签数据。 """ data = [] - labels = [] - with open(filepath, 'r') as file: + with open(filepath, 'r') as file: for line in file: - fields = line.strip().split() - if '?' in fields: - continue # 跳过包含缺失值的行 - data.append([float(x) for x in fields[:-1]]) - labels.append(int(fields[-1])) # 假设标签在最后一列 - return np.array(data), np.array(labels) + line_data line.strip().split() + if line_data: + try: + data. append([float(x) if x !='?'else np. nan for x in line_data]) + except ValueError as e: + print(f"Error processing line: {line}. Error: fe}") + data = np.array(data) + data = np.nan_to_num(data,nan=np.nanmean(data,axis=0))#将缺失值用平均值代替 + X = data[:,:-1]#特征 + y=data[:,-1]#标签 + return X, y if __name__ == "__main__":