From 23f59886a50d11812edc470a22dc2743803a1ae3 Mon Sep 17 00:00:00 2001 From: Dengddd <3149942468@qq.com> Date: Tue, 8 Oct 2024 21:27:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20function.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- function.py | 136 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 82 insertions(+), 54 deletions(-) diff --git a/function.py b/function.py index a433fd5..e26a141 100644 --- a/function.py +++ b/function.py @@ -40,63 +40,91 @@ name = "邓龙树" import numpy as np -class KNNClassifier: - def __init__(self, k=3): - self.k = k - self.train_data = None - self.train_labels = None +def euclidean_distance(point1, point2): + """计算两个点之间的欧氏距离。""" + return np.sqrt(np.sum((np.array(point1) - np.array(point2)) ** 2)) - def fit(self, data, labels): - self.train_data = np.array(data) - self.train_labels = np.array(labels) +def compute_distances(dataSet, inX): + """计算数据集中每个点与输入点inX的欧氏距离。""" + inX_array = np.array(inX) + diff = dataSet - inX_array + squared_diff = diff ** 2 + distances = np.sqrt(np.sum(squared_diff, axis=1)) + return distances - def _compute_distances(self, inX): - inX_array = np.array(inX).reshape(1, -1) - diff = self.train_data - inX_array - squared_diff = diff ** 2 - distances = np.sqrt(np.sum(squared_diff, axis=1)) - return distances +def classify0(inX, dataSet, labels, k=3): + """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) + k_nearest_indices = sorted_indices[:k] + k_nearest_labels = labels[k_nearest_indices] + unique_labels, counts = np.unique(k_nearest_labels, return_counts=True) + most_common_label = unique_labels[np.argmax(counts)] + return int(most_common_label) - def predict(self, inX): - distances = self._compute_distances(inX) - sorted_indices = np.argsort(distances) - k_nearest_indices = sorted_indices[:self.k] - k_nearest_labels = self.train_labels[k_nearest_indices] - unique_labels, counts = np.unique(k_nearest_labels, return_counts=True) - most_common_label = unique_labels[np.argmax(counts)] - return int(most_common_label) +def load_data(file_path): + """加载TSV格式数据文件。 + + Args: + - file_path (str): 数据文件的路径。 + + Returns: + - tuple: 特征集和标签集。 + """ + try: + data = np.loadtxt(file_path, delimiter="\t") + features = data[:, :-1] + labels = data[:, -1].astype(int) + return features, labels + except FileNotFoundError: + print(f"Error: The file {file_path} was not found.") + return None, None + except ValueError as e: + print(f"Error: The file {file_path} has an incorrect format. {e}") + return None, None - def load_data(self, file_path): - try: - data = np.loadtxt(file_path, delimiter="\t") - features = data[:, :-1] - labels = data[:, -1].astype(int) - return features, labels - except FileNotFoundError: - print(f"Error: The file {file_path} was not found.") - return None, None - except ValueError as e: - print(f"Error: The file {file_path} has an incorrect format. {e}") - return None, None - - def evaluate(self, test_data, test_labels): - accuracy = np.mean([self.predict(test_data[i]) == test_labels[i] for i in range(len(test_data))]) - print(f"Accuracy: {accuracy}") - return accuracy +def test_knn(train_file, test_file, k=3): + """测试KNN算法。 + + 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 if __name__ == "__main__": - train_file_path = "path/to/datingTrainSet2.tsv" - test_file_path = "path/to/datingTestSet2.tsv" - - knn = KNNClassifier(k=3) - train_data, train_labels = knn.load_data(train_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) - if test_data is not None and test_labels is not None: - knn.evaluate(test_data, test_labels) - else: - print("Failed to load test data.") - else: - print("Failed to load training data.") \ No newline at end of file + train_file_path = "path/to/datingTrainSet2.tsv" # 替换为您的训练文件路径 + test_file_path = "path/to/datingTestSet2.tsv" # 替换为您的测试文件路径 + + # 测试KNN算法,默认k=3 + test_knn(train_file_path, test_file_path) + + # 您可以尝试不同的k值来查看对准确率的影响 + # test_knn(train_file_path, test_file_path, k=5) + # test_knn(train_file_path, test_file_path, k=1) \ No newline at end of file