KNN - 基于numpy实现程序

本文之编写程序涉及到API介绍,程序的完整实现,具体算法原理请查看之前所写的KNN算法介绍

一、基础准备

1、python基础

** sorted: **
python内置的全局排序方法,它返回一个新的list,新的list的元素基于小于运算符(lt)来排序。

print(sorted([5, 2, 3, 1, 4]))
[1, 2, 3, 4, 5]

print(sorted("This is a test string from Andrew".split(), key=str.lower))
['a', 'Andrew', 'from', 'is', 'string', 'test', 'This']

Sorting basic:

>>> print sorted([5, 2, 3, 1, 4])
[1, 2, 3, 4, 5]

>>> L = [5, 2, 3, 1, 4]
>>> L.sort()
>>> print L
[1, 2, 3, 4, 5]

Sorting  cmp:
>>>L = [('b',2),('a',1),('c',3),('d',4)]
>>>print sorted(L, cmp=lambda x,y:cmp(x[1],y[1]))
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

Sorting  keys:
>>>L = [('b',2),('a',1),('c',3),('d',4)]
>>>print sorted(L, key=lambda x:x[1]))
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
Sorting  reverse:

>>> print sorted([5, 2, 3, 1, 4], reverse=True)
[5, 4, 3, 2, 1]

>>> print sorted([5, 2, 3, 1, 4], reverse=False)
[1, 2, 3, 4, 5]

>>> L = [('d',2),('a',4),('b',3),('c',2)]
>>> print sorted(L, key=lambda x:(x[1],x[0]))
>>>[('c', 2), ('d', 2), ('b', 3), ('a', 4)]

** Counter.most_common**
返回一个TopN列表。如果n没有被指定,则返回所有元素。当多个元素计数值相同时,排列是无确定顺序的。

data = [1,2,2,3,3,2,1,1,2,]
print(Counter(data).most_common(1))
# >> [(2, 4)]
print(Counter(data).most_common(2))
# >> [(2, 4), (1, 3)]
print(Counter(data).most_common(1)[0][0])
# >> 2
print(Counter(data).most_common(1)[0][1])
# >> 4

2、numpy基础

** np.shape**
shape函数是numpy.core.fromnumeric中的函数,它的功能是读取矩阵的长度

data = [[1,2,2],[3,3,2]]
print(np.shape(data)[0])
#>> 2
print(np.shape(data)[1])
#>> 3

** np.zeros**
用法:zeros(shape, dtype=float, order='C')
返回:返回来一个给定形状和类型的用0填充的数组;
参数:shape:形状
dtype:数据类型,可选参数,默认numpy.float64
order:可选参数,c代表与C语言类似,行优先;F代表列优先

print(np.zeros(5))
# >> [ 0.  0.  0.  0.  0.]
print(np.zeros((5), dtype=bool))
# >> [False False False False False]
print(np.zeros([3,2]))
# >> [[ 0.  0.]
 [ 0.  0.]
 [ 0.  0.]]

** np.tile**
tile函数位于python模块 numpy.lib.shape_base中,他的功能是重复某个数组。比如tile(A,n),功能是将数组A重复n次,

data=[[1,2],[3,4]]
print(np.tile(data,[2]))
# >>[[1 2 1 2]
 [3 4 3 4]]
print(np.tile(data,[2,2]))
# >>[[1 2 1 2]
 [3 4 3 4]
 [1 2 1 2]
 [3 4 3 4]]

** np.linalg.norm**


1500724082(1).png
>>> x = np.array([3, 4])
>>> np.linalg.norm(x)
5.
>>> np.linalg.norm(x, ord=2)
5.
>>> np.linalg.norm(x, ord=1)
7.
>>> np.linalg.norm(x, ord=np.inf)
4

二、完整程序

# -*- coding: utf-8 -*-
import math
import numpy as np
from collections import Counter
import warnings

## 经典测试
# 流程
#   1、对数据训练数据进行归一化,(防止某些参数值过大影响距离计算)
#   2、按个取出测试数据(归一化),计算该个数据到所有训练数据的欧几里得距离
#   3、对该个数据到各点的数据距离进行排序
#   4、过滤出排名前几名的点,列出各点的归类
#   5、计算最大归类就该分类了。


# k-Nearest Neighbor算法
def k_nearest_neighbors(data, predict, classLabel,  k=5):
    if len(data) >= k:
        warnings.warn("k is too small")
    distances = []
    dataSize = data.shape[0]
    # KNN的算法核心就是欧式距离的计算

    # 第一种方案:
    diffMat = np.tile(predict, (dataSize, 1)) - data
    sqDiffMat = diffMat ** 2
    euclidean_distance = sqDiffMat.sum(axis=1) ** 0.5
    for i in range(dataSize):
        # 将距离加上结果
        distances.append([euclidean_distance[i], classLabel[i]])

    # 第二种方案:
    # for i in range(dataSize):
    #     features = data[i]
    #     # 计算predict点到各点的距离
    #     euclidean_distance = np.linalg.norm(np.array(features)-np.array(predict))
    #     # 将距离加上结果
    #     distances.append([euclidean_distance, classLabel[i]])
    
    # 根据距离排序,并返回分类结果
    sorted_distances =[i[1]  for i in sorted(distances)]
    # 取前K个值的数据
    top_nearest = sorted_distances[:k]
    # 返回最多分类
    group_res = Counter(top_nearest).most_common(1)[0][0]

    return group_res

def file2Mat(testFileName, parammterNumber):
    fr = open(testFileName)
    lines = fr.readlines()
    lineNums = len(lines)
    resultMat = np.zeros((lineNums, parammterNumber))
    classLabelVector = []
    for i in range(lineNums):
        line = lines[i].strip()
        itemMat = line.split('\t')
        resultMat[i, :] = itemMat[0:parammterNumber]
        classLabelVector.append(itemMat[-1])
    fr.close()
    return resultMat, classLabelVector;

# 为了防止某个属性对结果产生很大的影响,所以有了这个优化,比如:10000,4.5,6.8 10000就对结果基本起了决定作用
def autoNorm(dataSet):
    minVals = dataSet.min(0)
    maxVals = dataSet.max(0)
    ranges = maxVals - minVals
    normMat = np.zeros(np.shape(dataSet))
    size = normMat.shape[0]
    normMat = dataSet - np.tile(minVals, (size, 1))
    normMat = normMat / np.tile(ranges, (size, 1))
    return normMat, minVals, ranges

if __name__=='__main__':

    trainigSetFileName=  'data\\datingTrainingSet.txt'
    testFileName = 'data\\datingTestSet.txt'

    # 读取训练数据
    trianingMat, classLabel = file2Mat(trainigSetFileName, 3)
    # 都数据进行归一化的处理
    trianingMat, minVals, ranges = autoNorm(trianingMat)
    # 读取测试数据
    testMat, testLabel = file2Mat(testFileName, 3)

    correct = 0
    total = 0
    testSize = testMat.shape[0]
    print(testSize)
    print(testMat.shape[1])
    testSize = testMat.shape[0]
    for i in range(testSize):
        predict = testMat[i]
        # 你可以调整这个k看看准确率的变化,你也可以使用matplotlib画出k对应的准确率,找到最好的k值
        res = k_nearest_neighbors(trianingMat, (predict - minVals) / ranges, classLabel, k = 5)
        if testLabel[i] == res:
            correct += 1
        total += 1

    print(correct)  # 准确率
    print(correct/(testSize*1.0))  # 准确率

    print(k_nearest_neighbors(trianingMat, [4,2,1], classLabel, k = 5)) # 预测一条记录

三、测试数据 `

推荐阅读更多精彩内容