chore: better loading of data
Gitea Actions Test / Exp-Actions (push) Successful in 8s

chore: add default value for k
This commit is contained in:
2024-09-27 23:50:12 +08:00
parent 4738466fa7
commit b00c13b550
+6 -11
View File
@@ -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__":