更新 function.py
Gitea Actions Experiment Test / Experiment-Actions (push) Successful in 10s

This commit is contained in:
2024-11-03 14:55:45 +08:00
parent ed3f3ce54b
commit b3b4564bd3
+12 -8
View File
@@ -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__":