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

This commit is contained in:
2024-11-07 16:43:36 +08:00
parent f55017e9d5
commit 6d574b6aa8
+13 -10
View File
@@ -58,7 +58,7 @@ def predict_logreg(inX, weights):
def load_data(filepath): def load_data(filepath):
""" """
从文件中加载数据,处理缺失数据。 从文件中加载数据,处理缺失数据和确保样本数匹配。
Args: Args:
- filepath (str): 数据文件路径。 - filepath (str): 数据文件路径。
@@ -76,22 +76,25 @@ def load_data(filepath):
if not line: if not line:
continue continue
# 检查并替换缺失值 '?' 为 np.nan # 过滤掉包含 '?' 的行
line = [float(x) if x != '?' else np.nan for x in line[:-1]] if '?' in line:
# 如果数据中包含 np.nan,则跳过该行
if np.any(np.isnan(line)):
continue continue
# 获取标签并添加到标签列表 # 确保每行都有正确数量的特征和标签
if len(line) != 9: # 假设每行有8个特征和1个标签
continue
# 将特征转换为浮点数
features = list(map(float, line[:-1]))
label = int(line[-1]) label = int(line[-1])
# 组合特征和标签 # 添加到数据集
data.append(line) data.append(features)
labels.append(label) labels.append(label)
return np.array(data), np.array(labels) return np.array(data), np.array(labels)
if __name__ == "__main__": if __name__ == "__main__":
# 加载训练集 # 加载训练集
X_train, y_train = load_data("./data/horse-colic.data") X_train, y_train = load_data("./data/horse-colic.data")