From b00c13b55094f14aec705a4e22e774260137939e Mon Sep 17 00:00:00 2001 From: LufsX Date: Fri, 27 Sep 2024 23:50:12 +0800 Subject: [PATCH] chore: better loading of data chore: add default value for k --- function.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/function.py b/function.py index 89b7dc3..2ce8f6f 100644 --- a/function.py +++ b/function.py @@ -38,7 +38,7 @@ name = "test_name" # 这里写自己的代码实现 -def classify0(inX, dataSet, labels): +def classify0(inX, dataSet, labels, k=""): """ KNN 分类算法。 @@ -46,6 +46,7 @@ def classify0(inX, dataSet, labels): - inX (numpy array): 输入待分类的样本。 - dataSet (numpy array): 训练数据集。 - labels (list or numpy array): 训练集对应的标签。 + - k (int): KNN 算法参数,请自行设置默认值,评测时仅会使用其默认值。 Returns: - int: 预测的分类标签。 @@ -56,16 +57,10 @@ def load_data(file_path): """ 加载 TSV 格式数据文件。 """ - features = [] - labels = [] - - with open(file_path, "r") as file: - for line in file: - values = line.strip().split("\t") - features.append([float(x) for x in values[:-1]]) - labels.append(int(values[-1])) - - return np.array(features), np.array(labels) + data = np.loadtxt(file_path, delimiter="\t") + features = data[:, :-1] + labels = data[:, -1].astype(int) + return features, labels if __name__ == "__main__":