From 4304c8e1ef84e5a65f841b128e8adbecf44e9886 Mon Sep 17 00:00:00 2001 From: dong <2938690875@qq.com> Date: Thu, 14 Nov 2024 16:59:55 +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 | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/function.py b/function.py index f7152f3..3afa3b0 100644 --- a/function.py +++ b/function.py @@ -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:]] - data.append(features) - + 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):