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)) # 预测一条记录

三、测试数据 `

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,277评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,689评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,624评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,356评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,402评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,292评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,135评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,992评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,429评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,636评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,785评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,492评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,092评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,723评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,858评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,891评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,713评论 2 354

推荐阅读更多精彩内容