generated from LufsX/Challenge-kNN
This commit is contained in:
+17
-34
@@ -71,7 +71,7 @@ def classify0(inX, dataSet, labels, k=3):
|
|||||||
Returns:
|
Returns:
|
||||||
- int: 预测的分类标签。
|
- int: 预测的分类标签。
|
||||||
"""
|
"""
|
||||||
dists = np.sqrt(((dataSet - inX) ** 2).sum(axis=1))
|
dists = np.sum(np.abs(dataSet - inX), axis=1)
|
||||||
sorted_index = np.argsort(dists)
|
sorted_index = np.argsort(dists)
|
||||||
class_count = {}
|
class_count = {}
|
||||||
for i in range(k):
|
for i in range(k):
|
||||||
@@ -84,48 +84,31 @@ def classify0(inX, dataSet, labels, k=3):
|
|||||||
return -1
|
return -1
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# 加载数据
|
# 假设 load_data 和 normalize 函数已经定义
|
||||||
def load_data(file_path):
|
train_features, train_labels = load_data("datingTrainSet2.tsv")
|
||||||
data = np.loadtxt(file_path, delimiter="\t")
|
test_features, test_labels = load_data("datingTestSet2.tsv")
|
||||||
features = data[:, :-1]
|
|
||||||
labels = data[:, -1].astype(int)
|
|
||||||
return features, labels
|
|
||||||
|
|
||||||
train_file_path = "datingTrainSet2.tsv"
|
|
||||||
test_file_path = "datingTestSet2.tsv"
|
|
||||||
|
|
||||||
train_features, train_labels = load_data(train_file_path)
|
|
||||||
test_features, test_labels = load_data(test_file_path)
|
|
||||||
|
|
||||||
# 归一化特征
|
|
||||||
def normalize(features):
|
|
||||||
min_vals = features.min(axis=0)
|
|
||||||
max_vals = features.max(axis=0)
|
|
||||||
ranges = max_vals - min_vals
|
|
||||||
normalized = (features - min_vals) / ranges
|
|
||||||
normalized[ranges == 0] = 0 # 防止除以零
|
|
||||||
return normalized
|
|
||||||
|
|
||||||
train_features = normalize(train_features)
|
train_features = normalize(train_features)
|
||||||
test_features = normalize(test_features)
|
test_features = normalize(test_features)
|
||||||
|
|
||||||
|
best_k = 3
|
||||||
|
best_accuracy = 0.0
|
||||||
|
|
||||||
|
for k in range(1, 10): # 尝试不同的 K 值
|
||||||
correct_count = 0
|
correct_count = 0
|
||||||
for i in range(len(test_features)):
|
for i in range(len(test_features)):
|
||||||
test_feature = test_features[i].reshape(1, -1)
|
test_feature = test_features[i].reshape(1, -1)
|
||||||
predict_label = classify0(test_feature, train_features, train_labels, k=3)
|
predict_label = classify0(test_feature, train_features, train_labels, k=k)
|
||||||
if predict_label == -1:
|
if predict_label == -1:
|
||||||
continue
|
continue
|
||||||
correct_count += (predict_label == test_labels[i])
|
correct_count += (predict_label == test_labels[i])
|
||||||
|
|
||||||
accuracy = correct_count / len(test_features)
|
accuracy = correct_count / len(test_features)
|
||||||
print(f"Accuracy: {accuracy:.2f}")
|
print(f"Accuracy with k={k}: {accuracy:.2f}")
|
||||||
correct_count = 0
|
if accuracy > best_accuracy:
|
||||||
for i in range(len(test_features)):
|
best_accuracy = accuracy
|
||||||
test_feature = test_features[i].reshape(1, -1)
|
best_k = k
|
||||||
predict_label = classify0(test_feature, train_features, train_labels, k=3)
|
|
||||||
if predict_label == -1:
|
print(f"Best K: {best_k}, Best Accuracy: {best_accuracy:.2f}")
|
||||||
continue
|
|
||||||
correct_count += (predict_label == test_labels[i])
|
|
||||||
|
|
||||||
accuracy = correct_count / len(test_features)
|
|
||||||
print(f"Accuracy: {accuracy:.2f}")
|
|
||||||
|
|||||||
Reference in New Issue
Block a user