机器学习--KNN基本实现

# _*_ 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)

 

上一篇:销售分析第二天:线性回归


下一篇:基于 lstm 的股票收盘价预测 -- python