knn+tkinter

使用python实现算法主体,还有用python自带的gui模块实现图形界面,能做到鼠标取点。

代码实现:

from tkinter import *
import tkinter as tk
import os
import numpy as np
import csv
import math
from PIL.FontFile import WIDTH
data_filename=os.path.join('D:\python3.7\data','iris','iris.data')
x=np.zeros((150,4),dtype='float')
dataSetNum=[]
dataSetClass=[]
with open(data_filename,'r') as input_file:
    reader=csv.reader(input_file)
    for i,row in enumerate(reader):
        data=[float(datum) for datum in row[0:4]]
        dataSetNum.append(data)
        strclass=[str(datum) for datum in row[4:5]]
        dataSetClass.append(strclass)
numArr=[]
for ele in dataSetNum:
    numArr.append(ele[2:4])
    
numSetFront=numArr[0:50]
numSetMiddle=numArr[50:100]
numSetLast=numArr[100:150]

root=Tk()
# root.geometry("1000x500+100+100")
root['background']="white"
root.geometry("800x600")
root.title("鸢尾花数据点集可视化")
root.iconbitmap("iris.ico")
w=Canvas(root,width=1000,height=500,background='white')
w.grid(rowspan=10,columnspan=100)
x1=0
y1=0
x2=0
y2=0
paint_x=0
paint_y=0
var=StringVar()
objArr=[]
for ele in numSetFront:
    x1,y1=(ele[0]*100-3),(ele[1]*100-3)
    x2,y2=(ele[0]*100+3),(ele[1]*100+3)
    w.create_oval(x1,y1,x2,y2,fill='red')
for ele in numSetMiddle:
    x1,y1=(ele[0]*100-3),(ele[1]*100-3)
    x2,y2=(ele[0]*100+3),(ele[1]*100+3)
    w.create_oval(x1,y1,x2,y2,fill='blue')
for ele in numSetLast:
    x1,y1=(ele[0]*100-3),(ele[1]*100-3)
    x2,y2=(ele[0]*100+3),(ele[1]*100+3)
    w.create_oval(x1,y1,x2,y2,fill='yellow')
def delete_obj(obj):
    w.delete(obj)
def paint(event):
    x1,y1=(event.x-3),(event.y-3)
    x2,y2=(event.x+3),(event.y+3)
    obj_dot=w.create_oval(x1,y1,x2,y2,fill='black')
    #print(event.x,'-->',event.y)
    objArr.append(obj_dot)
    if len(objArr)>1:
        delete_obj(objArr[0])
        objArr.remove(objArr[0])
    global paint_x
    global paint_y
    paint_x=event.x
    paint_y=event.y
def setValueFun(cla):
    global var
    var.set(cla)
    
# numSetFront_x=[]
# numSetFront_y=[]
# numSetMiddle_x=[]
# numSetMiddle_y=[]
# numSetLast_x=[]
# numSetLast_y=[]

# def getIndex_x(array=[]):
#     result=[]
#     for ele in array:
#         result.append(ele[0])
#     return result
# def getIndex_y(array=[]):
#     result=[]
#     for ele in array:
#         result.append(ele[1])
#     return result

# numSetFront_x=getIndex_x(numSetFront)
# numSetFront_y=getIndex_y(numSetFront)
# numSetMiddle_x=getIndex_x(numSetMiddle)
# numSetMiddle_y=getIndex_y(numSetMiddle)
# numSetLast_x=getIndex_x(numSetLast)
# numSetLast_y=getIndex_y(numSetLast)


def getDistance():
    resultDistance=0
    disFront=[]
    disMiddle=[]
    disLast=[]
    global paint_x
    global paint_y
    print(paint_x,'-->',paint_y)
    for ele in numSetFront:
        resultDistance=math.sqrt((paint_x-ele[0]*100)**2+(paint_y-ele[1]*100)**2)
        disFront.append(resultDistance/100)
    for ele in numSetMiddle:
        resultDistance=math.sqrt((paint_x-ele[0]*100)**2+(paint_y-ele[1]*100)**2)
        disMiddle.append(resultDistance/100)
    for ele in numSetLast:
        resultDistance=math.sqrt((paint_x-ele[0]*100)**2+(paint_y-ele[1]*100)**2)
        disLast.append(resultDistance/100)
    disFront.sort()
    disMiddle.sort()
    disLast.sort()
    
    disDic={}
    for ele in disFront:
        disDic[ele]='Iris-setosa'
    for ele in disMiddle:
        disDic[ele]='Iris-versicolor'
    for ele in disLast:
        disDic[ele]='Iris-virginica'
#     print(disDic)
    arrDic=[]
    arrDic=sorted(disDic.keys())
    k=0
    firstCount=0
    secondCount=0
    thirdCount=0
    while k<=20:
        if disDic[arrDic[k]]=='Iris-setosa':
            firstCount=firstCount+1
        elif disDic[arrDic[k]]=='Iris-versicolor':
            secondCount=secondCount+1
        else:
            thirdCount=thirdCount+1
        k=k+1
    item=[]
    item.append(firstCount)
    item.append(secondCount)
    item.append(thirdCount)
    max_index=item.index(max(item))
    if max_index==0:
        print('Iris-setosa')
        setValueFun('Iris-setosa')
    elif max_index==1:
        print('Iris-versicolor')
        setValueFun('Iris-versicolor')
    else:
        print('Iris-virginica')
        setValueFun('Iris-virginica')
#     print(disFront)
#     print(disMiddle)
#     print(disLast)
w.bind("<Button-1>",paint)
photo_red=PhotoImage(file="red.gif")
imgLabel_red=Label(root,image=photo_red,bg="white")
imgLabel_red.grid(row=11,column=0,sticky=W)
textLabel_red=Label(root,text="Iris-setosa",bg="white")
textLabel_red.grid(row=11,column=1,sticky=W)
photo_blue=PhotoImage(file="blue.gif")
imgLabel_blue=Label(root,image=photo_blue,bg="white")
imgLabel_blue.grid(row=12,column=0,sticky=W)
textLabel_blue=Label(root,text="Iris-versicolor",bg="white")
textLabel_blue.grid(row=12,column=1,sticky=W)
photo_yellow=PhotoImage(file="yellow.gif")
imgLabel_yellow=Label(root,image=photo_yellow,bg="white")
imgLabel_yellow.grid(row=13,column=0,sticky=W)
textLabel_yellow=Label(root,text="Iris-virginica",bg="white")
textLabel_yellow.grid(row=13,column=1,sticky=W)
theButton=Button(root,text="开始计算距离预测类别",width=50,command=getDistance)
theButton.grid(row=12,column=10,columnspan=10)
textLabel_title=Label(root,text="预测的类别是:",bg="white")
textLabel_title.grid(row=12,column=30)
textLabel=Label(root,textvariable=var,bg="white")
textLabel.grid(row=12,column=36)
mainloop()

图中黑色点是测试点,测试结果是最近的样本中第二种花的数量更多,所以将测试点归类为 Iris-versicolor

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值