# _*_ coding _*_
import numpy as np
import math
import operator
def get_data(dataset):
x = dataset[:,:-1].astype(np.float)
y = dataset[:,-1]
return x,y
# def cal_dis(a,b):
# x1,y1 = a[:]
# x2,y2 = b[:]
# dist = math.sqrt(math.pow(2,x2)-math.pow(2,x1))
def knnclassifer(dataset,predict,k=3):
x,y = get_data(dataset)
dic = {}
distince = np.sum((predict-x)**2,axis=1)**0.5
sorted_dict = np.argsort(distince)#[2 1 0 3 4]
countLabel = {}
for i in range(k):
label = y[sorted_dict[i]]
# print(i,sorted_dict[i],label)
countLabel[label] = countLabel.get(label,0)+1
new_dic = sorted(countLabel,key=operator.itemgetter(0),reverse=True)
return new_dic[0][0]
if __name__ == '__main__':
dataset = np.loadtxt("dataset.txt",dtype=np.str,delimiter=",")
predict = [2,2]
label = knnclassifer(dataset,predict,3)
print(label)