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

This commit is contained in:
77
2024-12-27 14:31:53 +08:00
parent 4776389760
commit effdc10347
+8 -4
View File
@@ -7,15 +7,20 @@ def load_data(filepath):
try:
# 使用numpy的genfromtxt函数加载数据,并处理缺失值
data = np.genfromtxt(filepath, delimiter=',', dtype=float, filling_values=np.nan)
except ValueError:
print("数据加载错误,可能是由于数据格式不正确。")
except Exception as e:
print(f"数据加载错误,可能是由于数据格式不正确:{e}")
return None, None
# 检查数据是否为二维数组
if data.ndim != 2:
if data is None or data.ndim != 2:
print("数据不是二维数组,无法继续处理。")
return None, None
# 确保所有行都有相同数量的列
if data.shape[1] < 2:
print("数据列数不正确,无法继续处理。")
return None, None
# 分离特征和标签
X = data[:, :-1]
y = data[:, -1].astype(int)
@@ -57,4 +62,3 @@ if __name__ == "__main__":
# 测试单个数据
y_pred = predict_svm(X_test[11], weights, bias)
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")