# -*- coding: utf-8 -*-
"""
Created on Wed Sep 25 15:16:30 2019
@author: King
"""
#K近邻算法(自己写未直接调用现存方法)
import numpy as np
import matplotlib.pyplot as plt
from math import sqrt
from collections import Counter
#已有训练数据集(列表)
raw_data_X = [
[3.393533211, 2.331273381],
[3.110073483, 1.781539638],
[1.343808831, 3.368360954],
[3.582294042, 4.679179110],
[2.280362439, 2.866990263],
[7.423436942, 4.696522875],
[5.745051997, 3.533989803],
[9.172168622, 2.511101045],
[7.792783481, 3.424088941],
[7.939820817, 0.791637231]
]
raw_data_y = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
#转换之后,运算速度会很快(数组类别)
X_train = np.array(raw_data_X)
Y_train = np.array(raw_data_y)
print(X_train)
print(Y_train)
#利用散点图,s代表大小 (属性值Y_train==0,坐标值下标0/1)
plt.scatter(X_train[Y_train==0,0],X_train[Y_train==0,1],color='g',s=100)
plt.scatter(X_train[Y_train==1,0],X_train[Y_train==1,1],color='r',s=100)
x=np.array([8.345051997, 3.333989803])
#把该点加入到散点图
plt.scatter(x[0],x[1],s=100,color='b')
#求距离
#distances=[]
#for x_train in X_train:
# d=sqrt(np.sum((x_train-x)**2))
# distances.append(d)
print("***********************************")
distances = [sqrt(np.sum((x_train-x)**2)) for x_train in X_train]
#distances = [sqrt((x_train-x)**2) for x_train in X_train]
print(distances)
#print(np.sort(distances))
##按距离排好序
#print(np.argsort(distances))
##按索引值排好序
nearest=np.argsort(distances)
k=6
#指定k值
topk_y=[Y_train[i] for i in nearest[:6]]
#拿到下标值最小的前k个点的属性值
print(topk_y)
votes=Counter(topk_y)
print(votes)
#votes.most_common(1)
#取票数最多的那n项
print(votes.most_common(1)[0][0])
#先取到元组,再取到"键值对"的中键