使用Python实现KNN算法

使用Python实现KNN算法

实验名称

KNN算法的实现,利用数据集processed.cleveland.data
UCI公开数据集-heart disease,属性信息如下:

  1. #3 (age)
  2. #4 (sex)
  3. #9 (cp)
  4. #10 (trestbps)
  5. #12 (chol)
  6. #16 (fbs)
  7. #19 (restecg)
  8. #32 (thalach)
  9. #38 (exang)
  10. #40 (oldpeak)
  11. #41 (slope)
  12. #44 (ca)
  13. #51 (thal)
  14. #58 (num) (the predicted attribute)
    数据集参考网址:https://archive.ics.uci.edu/ml/datasets/Heart+Disease

算法实现

训练数据集以及KNN函数的编写

import numpy as np
import operator
f=open('D:/Tencent/QQ/qq文件/WeChat Files/w384660107/FileStorage/File/2020-05/processed.cleveland.data')
age=[]
sex=[]
cp=[]
tresbps=[]
chol=[]
fbs=[]
restecg=[]
thalach=[]
exang=[]
oldpeak=[]
slope=[]
ca=[]
thal=[]
num=[]
for i,d in enumerate(f):
    d=d.strip()
    if not d:
        continue
    d=list(map(float,d.split(',')))
    age.append(d[0])
    sex.append(d[1])
    cp.append(d[2])
    tresbps.append(d[3])
    chol.append(d[4])
    fbs.append(d[5])
    restecg.append(d[6])
    thalach.append(d[7])
    exang.append(d[8])
    oldpeak.append(d[9])
    slope.append(d[10])
    ca.append(d[11])
    thal.append(d[12])
    num.append(d[13])
group=np.empty(shape=[0,13],dtype=float)
labels=np.empty(shape=[0,1],dtype=float)
for i in range(0,303):
    group=np.append(group,[[age[i],sex[i],cp[i],tresbps[i],chol[i],fbs[i],restecg[i],thalach[i],exang[i],oldpeak[i],slope[i],ca[i],thal[i]]],axis=0)
    labels=np.append(labels,[[num[i]]],axis=0)
print(group.shape)
print(labels.shape)
print("我想我明白12345229")
def kNN_Classify(inX, dataSet, labels, k):
    dataSetSize = dataSet.shape[0]
    diffMat = np.tile(inX, (dataSetSize, 1)) - dataSet
    #关于tile函数的用法
    #>>> b=[1,3,5]
    #>>> tile(b,[2,3])
    #array([[1, 3, 5, 1, 3, 5, 1, 3, 5],
    #       [1, 3, 5, 1, 3, 5, 1, 3, 5]])
    sqDiffMat = diffMat ** 2
    sqDistances = sum(sqDiffMat)
    distances = sqDistances ** 0.5            #                           算距离
    sortedDistIndicies =np.argsort(distances)
    #关于argsort函数的用法
    #argsort函数返回的是数组值从小到大的索引值
    #>>> x = np.array([3, 1, 2])
    #>>> np.argsort(x)
    #array([1, 2, 0])
    classCount = {}                                          #定义一个字典
#   选择k个最近邻
    for i in range(k):
        voteLabel = labels[sortedDistIndicies[i]]
                                                            # 计算k个最近邻中各类别出现的次数
        classCount[voteLabel] = sortedDistIndicies + 1
 
    #返回出现次数最多的类别标签i
    maxCount = 0
    for key, value in classCount.items():
        if value > maxCount:
            maxCount = value
            maxIndex = key
    return maxIndex

KNN函数的实现以及对目标样本的分析

import numpy as np
dataSet=group
test=np.array([61.0,1.0,1.0,134.0,234.0,0.0,0.0,145.0,0.0,2.6,2.0,2.0,3.0])
k=1
outputLabel = kNN_Classify(test, dataSet, labels,1)
print("你输入的样本我想我明白925", test, "wsx心血管病的类别为我想我明白123 ", outputLabel)

实现结果

在这里插入图片描述
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值