generated from LufsX/Challenge-kNN
This commit is contained in:
+19
-11
@@ -58,8 +58,6 @@ def load_data(file_path):
|
|||||||
return features, labels
|
return features, labels
|
||||||
|
|
||||||
|
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
def classify0(inX, dataSet, labels, k=3):
|
def classify0(inX, dataSet, labels, k=3):
|
||||||
"""
|
"""
|
||||||
KNN 分类算法。
|
KNN 分类算法。
|
||||||
@@ -85,29 +83,29 @@ def classify0(inX, dataSet, labels, k=3):
|
|||||||
else:
|
else:
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
# 加载数据
|
||||||
def load_data(file_path):
|
def load_data(file_path):
|
||||||
data = np.loadtxt(file_path, delimiter="\t")
|
data = np.loadtxt(file_path, delimiter="\t")
|
||||||
features = data[:, :-1]
|
features = data[:, :-1]
|
||||||
labels = data[:, -1].astype(int)
|
labels = data[:, -1].astype(int)
|
||||||
return features, labels
|
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):
|
def normalize(features):
|
||||||
min_vals = features.min(axis=0)
|
min_vals = features.min(axis=0)
|
||||||
max_vals = features.max(axis=0)
|
max_vals = features.max(axis=0)
|
||||||
ranges = max_vals - min_vals
|
ranges = max_vals - min_vals
|
||||||
normalized = (features - min_vals) / ranges
|
normalized = (features - min_vals) / ranges
|
||||||
normalized[ranges == 0] = 0 # Prevent division by zero
|
normalized[ranges == 0] = 0 # 防止除以零
|
||||||
return normalized
|
return normalized
|
||||||
|
|
||||||
train_file_path = "datingTrainSet2.tsv"
|
|
||||||
train_features, train_labels = load_data(train_file_path)
|
|
||||||
|
|
||||||
test_file_path = "datingTestSet2.tsv"
|
|
||||||
test_features, test_labels = load_data(test_file_path)
|
|
||||||
|
|
||||||
# Normalize features
|
|
||||||
train_features = normalize(train_features)
|
train_features = normalize(train_features)
|
||||||
test_features = normalize(test_features)
|
test_features = normalize(test_features)
|
||||||
|
|
||||||
@@ -121,3 +119,13 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
accuracy = correct_count / len(test_features)
|
accuracy = correct_count / len(test_features)
|
||||||
print(f"Accuracy: {accuracy:.2f}")
|
print(f"Accuracy: {accuracy:.2f}")
|
||||||
|
correct_count = 0
|
||||||
|
for i in range(len(test_features)):
|
||||||
|
test_feature = test_features[i].reshape(1, -1)
|
||||||
|
predict_label = classify0(test_feature, train_features, train_labels, k=3)
|
||||||
|
if predict_label == -1:
|
||||||
|
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