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

This commit is contained in:
sn
2024-10-16 19:11:52 +08:00
parent f6e0236ed7
commit 9d6664c5d8
+44 -54
View File
@@ -41,74 +41,64 @@ name = "严锡好"
# 这里写自己的代码实现 # 这里写自己的代码实现
def mahalanobis_distance(x1, x2, cov_inv, mean): class KDNode:
""" def __init__(self, data, left=None, right=None, label=None):
计算两点之间的马氏距离。 self.data = data
self.left = left
self.right = right
self.label = label
Args: class KDTree:
x1 (numpy array): 第一个点。 def __init__(self, data, labels, k=3):
x2 (numpy array): 第二个点。 self.k = k
cov_inv (numpy array): 协方差矩阵的逆矩阵。 self.root = self.build_tree(data, labels)
mean (numpy array): 数据集的均值。
Returns: def build_tree(self, data, labels, depth=0):
float: 两点之间的马氏距离。 if not data:
""" return None
delta = x1 - x2 indices = np.argsort(data[:, depth % data.shape[1]])
dist = delta @ cov_inv @ delta.T median_idx = indices[len(indices) // 2]
return np.sqrt(dist) node = KDNode(data[median_idx], label=labels[median_idx])
node.left = self.build_tree(data[indices[:median_idx]], labels[indices[:median_idx]], depth + 1)
node.right = self.build_tree(data[indices[median_idx + 1:]], labels[indices[median_idx + 1:]], depth + 1)
return node
def classify0(inX, dataSet, labels, k=3): def find_nearest_neighbors(self, point, node=None, depth=0, neighbors=None):
""" if not node:
使用马氏距离的 KNN 分类算法。 return
dist = np.linalg.norm(point - node.data)
Args: if neighbors is None:
- inX (numpy array): 输入待分类的样本。 neighbors = [(dist, node.label)]
- dataSet (numpy array): 训练数据集。 else:
- labels (list or numpy array): 训练集对应的标签。 neighbors = [(n[0], n[1]) for n in neighbors if n[0] < dist] + [(dist, node.label)]
- k (int): kNN 算法参数,默认值为 3。 neighbors.sort(key=lambda x: x[0])
if len(neighbors) < self.k:
Returns: self.find_nearest_neighbors(point, node.left, depth + 1, neighbors)
- int: 预测的分类标签。 self.find_nearest_neighbors(point, node.right, depth + 1, neighbors)
""" else:
# 计算训练数据集的协方差矩阵和均值 if depth % point.shape[0] != point.shape[0] - 1:
cov_matrix = np.cov(dataSet, rowvar=False) self.find_nearest_neighbors(point, node.left, depth + 1, neighbors)
mean = np.mean(dataSet, axis=0) if depth % point.shape[0] != point.shape[0] - 1:
cov_inv = np.linalg.inv(cov_matrix + np.eye(cov_matrix.shape[0]) * 1e-5) # 加上一个小的单位矩阵以避免奇异矩阵 self.find_nearest_neighbors(point, node.right, depth + 1, neighbors)
# 计算待分类样本与训练集中每个样本的马氏距离
distances = np.array([mahalanobis_distance(inX, x, cov_inv, mean) for x in dataSet])
# 获取距离最近的 k 个样本的索引
sortedDistIndices = distances.argsort()[:k]
# 获取这些样本对应的标签
classCount = {}
for i in sortedDistIndices:
voteLabel = labels[i]
classCount[voteLabel] = classCount.get(voteLabel, 0) + 1
# 选择出现次数最多的标签作为预测标签
sortedClassCount = sorted(classCount.items(), key=lambda x: x[1], reverse=True)
return sortedClassCount[0][0]
def predict(self, point):
neighbors = []
self.find_nearest_neighbors(point, self.root, 0, neighbors)
neighbors.sort(key=lambda x: x[0])
labels = [n[1] for n in neighbors]
return np.argmax(np.bincount(labels))
def load_data(file_path): def load_data(file_path):
"""
加载 TSV 格式数据文件。
"""
data = np.loadtxt(file_path, delimiter="\t") data = np.loadtxt(file_path, delimiter="\t")
features = data[:, :-1] features = data[:, :-1]
labels = data[:, -1].astype(int) labels = data[:, -1].astype(int)
return features, labels return features, labels
if __name__ == "__main__": if __name__ == "__main__":
# 以下代码可用于测试,可自行随意修改
train_file = "datingTrainSet2.tsv" train_file = "datingTrainSet2.tsv"
train_data, train_labels = load_data(train_file) train_data, train_labels = load_data(train_file)
kdt = KDTree(train_data, train_labels, k=3)
# 测试一个样本
test_sample = np.array([65915, 3.868359, 0.128078]) test_sample = np.array([65915, 3.868359, 0.128078])
predicted_label = classify0(test_sample, train_data, train_labels) predicted_label = kdt.predict(test_sample)
print(f"The predicted label for the test sample is: {predicted_label}") print(f"The predicted label for the test sample is: {predicted_label}")