KNN算法常用的距离有:
算法流程:
1. 未知样本与所有已知样本的距离计算
2. 对距离由小到大排序
3. 取与他最近的几个点(K)
4.统计所取点中的已知样本数
5.看那个类别占比更多
(k:超参数,1)要选基数,2)k最好小于20)
用处:对数据的初次尝试与探索,当作一个参考;使用KNN算法填补数据中的缺失值
#coding:utf-8
import numpy as np
import pandas as pd
import matplotlib.pylab as plt
data = pd.read_csv("vehicle.csv")
print data
feature = np.array(data.iloc[:,0:2])
lables = data['label'].tolist()
print lables
plt.scatter(data['length'][data['label']=='car'],data['width'][data['label'] == 'car'],c = 'y')
plt.scatter(data['length'][data['label']=='truck'],data['width'][data['label'] == 'truck'],c = 'r')
# plt.show()
#step 1 calculate distance
numSamples = data.shape[0]#获取行数
# print numSamples
test = [4.7,2.1]
diff = np.tile(test,(numSamples,1)) - feature #np.tile 重复某个数组 重复test 为150行1列
squreDiff = diff**2
# print squreDiff
squreDist = np.sum(squreDiff,axis = 1) #将长度和宽度相减得到的值相加
# 当axis为0时,是压缩行,即将每一列的元素相加,将矩阵压缩为一行
# 当axis为1时,是压缩列,即将每一行的元素相加,将矩阵压缩为一列
# >>> np.sum([[0, 1], [0, 5]], axis=0)
# array([0, 6])
# >>> np.sum([[0, 1], [0, 5]], axis=1)
# array([1, 5])
# print squreDist
distance = squreDist **0.5
# print distance
sorteDistIndices = np.argsort(distance) #排序结果为对应的索引
k = 9
classCount = {}
label_count =[]
for i in range(k):
voteLabel = lables[sorteDistIndices[i]] #lables
classCount[voteLabel] = classCount.get(voteLabel,0)+1
label_count.append(voteLabel)
print classCount
print label_count
from collections import Counter
word_counts = Counter(label_count)
top = word_counts.most_common(1)