import numpy as np def CreateDateSet(): group = np.array([[1.0, 2.0], [1.2, 0.1], [0.1, 1.4], [0.3, 3.5]]) labels = ['A','A','B','B'] return group,labelscode
from numpy import * import operator排序
##給出訓練數據以及對應的類別 def createDataSet(): group = array([[1.0, 2.0], [1.2, 0.1], [0.1, 1.4], [0.3, 3.5],[0.2,1.5]]) labels = ['A', 'A', 'B', 'B','B'] return group, labelsutf-8
###經過KNN進行分類 def classify(input, dataSet, label, k): dataSize = dataSet.shape[0] ####計算歐式距離 diff = tile(input, (dataSize, 1)) - dataSet sqdiff = diff ** 2get
squareDist = sum(sqdiff, axis=1) ###行向量分別相加,從而獲得新的一個行向量 dist = squareDist ** 0.5 ##對距離進行排序 sortedDistIndex = argsort(dist) ##argsort()根據元素的值從大到小對元素進行排序,返回下標 print(sortedDistIndex ) classCount = {} for i in range(k): voteLabel = label[sortedDistIndex[i]] ###對選取的K個樣本所屬的類別個數進行統計 classCount[voteLabel] = classCount.get(voteLabel, 0) + 1 print(classCount) ###選取出現的類別次數最多的類別 maxCount = 0 for key, value in classCount.items(): if value > maxCount: maxCount = value classes = key return classes
def main(): input = array([1.1, 0.3]) dataSet, labels = createDataSet() K = 3 classify(input,dataSet,labels,K)input
main()it