1. 程式人生 > >kNN分類演算法的Python實現

kNN分類演算法的Python實現

1.k-近鄰演算法實現

from numpy import *
import operator


def createDataSet():
    group = array([[1.0, 1.1], [2.0, 2.0], [0, 0], [4.1, 5.1]])
    labels = ['A', 'B', 'C', 'D']
    return group, labels


def classify0(inX, dataSet, labels, k):
    """

    :param inX: 用於分類的輸出向量
    :param dataSet:輸入的樣本集
    :param labels:標籤向量
    :param k:用於選擇最近鄰居的樹目
    :return:
    
""" dataSetsize = dataSet.shape[0] # 得到資料集的行數 diffMat = tile(inX, (dataSetsize, 1)) - dataSet # tile生成和訓練樣本對應的矩陣,並與訓練樣本求差 sqDiffMat = diffMat ** 2 sqDistances = sqDiffMat.sum(axis=1) # 將矩陣的每一行相加 distances = sqDistances ** 0.5 sortedDistIndicies = distances.argsort() # 從小到大排序 返回對應的索引位置
classCount = {} for i in range(k): voteIlabel = labels[sortedDistIndicies[i]] # 找到該樣本的型別 classCount[voteIlabel] = classCount.get(voteIlabel, 0) + 1 # 在字典中將該型別加一 sortedClassCount = sorted(classCount.items(), key=operator.itemgetter(1), reverse=True) # reverse = True代表降序
return sortedClassCount[0][0] # 排序並返回出現最多的那個型別

2.測試

import kNN


group,labels = kNN.createDataSet()
print(kNN.classify0([0,0],group,labels,3))
print(kNN.classify0([1,2],group,labels,3))
print(kNN.classify0([3,3],group,labels,3))
print(kNN.classify0([5,5],group,labels,3))

3.實驗結果

C
A
B
D

實驗環境:Ubuntu18.04+Pycharm+python3.6+numpy