一、界面实现
from tkinter import *
from tkinter import ttk
import NB
def main():
win = Tk()
win.title('甜的西瓜挑选系统')
win.geometry('1000x600')
lb2 = Label(win, text="色泽", font="tahoma 12 normal")
lb2.grid(column=0, row=4, padx=8, pady=4)
def show_data_2(*args):
#print(cbx_2.get())
pass
data2 = ["青绿", "乌黑", "浅白"]
cbx_2 = ttk.Combobox(win, width=12, height=8)
cbx_2.grid(column=0, row=5)
cbx_2.configure(state="readonly")
cbx_2["values"] = data2
cbx_2.current(0)
cbx_2.bind("<>" , show_data_2)
lb21 = Label(win, text="根蒂", font="tahoma 12 normal")
lb21.grid(column=14, row=4, padx=8, pady=4)
def show_data_21(*args):
#print(cbx_21.get())
pass
data21 = ['蜷缩', '硬挺', '稍蜷']
cbx_21 = ttk.Combobox(win, width=12, height=8)
cbx_21.grid(column=14, row=5)
cbx_21.configure(state="readonly")
cbx_21["values"] = data21
cbx_21.current(0)
cbx_21.bind("<>" , show_data_21)
lb22 = Label(win, text="敲声", font="tahoma 12 normal")
lb22.grid(column=24, row=4, padx=8, pady=4)
def show_data_22(*args):
#print(cbx_22.get())
pass
data22 = ['浊响', '清脆', '沉闷']
cbx_22 = ttk.Combobox(win, width=12, height=8)
cbx_22.grid(column=24, row=5)
cbx_22.configure(state="readonly")
cbx_22["values"] = data22
cbx_22.current(0)
cbx_22.bind("<>" , show_data_22)
lb23 = Label(win, text="纹理", font="tahoma 12 normal")
lb23.grid(column=34, row=4, padx=8, pady=4)
def show_data_23(*args):
#print(cbx_23.get())
pass
data23 = ['模糊', '稍糊', '清晰']
cbx_23 = ttk.Combobox(win, width=12, height=8)
cbx_23.grid(column=34, row=5)
cbx_23.configure(state="readonly")
cbx_23["values"] = data23
cbx_23.current(0)
cbx_23.bind("<>" , show_data_23)
lb24 = Label(win, text="脐部", font="tahoma 12 normal")
lb24.grid(column=40, row=4, padx=8, pady=4)
def show_data_24(*args):
#print(cbx_24.get())
pass
data24 = ['凹陷', '平坦', '稍凹']
cbx_24 = ttk.Combobox(win, width=12, height=8)
cbx_24.grid(column=40, row=5)
cbx_24.configure(state="readonly")
cbx_24["values"] = data24
cbx_24.current(0)
cbx_24.bind("<>" , show_data_24)
lb25 = Label(win, text="触感", font="tahoma 12 normal")
lb25.grid(column=42, row=4, padx=8, pady=4)
def show_data_25(*args):
#print(cbx_25.get())
pass
data25 = ['硬滑', '软粘']
cbx_25 = ttk.Combobox(win, width=12, height=8)
cbx_25.grid(column=42, row=5)
cbx_25.configure(state="readonly")
cbx_25["values"] = data25
cbx_25.current(0)
cbx_25.bind("<>" , show_data_25)
def predict():
a,b,c,d,e,f=cbx_2.get(),cbx_21.get(),cbx_22.get(),cbx_23.get(),cbx_24.get(),cbx_25.get()
print(type(a),b,c,d,e,f)
testEntry1=[a,b,c,d,e,f]
result=NB.testingNB(testEntry1)
E1.insert(0, result)
b = Button(win, text='预测', font=('KaiTi', 36, 'bold'), height=1,bg='pink', fg='green', bd=4, width=5, command=predict)
b.grid(column=24, row=15, pady=5)
L1 = Label(win, text="预测西瓜类型结果")
L1.grid(column=40, row=15, pady=10)
E1 = Entry(win)
E1.grid(column=42, row=15, pady=2)
win.mainloop()
if __name__ == '__main__':
main()
二、朴素贝叶斯算法实现
from numpy import *
def loadDataSet():
postingList=[
['青绿', '蜷缩', '浊响', '清晰', '凹陷', '硬滑'],
['乌黑', '蜷缩', '沉闷', '清晰', '凹陷', '硬滑'],
['乌黑', '蜷缩', '浊响', '清晰', '凹陷', '硬滑'],
['青绿', '蜷缩', '沉闷', '清晰', '凹陷', '硬滑'],
['浅白', '蜷缩', '浊响', '清晰', '凹陷', '硬滑'],
['青绿', '稍蜷', '浊响', '清晰', '稍凹', '软粘'],
['乌黑', '稍蜷', '浊响', '稍糊', '稍凹', '软粘'],
['乌黑', '稍蜷', '浊响', '清晰', '稍凹', '硬滑'],
['乌黑', '稍蜷', '沉闷', '稍糊', '稍凹', '硬滑'],
['青绿', '硬挺', '清脆', '清晰', '平坦', '软粘'],
['浅白', '硬挺', '清脆', '模糊', '平坦', '硬滑'],
['浅白', '蜷缩', '浊响', '模糊', '平坦', '软粘'],
['青绿', '稍蜷', '浊响', '稍糊', '凹陷', '硬滑'],
['浅白', '稍蜷', '沉闷', '稍糊', '凹陷', '硬滑'],
['乌黑', '稍蜷', '浊响', '清晰', '稍凹', '软粘'],
['浅白', '蜷缩', '浊响', '稍糊', '凹陷', '硬滑'],
['青绿', '蜷缩', '沉闷', '清晰', '稍凹', '硬滑']
]
classVec=[1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0]
return postingList,classVec
def createVocabList(dataSet):
vocabSet=set([])
for document in dataSet:
vocabSet|=set(document)
return list(vocabSet)
def setOfWords2Vec(vocabList,inputSet):
returnVec=[0]*len(vocabList)
for word in inputSet:
if word in vocabList:
returnVec[vocabList.index(word)]=1
else:
print('not in vocabulary')
return returnVec
def bagOfWords2VecMN(vocabList,inputSet):
returnVec=[0]*len(vocabList)
for word in inputSet:
if word in vocabList:
returnVec[vocabList.index(word)]+=1
return returnVec
def wordsAll(vocabList,listOPosts):
trainMat=[]
for postinDoc in listOPosts:
trainMat.append(bagOfWords2VecMN(vocabList,postinDoc))
return trainMat
def trainND0(trainMatrix,trainCategory):
numTrainDocs=len(trainMatrix)
numWords=len(trainMatrix[0])
pAbusive=sum(trainCategory)/float(numTrainDocs)
p0Num=ones(numWords)
p1Num=ones(numWords)
p0Denom=2.0
p1Denom=2.0
for i in range(numTrainDocs):
if trainCategory[i]==1:
p1Num+=trainMatrix[i]
p1Denom+=sum(trainMatrix[i])
else:
p0Num+=trainMatrix[i]
p0Denom+=sum(trainMatrix[i])
p1Vec=log(p1Num/p1Denom)
p0Vec=log(p0Num/p0Denom)
return p0Vec,p1Vec,pAbusive
def classifyNB(vec2Classify,p0Vec,p1Vec,pClass1):
p1=sum(vec2Classify*p1Vec)+log(pClass1)
p0=sum(vec2Classify*p0Vec)+log(1.0-pClass1)
if p1>p0:
return 1
else:
return 0
def testingNB(testEntry1):
listOPosts,listClasses = loadDataSet()
myVocabList=createVocabList(listOPosts)
trainMat=wordsAll(myVocabList,listOPosts)
p0V,p1V,pAb=trainND0(trainMat,listClasses)
thisDoc=array(setOfWords2Vec(myVocabList,testEntry1))
if classifyNB(thisDoc,p0V,p1V,pAb)=='0':
return "坏瓜"
else:
return "好瓜"
if __name__ == '__main__':
testEntry1 = ['青绿', '蜷缩', '浊响', '清晰', '凹陷', '硬滑']
print(testingNB(testEntry1))