更新 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
+14 -11
View File
@@ -85,16 +85,20 @@ def load_data(filepath):
data = [] data = []
labels = [] labels = []
with open(filepath, 'r') as file: with open(filepath, 'r') as file:
lines = file.readlines() lines = file.readlines()
for line in lines: for line in lines:
line = line.strip().split() try:
if '?' not in line: # 只处理不包含缺失值的行 line = line.strip().split()
features = [float(x) for x in line[:-1]] if '?' not in line: # 只处理不包含缺失值的行
label = float(line[-1]) features = [float(x) for x in line[:-1]]
if label == 0: label = float(line[-1])
label = -1 # 将类别0转换为 -1 ,符合SVM二分类的约定 if label == 0:
data.append(features) label = -1 # 将类别0转换为 -1 ,符合SVM二分类的约定
labels.append(label) data.append(features)
labels.append(label)
except ValueError:
print(f"数据解析出现问题,跳过该行数据: {line}")
continue
return np.array(data), np.array(labels) return np.array(data), np.array(labels)
if __name__ == "__main__": if __name__ == "__main__":
@@ -112,4 +116,3 @@ if __name__ == "__main__":
for i in range(len(X_test)): for i in range(len(X_test)):
y_pred = predict_svm(X_test[i], weights) y_pred = predict_svm(X_test[i], weights)
print(f"预测结果: {y_pred}, 实际结果: {y_test[i]}") print(f"预测结果: {y_pred}, 实际结果: {y_test[i]}")
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")