From 095bfc00025de3053c2bab1d9dd7dec149adb6a4 Mon Sep 17 00:00:00 2001 From: Yijuehen <1906265724@qq.com> Date: Wed, 13 Nov 2024 18:24:06 +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 | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/function.py b/function.py index a2c2588..0fdf689 100644 --- a/function.py +++ b/function.py @@ -5,29 +5,35 @@ id = "202210423230" name = "尹家立" def load_data(filepath): - """ - 从文件中加载数据,并处理缺失值。 + data, labels = [], [] + with open(filepath, 'r') as f: + for line in f: + line = line.strip().split() + if len(line) >= 1 and line[-1].isdigit(): + line_data = [0 if x == '?' else float(x) for x in line[:-1]] + data.append(line_data) + labels.append(int(line[-1])) - Args: - - filepath (str): 数据文件路径。 + data = np.array(data) + labels = np.array(labels) - Returns: - - np.array: 特征数据。 - - np.array: 标签数据。 - """ - # 读取数据,替换缺失值 '?' 为 NaN - data = np.genfromtxt(filepath, delimiter=' ', dtype=float, missing_values='?', filling_values=np.nan) + # 检查特征数量 + num_features = data.shape[1] + + # 特征选择:去掉特征3(索引2)和特征24~28(索引23~26),确保索引有效 + indices_to_delete = [2] # 特征3 + if num_features > 24: # 确保特征数量足够 + indices_to_delete += list(range(24, 27)) # 特征24到27(索引23到26) + + # 删除特征 + data = np.delete(data, indices_to_delete, axis=1) + + # 将标签二分类化,特征23(索引22)取值2和3合并为0,其余为1 + target_variable = np.where((labels == 2) | (labels == 3), 0, 1) + + return data, target_variable - # 将 NaN 替换为列均值 - nan_indices = np.isnan(data) - col_means = np.nanmean(data, axis=0) - data[nan_indices] = np.take(col_means, np.where(nan_indices)[1]) - # 将最后一列作为标签 - X = data[:, :-1] - y = data[:, -1] - - return X, y