KNN算法

1、使用python标准库实现knn算法

示例代码如下(knn_test1.py):

#! /usr/bin/env python
#-*- coding:utf-8 -*-

import math

class KNN:
    def __init__(self,trainData,trainLabel,k):
        self.trainData = trainData
        self.trainLabel = trainLabel
        self.k = k

    def predict(self,inputPoint):
        retLable = "None"
        arr=[]
        for vector,lable in zip(self.trainData,self.trainLabel):
            s = 0
            for i,n in enumerate(vector) :
                s += (n-inputPoint[i]) ** 2
            arr.append([math.sqrt(s),lable])
        arr = sorted(arr,key=lambda x:x[0])[:self.k]
        dtmp = {}
        for k,v in arr :
            if not v in dtmp : dtmp[v]=0
            dtmp[v] += 1
        retLable,_ = sorted(dtmp.items(),key=lambda x:x[1],reverse=True)[0]
        return retLable

data = [
    [1.0, 1.1],
    [1.0, 1.0],
    [0.0, 0.0],
    [0.0, 0.1],
    [1.3, 1.1],
]

labels = ['A','A','B','B','A']
knn = KNN(data,labels,3)

print knn.predict([1.2, 1.1])
print knn.predict([0.2, 0.1])

运行效果如下:

$ python knn_test1.py
A
B

该代码的github地址: https://github.com/mike-zhang/pyExamples/blob/master/algorithm/dataMining_KNN/knn_test1.py

2、使用sklearn库提供knn算法

示例代码如下(knn_sklearn_test1.py):

#! /usr/bin/env python
#-*- coding:utf-8 -*-

from sklearn import neighbors
import numpy

data = [
    numpy.array([1.0, 1.1]),
    numpy.array([1.0, 1.0]),
    numpy.array([0.0, 0.0]),
    numpy.array([0.0, 0.1]),
    numpy.array([1.3, 1.1])
]
labels = ['A','A','B','B','A']

knn = neighbors.KNeighborsClassifier(n_neighbors=3)
knn.fit(data, labels)

print knn.predict(numpy.array([1.2, 1.1]).reshape(1,-1))
print knn.predict(numpy.array([0.2, 0.1]).reshape(1,-1))

运行效果如下:

$ python knn_sklearn_test1.py
['A']
['B']

该代码的github地址: https://github.com/mike-zhang/pyExamples/blob/master/dataStructure/sets_examples/setsTest1.py