generated from LufsX/Challenge-kNN
This commit is contained in:
+24
-36
@@ -38,44 +38,32 @@ name = "潘宇豪"
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
# 这里写自己的代码实现
|
def classify0(inX, dataSet, labels, k):
|
||||||
def classify0(inX, dataSet, labels, ,k_values = [3,4,5,6,7,8,9]):
|
|
||||||
"""
|
"""
|
||||||
KNN 分类算法。
|
KNN classification using Mahalanobis distance for fast computation.
|
||||||
|
inX: The input sample to classify
|
||||||
参数:
|
dataSet: The training data
|
||||||
- inX (numpy array): 输入待分类的样本。
|
labels: The corresponding labels for the training data
|
||||||
- dataSet (numpy array): 训练数据集。
|
k: The number of nearest neighbors to consider
|
||||||
- labels (list or numpy array): 训练集对应的标签。
|
|
||||||
- k (int): 使用的邻居数量。
|
|
||||||
|
|
||||||
返回值:
|
|
||||||
- int: 预测的分类标签。
|
|
||||||
"""
|
"""
|
||||||
|
# Calculate covariance matrix and its inverse from the dataset
|
||||||
def mahalanobis_distance(inX, data, cov_inv):
|
cov_matrix = np.cov(dataSet.T)
|
||||||
diff = inX - data
|
inv_cov_matrix = inv(cov_matrix)
|
||||||
return np.sqrt(np.dot(np.dot(diff, cov_inv), diff.T))
|
|
||||||
|
# Compute the Mahalanobis distance between inX and all points in the dataset
|
||||||
mean = np.mean(dataSet, axis=0)
|
diff = dataSet - inX
|
||||||
cov_inv = inv(np.cov(dataSet, rowvar=False))
|
distances = np.sqrt(np.sum(np.dot(diff, inv_cov_matrix) * diff, axis=1))
|
||||||
|
|
||||||
# 计算所有k值的预测
|
# Sort by distance and return the indices of the k nearest neighbors
|
||||||
predictions = []
|
sorted_indices = np.argsort(distances)
|
||||||
for k in k_values:
|
|
||||||
distances = [mahalanobis_distance(inX, data, cov_inv) for data in dataSet]
|
# Get the labels of the k nearest neighbors
|
||||||
sortedDistIndices = np.argsort(distances)
|
k_nearest_labels = [labels[i] for i in sorted_indices[:k]]
|
||||||
classCount = {}
|
|
||||||
for i in range(k):
|
# Return the most common label among the k nearest neighbors
|
||||||
voteIlabel = labels[sortedDistIndices[i]]
|
most_common_label = Counter(k_nearest_labels).most_common(1)[0][0]
|
||||||
classCount[voteIlabel] = classCount.get(voteIlabel, 0) + 1
|
|
||||||
|
return most_common_label
|
||||||
sortedClassCount = sorted(classCount.items(), key=lambda item: item[1], reverse=True)
|
|
||||||
predictions.append(sortedClassCount[0][0])
|
|
||||||
|
|
||||||
# 多数投票机制
|
|
||||||
final_prediction, _ = mode(predictions)
|
|
||||||
return final_prediction.mode[0]
|
|
||||||
|
|
||||||
def load_data(file_path):
|
def load_data(file_path):
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user