generated from LufsX/Experiment-kNN
This commit is contained in:
+21
-39
@@ -41,58 +41,38 @@ name = "万子玉"
|
||||
"""
|
||||
|
||||
# 这里写自己的代码实现
|
||||
def mahalanobis_distance(x, mean, inv_covmat):
|
||||
delta = x - mean
|
||||
m_dist = np.sqrt(np.dot(np.dot(delta, inv_covmat), delta.T))
|
||||
return m_dist
|
||||
def euclidean_distance(a, b):
|
||||
return np.sqrt(np.sum((a - b) ** 2))
|
||||
|
||||
def classify(input_vector, dataSet, labels, k=2, reg=1e-5):
|
||||
# 确保dataSet是二维的,每个样本是一维的
|
||||
if dataSet.ndim > 2:
|
||||
dataSet = dataSet.reshape(dataSet.shape[0], -1)
|
||||
|
||||
mean = np.mean(dataSet, axis=0)
|
||||
covmat = np.cov(dataSet, rowvar=0)
|
||||
|
||||
# 正则化协方差矩阵以避免奇异矩阵
|
||||
covmat += np.eye(covmat.shape[0]) * reg
|
||||
|
||||
inv_covmat = np.linalg.inv(covmat)
|
||||
|
||||
# 马氏距离计算
|
||||
dists = [mahalanobis_distance(input_vector, mean, inv_covmat)]
|
||||
for data in dataSet:
|
||||
dists.append(mahalanobis_distance(data, mean, inv_covmat))
|
||||
|
||||
# 移除第一个距离,因为它是输入向量的马氏距离
|
||||
dists.pop(0)
|
||||
|
||||
|
||||
def classify(input_vector, dataSet, labels, k=2):
|
||||
dists = [euclidean_distance(input_vector, data) for data in dataSet]
|
||||
k_indices = np.argsort(dists)[:k]
|
||||
k_nearest_labels = [labels[i] for i in k_indices]
|
||||
k_nearest_dists = [dists[i] for i in k_indices]
|
||||
|
||||
if not k_nearest_labels:
|
||||
raise ValueError("No neighbors found; possible causes: k is too small or all distances are the same.")
|
||||
|
||||
most_common = Counter(k_nearest_labels).most_common(1)
|
||||
# 计算权重,距离越小权重越大
|
||||
weights = [1/dist if dist != 0 else 0 for dist in k_nearest_dists]
|
||||
total_weights = sum(weights)
|
||||
weighted_votes = Counter()
|
||||
|
||||
for label, weight in zip(k_nearest_labels, weights):
|
||||
weighted_votes[label] += weight
|
||||
|
||||
if most_common:
|
||||
return most_common[0][0]
|
||||
# 根据权重排序
|
||||
sorted_weighted_votes = sorted(weighted_votes.items(), key=lambda x: x[1], reverse=True)
|
||||
|
||||
if sorted_weighted_votes:
|
||||
return sorted_weighted_votes[0][0]
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
# 这里也写自己的代码实现
|
||||
def load_dataset(directory):
|
||||
"""
|
||||
从文件夹中加载数据,若不自行正确实现,则无法通过评测,**不记录成绩**。
|
||||
|
||||
Args:
|
||||
- directory (str): 数据文件夹路径。
|
||||
|
||||
Returns:
|
||||
- np.array: 特征数据。
|
||||
- np.array: 标签数据。
|
||||
"""
|
||||
data_list = []
|
||||
label_list = []
|
||||
for root, dirs, files in os.walk(directory):
|
||||
@@ -104,6 +84,7 @@ def load_dataset(directory):
|
||||
return np.array(data_list), np.array(label_list)
|
||||
|
||||
|
||||
|
||||
# 这里还写自己的代码实现
|
||||
def img_to_vector(filename):
|
||||
"""
|
||||
@@ -121,6 +102,7 @@ def img_to_vector(filename):
|
||||
vector = np.array([int(char) for line in lines for char in line.strip() if char.isdigit()])
|
||||
return vector.reshape(1, -1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 以下代码可用于测试,可自行随意修改
|
||||
train_data, train_labels = load_dataset("trainingDigits")
|
||||
@@ -159,7 +141,7 @@ if __name__ == "__main__":
|
||||
"00000000000001111111110000000000"
|
||||
"00000000000000000111100000000000"
|
||||
)
|
||||
predicted_label = classify(
|
||||
predicted_label = classify(
|
||||
np.array([int(char) for char in test_data]).reshape(1, 1024),
|
||||
train_data,
|
||||
train_labels,
|
||||
|
||||
Reference in New Issue
Block a user