generated from LufsX/Challenge-kNN
This commit is contained in:
+61
-33
@@ -40,33 +40,47 @@ name = "邓龙树"
|
|||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
class KNNClassifier:
|
def euclidean_distance(point1, point2):
|
||||||
def __init__(self, k=3):
|
"""计算两个点之间的欧氏距离。"""
|
||||||
self.k = k
|
return np.sqrt(np.sum((np.array(point1) - np.array(point2)) ** 2))
|
||||||
self.train_data = None
|
|
||||||
self.train_labels = None
|
|
||||||
|
|
||||||
def fit(self, data, labels):
|
def compute_distances(dataSet, inX):
|
||||||
self.train_data = np.array(data)
|
"""计算数据集中每个点与输入点inX的欧氏距离。"""
|
||||||
self.train_labels = np.array(labels)
|
inX_array = np.array(inX)
|
||||||
|
diff = dataSet - inX_array
|
||||||
def _compute_distances(self, inX):
|
|
||||||
inX_array = np.array(inX).reshape(1, -1)
|
|
||||||
diff = self.train_data - inX_array
|
|
||||||
squared_diff = diff ** 2
|
squared_diff = diff ** 2
|
||||||
distances = np.sqrt(np.sum(squared_diff, axis=1))
|
distances = np.sqrt(np.sum(squared_diff, axis=1))
|
||||||
return distances
|
return distances
|
||||||
|
|
||||||
def predict(self, inX):
|
def classify0(inX, dataSet, labels, k=3):
|
||||||
distances = self._compute_distances(inX)
|
"""KNN 分类算法。
|
||||||
|
|
||||||
|
Args:
|
||||||
|
- inX (list or numpy array): 输入待分类的样本。
|
||||||
|
- dataSet (numpy array): 训练数据集。
|
||||||
|
- labels (numpy array): 训练集对应的标签。
|
||||||
|
- k (int): KNN 算法参数,表示选择最近的k个邻居,默认值为3。
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
- int: 预测的分类标签。
|
||||||
|
"""
|
||||||
|
distances = compute_distances(dataSet, inX)
|
||||||
sorted_indices = np.argsort(distances)
|
sorted_indices = np.argsort(distances)
|
||||||
k_nearest_indices = sorted_indices[:self.k]
|
k_nearest_indices = sorted_indices[:k]
|
||||||
k_nearest_labels = self.train_labels[k_nearest_indices]
|
k_nearest_labels = labels[k_nearest_indices]
|
||||||
unique_labels, counts = np.unique(k_nearest_labels, return_counts=True)
|
unique_labels, counts = np.unique(k_nearest_labels, return_counts=True)
|
||||||
most_common_label = unique_labels[np.argmax(counts)]
|
most_common_label = unique_labels[np.argmax(counts)]
|
||||||
return int(most_common_label)
|
return int(most_common_label)
|
||||||
|
|
||||||
def load_data(self, file_path):
|
def load_data(file_path):
|
||||||
|
"""加载TSV格式数据文件。
|
||||||
|
|
||||||
|
Args:
|
||||||
|
- file_path (str): 数据文件的路径。
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
- tuple: 特征集和标签集。
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
data = np.loadtxt(file_path, delimiter="\t")
|
data = np.loadtxt(file_path, delimiter="\t")
|
||||||
features = data[:, :-1]
|
features = data[:, :-1]
|
||||||
@@ -79,24 +93,38 @@ class KNNClassifier:
|
|||||||
print(f"Error: The file {file_path} has an incorrect format. {e}")
|
print(f"Error: The file {file_path} has an incorrect format. {e}")
|
||||||
return None, None
|
return None, None
|
||||||
|
|
||||||
def evaluate(self, test_data, test_labels):
|
def test_knn(train_file, test_file, k=3):
|
||||||
accuracy = np.mean([self.predict(test_data[i]) == test_labels[i] for i in range(len(test_data))])
|
"""测试KNN算法。
|
||||||
print(f"Accuracy: {accuracy}")
|
|
||||||
|
Args:
|
||||||
|
- train_file (str): 训练数据文件的路径。
|
||||||
|
- test_file (str): 测试数据文件的路径。
|
||||||
|
- k (int): KNN算法参数,默认值为3。
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
- float: 测试集上的准确率。
|
||||||
|
"""
|
||||||
|
train_data, train_labels = load_data(train_file)
|
||||||
|
if train_data is None or train_labels is None:
|
||||||
|
print("Failed to load training data.")
|
||||||
|
return None
|
||||||
|
|
||||||
|
test_data, test_labels = load_data(test_file)
|
||||||
|
if test_data is None or test_labels is None:
|
||||||
|
print("Failed to load test data.")
|
||||||
|
return None
|
||||||
|
|
||||||
|
accuracy = np.mean([classify0(test_data[i], train_data, train_labels, k) == test_labels[i] for i in range(len(test_data))])
|
||||||
|
print(f"Accuracy on test set with k={k}: {accuracy}")
|
||||||
return accuracy
|
return accuracy
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
train_file_path = "path/to/datingTrainSet2.tsv"
|
train_file_path = "path/to/datingTrainSet2.tsv" # 替换为您的训练文件路径
|
||||||
test_file_path = "path/to/datingTestSet2.tsv"
|
test_file_path = "path/to/datingTestSet2.tsv" # 替换为您的测试文件路径
|
||||||
|
|
||||||
knn = KNNClassifier(k=3)
|
# 测试KNN算法,默认k=3
|
||||||
train_data, train_labels = knn.load_data(train_file_path)
|
test_knn(train_file_path, test_file_path)
|
||||||
if train_data is not None and train_labels is not None:
|
|
||||||
knn.fit(train_data, train_labels)
|
|
||||||
|
|
||||||
test_data, test_labels = knn.load_data(test_file_path)
|
# 您可以尝试不同的k值来查看对准确率的影响
|
||||||
if test_data is not None and test_labels is not None:
|
# test_knn(train_file_path, test_file_path, k=5)
|
||||||
knn.evaluate(test_data, test_labels)
|
# test_knn(train_file_path, test_file_path, k=1)
|
||||||
else:
|
|
||||||
print("Failed to load test data.")
|
|
||||||
else:
|
|
||||||
print("Failed to load training data.")
|
|
||||||
Reference in New Issue
Block a user