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

This commit is contained in:
2024-11-21 09:24:30 +08:00
parent 32b108630f
commit 35345eea6b
+45 -22
View File
@@ -44,38 +44,61 @@ name = "李燕如"
# 这里写自己的代码实现
import numpy as np
def classify0(inX, dataSet, labels, k=3):
# 数据标准化
dataSet = (dataSet - np.mean(dataSet, axis=0)) / np.std(dataSet, axis=0)
inX = (inX - np.mean(dataSet, axis=0)) / np.std(dataSet, axis=0)
def classify0(inX, dataSet, labels, k=5):
"""
KNN 分类算法。
Args:
- inX (numpy array): 输入待分类的样本。
- dataSet (numpy array): 训练数据集。
- labels (list or numpy array): 训练集对应的标签。
- k (int): kNN 算法参数,默认值为 5。
Returns:
- int: 预测的分类标签。
"""
# 计算待分类样本与训练集中每个样本的欧氏距离
diffMat = np.tile(inX, (dataSet.shape[0], 1)) - dataSet
sqDiffMat = diffMat**2
sqDistances = sqDiffMat.sum(axis=1)
distances = sqDistances**0.5
# 计算距离
distances = np.sqrt(np.sum((dataSet - inX) ** 2, axis=1))
# 对距离进行排序,获取距离最近的 k 个样本的索引
sortedDistIndices = distances.argsort()
# 获取k个最近邻的索引
k_indices = np.argsort(distances)[:k]
# 提取距离最近的 k 个样本的标签
classCount = {}
for i in range(k):
voteIlabel = labels[sortedDistIndices[i]]
classCount[voteIlabel] = classCount.get(voteIlabel, 0) + 1
# 获取这些样本对应的标签
k_nearest_labels = labels[k_indices]
# 投票决定分类
result = {}
for label in k_nearest_labels:
if label in result:
result[label] += 1
else:
result[label] = 1
# 返回出现次数最多的类别
return max(result, key=result.get)
# 根据出现次数最多的标签来预测待分类样本的标签
sortedClassCount = sorted(classCount.items(), key=lambda x: x[1], reverse=True)
return sortedClassCount[0][0]
def load_data(file_path):
"""
加载 TSV 格式数据文件,并进行标准化处理。
"""
data = np.loadtxt(file_path, delimiter="\t")
features = data[:, :-1]
labels = data[:, -1].astype(int)
# 标准化特征
features = (features - np.mean(features, axis=0)) / np.std(features, axis=0)
return features, labels
if __name__ == "__main__":
train_file = "datingTrainSet2.tsv"
train_data, train_labels = load_data(train_file)
print(classify0([65915, 3.868359, 0.128078], train_data, train_labels))
test_file = "datingTestSet2.tsv"
test_data, test_labels = load_data(test_file)
# 测试 KNN 分类器
correct_count = 0
for i in range(test_data.shape[0]):
if classify0(test_data[i], train_data, train_labels) == test_labels[i]:
correct_count += 1
print(f"Accuracy: {correct_count / test_data.shape[0] * 100:.2f}%")