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

This commit is contained in:
2024-11-21 17:14:23 +08:00
parent 9fc2f107fe
commit 0d6b02d2a0
+17 -13
View File
@@ -78,20 +78,24 @@ def predict_svm(inX, weights, bias):
# 这里也写自己的代码实现
import numpy as np
import pandas as pd
def load_data(filepath):
"""
从文件中加载数据,若不自行正确实现,则无法通过评测,**不记录成绩**。
def load_data(data):
# 将数据转换为 DataFrame
df = pd.DataFrame(data)
# 清洗数据,替换 '?' 为 NaN
df.replace('?', np.nan, inplace=True)
# 尝试将数据转换为浮点数
df = df.apply(pd.to_numeric, errors='coerce')
return df
Args:
- filepath (str): 数据文件路径。
Returns:
- np.array: 特征数据。
- np.array: 标签数据。
"""
data = np.loadtxt(filepath, delimiter=',', dtype=str)
X = data[:, :-1].astype(float) # 特征数据
y = data[:, -1] # 标签数据
# 将标签转换为 -1 和 1
y = np.where(y == '1', 1, -1) # 假设标签为 '1' 表示死亡,其他为存活
return X, y
if __name__ == "__main__":