KNN算法

 

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)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

羽峰码字

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值