generated from LufsX/Challenge-kNN
This commit is contained in:
+14
@@ -7,9 +7,12 @@ name = "张钦诚"
|
|||||||
|
|
||||||
def classify0(inX, dataSet, labels, k=5):
|
def classify0(inX, dataSet, labels, k=5):
|
||||||
inX = np.array(inX)
|
inX = np.array(inX)
|
||||||
|
# 计算欧氏距离的平方,避免开平方以提高效率
|
||||||
distances = np.sum((dataSet - inX) ** 2, axis=1)
|
distances = np.sum((dataSet - inX) ** 2, axis=1)
|
||||||
|
# 获取距离最小的k个样本的索引
|
||||||
nearest_indices = np.argpartition(distances, k)[:k]
|
nearest_indices = np.argpartition(distances, k)[:k]
|
||||||
nearest_labels = labels[nearest_indices]
|
nearest_labels = labels[nearest_indices]
|
||||||
|
# 统计k个最近邻中出现次数最多的标签
|
||||||
label_count = Counter(nearest_labels)
|
label_count = Counter(nearest_labels)
|
||||||
return label_count.most_common(1)[0][0]
|
return label_count.most_common(1)[0][0]
|
||||||
|
|
||||||
@@ -20,8 +23,12 @@ def load_data(file_path):
|
|||||||
return features, labels
|
return features, labels
|
||||||
|
|
||||||
def standardize(train_data, test_data):
|
def standardize(train_data, test_data):
|
||||||
|
"""
|
||||||
|
对训练数据和测试数据进行标准化处理。
|
||||||
|
"""
|
||||||
mean = np.mean(train_data, axis=0)
|
mean = np.mean(train_data, axis=0)
|
||||||
std = np.std(train_data, axis=0)
|
std = np.std(train_data, axis=0)
|
||||||
|
# 防止标准差为零
|
||||||
std[std == 0] = 1
|
std[std == 0] = 1
|
||||||
train_standardized = (train_data - mean) / std
|
train_standardized = (train_data - mean) / std
|
||||||
test_standardized = (test_data - mean) / std
|
test_standardized = (test_data - mean) / std
|
||||||
@@ -31,17 +38,24 @@ if __name__ == "__main__":
|
|||||||
train_file = "datingTrainSet2.tsv"
|
train_file = "datingTrainSet2.tsv"
|
||||||
test_file = "datingTestSet2.tsv"
|
test_file = "datingTestSet2.tsv"
|
||||||
|
|
||||||
|
# 加载训练数据
|
||||||
train_data, train_labels = load_data(train_file)
|
train_data, train_labels = load_data(train_file)
|
||||||
|
# 加载测试数据
|
||||||
test_data, test_labels = load_data(test_file)
|
test_data, test_labels = load_data(test_file)
|
||||||
|
|
||||||
|
# 数据标准化
|
||||||
train_data, test_data = standardize(train_data, test_data)
|
train_data, test_data = standardize(train_data, test_data)
|
||||||
|
|
||||||
|
# 测试样本
|
||||||
test_sample = [65915, 3.868359, 0.128078]
|
test_sample = [65915, 3.868359, 0.128078]
|
||||||
|
# 标准化测试样本
|
||||||
test_sample = (np.array(test_sample) - np.mean(train_data, axis=0)) / np.std(train_data, axis=0)
|
test_sample = (np.array(test_sample) - np.mean(train_data, axis=0)) / np.std(train_data, axis=0)
|
||||||
|
|
||||||
|
# 预测标签
|
||||||
predicted_label = classify0(test_sample, train_data, train_labels)
|
predicted_label = classify0(test_sample, train_data, train_labels)
|
||||||
print(f"预测的分类标签: {predicted_label}")
|
print(f"预测的分类标签: {predicted_label}")
|
||||||
|
|
||||||
|
# 计算准确率
|
||||||
predictions = np.array([classify0(sample, train_data, train_labels) for sample in test_data])
|
predictions = np.array([classify0(sample, train_data, train_labels) for sample in test_data])
|
||||||
accuracy = np.mean(predictions == test_labels) * 100
|
accuracy = np.mean(predictions == test_labels) * 100
|
||||||
print(f"准确率: {accuracy:.2f}%")
|
print(f"准确率: {accuracy:.2f}%")
|
||||||
|
|||||||
Reference in New Issue
Block a user