generated from LufsX/Challenge-SVM
This commit is contained in:
+19
-7
@@ -86,18 +86,30 @@ def load_data(filepath):
|
||||
- np.array: 标签数据。
|
||||
"""
|
||||
data = []
|
||||
labels = []
|
||||
|
||||
with open(filepath, 'r') as file:
|
||||
for line in file:
|
||||
# 将 "?" 替换为 "0",然后将整行数据转换为浮点数
|
||||
line = line.strip().replace("?", "0")
|
||||
data.append([float(x) for x in line.split()])
|
||||
values = line.strip().split()
|
||||
# 将缺失值用0填充,并将所有数据转换为浮点数
|
||||
features = [float(v) if v != '?' else 0.0 for v in values[:-1]]
|
||||
label = values[-1] # 假设最后一列是标签列
|
||||
|
||||
data = np.array(data)
|
||||
X = data[:, :-1] # 所有列,除最后一列
|
||||
y = data[:, -1] # 最后一列
|
||||
y = np.where(y == 1, 1, -1) # 将标签转换为 +1 和 -1
|
||||
# 根据标签值进行三分类的标签转换
|
||||
if label == '1':
|
||||
labels.append(0) # 存活
|
||||
elif label == '2':
|
||||
labels.append(1) # 死亡
|
||||
elif label == '3':
|
||||
labels.append(2) # 安乐死
|
||||
|
||||
data.append(features)
|
||||
|
||||
X = np.array(data)
|
||||
y = np.array(labels)
|
||||
return X, y
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 以下代码可用于测试,可自行随意修改
|
||||
# 加载训练集
|
||||
|
||||
Reference in New Issue
Block a user