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

This commit is contained in:
2024-11-07 16:48:50 +08:00
parent 820027e894
commit 8b9ff6e020
+6 -22
View File
@@ -58,7 +58,7 @@ def predict_logreg(inX, weights):
def load_data(filepath):
"""
从文件中加载数据,处理缺失数据和确保样本数匹配。
从文件中加载数据。
Args:
- filepath (str): 数据文件路径。
@@ -69,36 +69,20 @@ def load_data(filepath):
"""
data = []
labels = []
with open(filepath, 'r') as file:
for line in file:
line = line.strip().split()
if not line:
continue
# 过滤掉包含 '?' 的行
if '?' in line:
continue
# 确保每行都有正确数量的特征和标签
if len(line) != 9: # 假设每行有8个特征和1个标签
continue
# 将特征转换为浮点数
try:
features = list(map(float, line[:-1])) # 所有特征转换为浮点数
label = int(line[-1]) # 标签为整数
except ValueError:
# 如果遇到无法转换的值,则跳过这一行
continue
# 添加到数据集
# 将特征转换为浮点型
features = list(map(float, line[:-1]))
label = int(line[-1])
data.append(features)
labels.append(label)
return np.array(data), np.array(labels)
if __name__ == "__main__":
# 加载训练集
X_train, y_train = load_data("./data/horse-colic.data")