generated from LufsX/Challenge-kNN
This commit is contained in:
+64
-50
@@ -41,76 +41,90 @@ name = "邓龙树"
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
def euclidean_distance(point1, point2):
|
def euclidean_distance(point1, point2):
|
||||||
"""
|
"""计算两个点之间的欧氏距离。"""
|
||||||
计算两个点之间的欧氏距离。
|
return np.sqrt(np.sum((np.array(point1) - np.array(point2)) ** 2))
|
||||||
"""
|
|
||||||
return np.sqrt(np.sum((point1 - point2) ** 2))
|
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 classify0(inX, dataSet, labels, k=3):
|
def classify0(inX, dataSet, labels, k=3):
|
||||||
"""
|
"""KNN 分类算法。
|
||||||
KNN 分类算法。
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
- inX (numpy array): 输入待分类的样本。
|
- inX (list or numpy array): 输入待分类的样本。
|
||||||
- dataSet (numpy array): 训练数据集。
|
- dataSet (numpy array): 训练数据集。
|
||||||
- labels (numpy array): 训练集对应的标签。
|
- labels (numpy array): 训练集对应的标签。
|
||||||
- k (int): KNN 算法参数,默认值为3。
|
- k (int): KNN 算法参数,表示选择最近的k个邻居,默认值为3。
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
- int: 预测的分类标签。
|
- int: 预测的分类标签。
|
||||||
"""
|
"""
|
||||||
# 确保 labels 是 numpy 数组
|
distances = compute_distances(dataSet, inX)
|
||||||
labels = np.array(labels)
|
|
||||||
|
|
||||||
# 计算待分类样本与训练集中每个样本之间的距离
|
|
||||||
distances = [euclidean_distance(inX, x) for x in dataSet]
|
|
||||||
|
|
||||||
# 对距离进行排序,并获取距离最小的k个样本的索引
|
|
||||||
sorted_indices = np.argsort(distances)
|
sorted_indices = np.argsort(distances)
|
||||||
k_nearest_indices = sorted_indices[:k]
|
k_nearest_indices = sorted_indices[:k]
|
||||||
|
|
||||||
# 获取k个最近样本的标签
|
|
||||||
k_nearest_labels = labels[k_nearest_indices]
|
k_nearest_labels = labels[k_nearest_indices]
|
||||||
|
|
||||||
# 使用 np.unique 和 np.bincount 的一个更健壮的方法来找到出现次数最多的标签
|
|
||||||
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(file_path):
|
def load_data(file_path):
|
||||||
|
"""加载TSV格式数据文件。
|
||||||
|
|
||||||
|
Args:
|
||||||
|
- file_path (str): 数据文件的路径。
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
- tuple: 特征集和标签集。
|
||||||
"""
|
"""
|
||||||
加载 TSV 格式数据文件。
|
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 test_knn(train_file, test_file, k=3):
|
||||||
|
"""测试KNN算法。
|
||||||
|
|
||||||
|
Args:
|
||||||
|
- train_file (str): 训练数据文件的路径。
|
||||||
|
- test_file (str): 测试数据文件的路径。
|
||||||
|
- k (int): KNN算法参数,默认值为3。
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
- float: 测试集上的准确率。
|
||||||
"""
|
"""
|
||||||
data = np.loadtxt(file_path, delimiter="\t")
|
train_data, train_labels = load_data(train_file)
|
||||||
features = data[:, :-1]
|
if train_data is None or train_labels is None:
|
||||||
labels = data[:, -1].astype(int)
|
print("Failed to load training data.")
|
||||||
return features, labels
|
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__":
|
if __name__ == "__main__":
|
||||||
# 测试代码
|
train_file_path = "path/to/datingTrainSet2.tsv" # 替换为您的训练文件路径
|
||||||
train_file = "datingTrainSet2.tsv"
|
test_file_path = "path/to/datingTestSet2.tsv" # 替换为您的测试文件路径
|
||||||
test_file = "datingTestSet2.tsv" # 假设有一个测试文件用于验证
|
|
||||||
|
|
||||||
# 加载训练数据和测试数据
|
# 测试KNN算法,默认k=3
|
||||||
train_data, train_labels = load_data(train_file)
|
test_knn(train_file_path, test_file_path)
|
||||||
test_data, test_labels = load_data(test_file) # 取消注释以测试整个测试集
|
|
||||||
|
|
||||||
# 测试 classify0 函数
|
# 您可以尝试不同的k值来查看对准确率的影响
|
||||||
sample = [65915, 3.868359, 0.128078] # 示例样本
|
# test_knn(train_file_path, test_file_path, k=5)
|
||||||
predicted_label = classify0(sample, train_data, train_labels)
|
# test_knn(train_file_path, test_file_path, k=1)
|
||||||
print(f"Predicted label for sample {sample}: {predicted_label}")
|
|
||||||
|
|
||||||
# 测试整个测试集(可选)
|
|
||||||
accuracy = np.mean([classify0(test_data[i], train_data, train_labels) == test_labels[i] for i in range(len(test_data))])
|
|
||||||
print(f"Accuracy on test set: {accuracy}")
|
|
||||||
# test_data, test_labels = load_data(test_file) # 如果需要测试,可以取消注释并添加相应的测试代码
|
|
||||||
|
|
||||||
# 测试 classify0 函数
|
|
||||||
sample = [65915, 3.868359, 0.128078] # 示例样本
|
|
||||||
predicted_label = classify0(sample, train_data, train_labels)
|
|
||||||
print(f"Predicted label for sample {sample}: {predicted_label}")
|
|
||||||
|
|
||||||
# 如果需要测试整个测试集,可以添加以下代码(注意:这部分代码是为了完整性而添加的,题目没明确要求)
|
|
||||||
# accuracy = np.mean([classify0(test_data[i], train_data, train_labels) == test_labels[i] for i in range(len(test_data))])
|
|
||||||
# print(f"Accuracy on test set: {accuracy}")
|
|
||||||
Reference in New Issue
Block a user