From 8b9ff6e020c36f5868cea11b55d7e9ee51d4e74b Mon Sep 17 00:00:00 2001 From: DREMN2112 Date: Thu, 7 Nov 2024 16:48:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20function.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- function.py | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/function.py b/function.py index 9720f59..d0c9b22 100644 --- a/function.py +++ b/function.py @@ -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")