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

This commit is contained in:
2024-11-14 17:04:27 +08:00
parent a4bb608bd4
commit 2bfe92a76f
+15 -12
View File
@@ -85,16 +85,20 @@ def load_data(filepath):
data = []
labels = []
with open(filepath, 'r') as file:
lines = file.readlines()
for line in lines:
line = line.strip().split()
if '?' not in line: # 只处理不包含缺失值的行
features = [float(x) for x in line[:-1]]
label = float(line[-1])
if label == 0:
label = -1 # 将类别0转换为 -1 ,符合SVM二分类的约定
data.append(features)
labels.append(label)
lines = file.readlines()
for line in lines:
try:
line = line.strip().split()
if '?' not in line: # 只处理不包含缺失值的行
features = [float(x) for x in line[:-1]]
label = float(line[-1])
if label == 0:
label = -1 # 将类别0转换为 -1 ,符合SVM二分类的约定
data.append(features)
labels.append(label)
except ValueError:
print(f"数据解析出现问题,跳过该行数据: {line}")
continue
return np.array(data), np.array(labels)
if __name__ == "__main__":
@@ -111,5 +115,4 @@ if __name__ == "__main__":
for i in range(len(X_test)):
y_pred = predict_svm(X_test[i], weights)
print(f"预测结果: {y_pred}, 实际结果: {y_test[i]}")
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")
print(f"预测结果: {y_pred}, 实际结果: {y_test[i]}")