generated from LufsX/Experiment-kNN
This commit is contained in:
+4
-28
@@ -42,11 +42,10 @@ def classify(input_vector, dataSet, labels, k=2):
|
|||||||
- int: 预测的分类标签。
|
- int: 预测的分类标签。
|
||||||
"""
|
"""
|
||||||
dataSet_size = dataSet.shape[0]
|
dataSet_size = dataSet.shape[0]
|
||||||
# 计算欧氏距离
|
# 计算曼哈顿距离
|
||||||
diff_mat = np.tile(input_vector, (dataSet_size, 1)) - dataSet
|
diff_mat = np.tile(input_vector, (dataSet_size, 1)) - dataSet
|
||||||
sq_diff_mat = diff_mat ** 2
|
abs_diff_mat = np.abs(diff_mat)
|
||||||
sq_distances = sq_diff_mat.sum(axis=1)
|
distances = abs_diff_mat.sum(axis=1)
|
||||||
distances = np.sqrt(sq_distances)
|
|
||||||
# 对距离进行排序
|
# 对距离进行排序
|
||||||
sorted_indices = distances.argsort()
|
sorted_indices = distances.argsort()
|
||||||
class_count = {}
|
class_count = {}
|
||||||
@@ -56,27 +55,7 @@ def classify(input_vector, dataSet, labels, k=2):
|
|||||||
sorted_class_count = sorted(class_count.items(), key=lambda x: x[1], reverse=True)
|
sorted_class_count = sorted(class_count.items(), key=lambda x: x[1], reverse=True)
|
||||||
return sorted_class_count[0][0]
|
return sorted_class_count[0][0]
|
||||||
|
|
||||||
def cross_validation(dataSet, labels, k_list=(2, 3, 5, 7, 9)):
|
|
||||||
from sklearn.model_selection import KFold
|
|
||||||
kf = KFold(n_splits=5)
|
|
||||||
best_k = None
|
|
||||||
best_accuracy = 0
|
|
||||||
for k in k_list:
|
|
||||||
accuracies = []
|
|
||||||
for train_index, test_index in kf.split(dataSet):
|
|
||||||
train_data, test_data = dataSet[train_index], dataSet[test_index]
|
|
||||||
train_labels, test_labels = np.array(labels)[train_index], np.array(labels)[test_index]
|
|
||||||
predictions = []
|
|
||||||
for vector in test_data:
|
|
||||||
predicted_label = classify(vector.reshape(1, -1), train_data, train_labels, k)
|
|
||||||
predictions.append(predicted_label)
|
|
||||||
accuracy = sum([1 for p, t in zip(predictions, test_labels) if p == t]) / len(test_labels)
|
|
||||||
accuracies.append(accuracy)
|
|
||||||
avg_accuracy = sum(accuracies) / len(accuracies)
|
|
||||||
if avg_accuracy > best_accuracy:
|
|
||||||
best_accuracy = avg_accuracy
|
|
||||||
best_k = k
|
|
||||||
return best_k
|
|
||||||
|
|
||||||
def img_to_vector(filename):
|
def img_to_vector(filename):
|
||||||
"""
|
"""
|
||||||
@@ -122,8 +101,6 @@ def load_dataset(directory):
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
train_data, train_labels = load_dataset("trainingDigits")
|
train_data, train_labels = load_dataset("trainingDigits")
|
||||||
best_k = cross_validation(train_data, train_labels)
|
|
||||||
print("Best k value:", best_k)
|
|
||||||
|
|
||||||
test_data = (
|
test_data = (
|
||||||
"00000000000000110000000000000000"
|
"00000000000000110000000000000000"
|
||||||
@@ -163,7 +140,6 @@ if __name__ == "__main__":
|
|||||||
np.array([int(char) for char in test_data]).reshape(1, 1024),
|
np.array([int(char) for char in test_data]).reshape(1, 1024),
|
||||||
train_data,
|
train_data,
|
||||||
train_labels,
|
train_labels,
|
||||||
best_k
|
|
||||||
)
|
)
|
||||||
|
|
||||||
print(f"预测的标签: {predicted_label}")
|
print(f"预测的标签: {predicted_label}")
|
||||||
Reference in New Issue
Block a user