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

This commit is contained in:
2024-11-14 16:59:55 +08:00
parent 1b033a9e86
commit 4304c8e1ef
+7 -14
View File
@@ -33,20 +33,13 @@ def load_data(filepath):
labels = [] labels = []
with open(filepath, 'r') as file: with open(filepath, 'r') as file:
for line in file: for line in file:
line = line.strip().split() fields = line.strip().split()
if len(line) > 1: if '?' in fields: # 处理缺失值
label = line[0] fields = [0 if f == '?' else float(f) for f in fields]
if label == "死亡": features = np.array(fields[:-1], dtype=float)
label = 0 label = int(fields[-1]) # 确保标签未合并
elif label == "安乐死": data.append(features)
label = 1 labels.append(label)
elif label == "存活":
label = 2
labels.append(label)
features = [float(x) if x != '?' else 0.0 for x in line[1:]]
data.append(features)
return np.array(data), np.array(labels) return np.array(data), np.array(labels)
def train_svm(X, y, learning_rate=0.01, iterations=1000): def train_svm(X, y, learning_rate=0.01, iterations=1000):