generated from LufsX/Challenge-SVM
This commit is contained in:
+6
-13
@@ -33,20 +33,13 @@ def load_data(filepath):
|
||||
labels = []
|
||||
with open(filepath, 'r') as file:
|
||||
for line in file:
|
||||
line = line.strip().split()
|
||||
if len(line) > 1:
|
||||
label = line[0]
|
||||
if label == "死亡":
|
||||
label = 0
|
||||
elif label == "安乐死":
|
||||
label = 1
|
||||
elif label == "存活":
|
||||
label = 2
|
||||
labels.append(label)
|
||||
|
||||
features = [float(x) if x != '?' else 0.0 for x in line[1:]]
|
||||
fields = line.strip().split()
|
||||
if '?' in fields: # 处理缺失值
|
||||
fields = [0 if f == '?' else float(f) for f in fields]
|
||||
features = np.array(fields[:-1], dtype=float)
|
||||
label = int(fields[-1]) # 确保标签未合并
|
||||
data.append(features)
|
||||
|
||||
labels.append(label)
|
||||
return np.array(data), np.array(labels)
|
||||
|
||||
def train_svm(X, y, learning_rate=0.01, iterations=1000):
|
||||
|
||||
Reference in New Issue
Block a user