generated from LufsX/Experiment-kNN
This commit is contained in:
+8
-7
@@ -41,7 +41,7 @@ name = "尹家立"
|
||||
|
||||
|
||||
# 这里写自己的代码实现
|
||||
def classify(input_vector, dataSet, labels, k=3):
|
||||
def classify(input_vector, dataSet, labels, k):
|
||||
# 计算输入向量与每个训练样本的欧氏距离
|
||||
distances = np.linalg.norm(dataSet - input_vector, axis=1)
|
||||
|
||||
@@ -57,7 +57,6 @@ def classify(input_vector, dataSet, labels, k=3):
|
||||
return predicted_label
|
||||
|
||||
|
||||
|
||||
# 这里也写自己的代码实现
|
||||
def load_dataset(directory):
|
||||
data = []
|
||||
@@ -124,10 +123,12 @@ if __name__ == "__main__":
|
||||
"00000000000001111111110000000000"
|
||||
"00000000000000000111100000000000"
|
||||
)
|
||||
predicted_label = classify(
|
||||
np.array([int(char) for char in test_data]).reshape(1, 1024),
|
||||
train_data,
|
||||
train_labels,
|
||||
)
|
||||
for i in range(1,21):
|
||||
predicted_label = classify(
|
||||
np.array([int(char) for char in test_data]).reshape(1, 1024),
|
||||
train_data,
|
||||
train_labels,
|
||||
i,
|
||||
)
|
||||
|
||||
print(f"预测的标签: {predicted_label}")
|
||||
|
||||
Reference in New Issue
Block a user