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__":