[英]Different output for same function
我在 python 中實現了 KNN 算法。
import math
#height,width,deepth,thickness,Label
data_set = [(2,9,8,4, "Good"),
(3,7,7,9, "Bad"),
(10,3,10,3, "Good"),
(2,9,6,10, "Good"),
(3,3,2,5, "Bad"),
(2,8,5,6, "Bad"),
(7,2,3,10, "Good"),
(1,10,8,10, "Bad"),
(2,8,1,10, "Good")
]
A = (3,2,1,5)
B = (8,3,1,2)
C = (6,10,8,3)
D = (9,6,4,1)
distances = []
labels = []
def calc_distance(datas,test):
for data in datas:
distances.append(
( round(math.sqrt(((data[0] - test[0])**2 + (data[1] - test[1])**2 + (data[2] - test[2])**2 + (data[3] - test[3])**2)), 3), data[4] ))
return distances
def most_frequent(list1):
return max(set(list1), key = list1.count)
def get_neibours(k):
distances.sort()
print(distances[:k])
for distance in distances[:k]:
labels.append(distance[1])
print("It can be classified as: ", end="")
print(most_frequent(labels))
calc_distance(data_set,D)
get_neibours(7)
calc_distance(data_set,D)
get_neibours(7)
我大部分時間工作得很好,我得到了正確的標簽。 例如對於 D,我確實得到標簽“好”。 但是我發現了一個錯誤,例如當我調用它兩次時:
calc_distance(data_set,D)
get_neibours(7)
calc_distance(data_set,D)
get_neibours(7)
我運行了幾次,我得到了不同的輸出——“好”和“壞”,當我運行程序幾次時..
一定有我無法找出的錯誤。
問題是您使用相同的距離和標簽,對第 k 個元素進行排序和獲取。 在函數內創建列表並返回它。 檢查下面的修改。
import math
data_set = [
(2,9,8,4, "Good"),
(3,7,7,9, "Bad"),
(10,3,10,3, "Good"),
(2,9,6,10, "Good"),
(3,3,2,5, "Bad"),
(2,8,5,6, "Bad"),
(7,2,3,10, "Good"),
(1,10,8,10, "Bad"),
(2,8,1,10, "Good"),
]
A = (3,2,1,5)
B = (8,3,1,2)
C = (6,10,8,3)
D = (9,6,4,1)
def calc_distance(datas, test):
distances = []
for data in datas:
distances.append(
( round(math.sqrt(((data[0] - test[0])**2 + (data[1] - test[1])**2 + (data[2] - test[2])**2 + (data[3] - test[3])**2)), 3), data[4] ))
return distances
def most_frequent(list1):
return max(set(list1), key = list1.count)
def get_neibours(distances, k):
labels = []
distances.sort()
print(distances[:k])
for distance in distances[:k]:
labels.append(distance[1])
print("It can be classified as: ", end="")
print(most_frequent(labels))
distances = calc_distance(data_set,D)
get_neibours(distances, 7)
distances = calc_distance(data_set,D)
get_neibours(distances, 7)
[(7.071, '好'), (8.062, '壞'), (8.888, '壞'), (9.11, '好'), (10.1, '好'), (10.488, '壞'), ( 11.958, 'Good')] 可歸類為:Good
[(7.071, '好'), (8.062, '壞'), (8.888, '壞'), (9.11, '好'), (10.1, '好'), (10.488, '壞'), ( 11.958, 'Good')] 可歸類為:Good
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.