欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【机器学习】【KNN】线性扫描算法,python实现识别手写数字的系统

程序员文章站 2022-07-14 20:03:07
...

1.样本集资源

1.1样本集所在目录情况

【机器学习】【KNN】线性扫描算法,python实现识别手写数字的系统

1.2训练样本集所在目录情况

训练样本集中每个文件命名规则介绍:

x_y.txt

x是文件中手写数字的真实数值

y是第y个手写数字值为x的样本集文件

【机器学习】【KNN】线性扫描算法,python实现识别手写数字的系统

1.3测试样本集所在目录情况

测试样本集中每个文件命名规则介绍:

x_y.txt

x是文件中手写数字的真实数值

y是第y个手写数字值为x的样本集文件

【机器学习】【KNN】线性扫描算法,python实现识别手写数字的系统

1.4样本集下载链接

https://download.csdn.net/download/u012421852/10316218

2.KNN算法识别手写数字的系统实现代码

'''
@author: 蔚蓝的天空Tom
Talk is cheap, show me the code
Aim:KNN(k Nearest Neighbors)算法实现手写数字的识别
'''
import numpy as np
import math
#import operator
from os import listdir


train_set_dir = 'D:\\tom\\data\\digits\\trainingDigits\\'
test_set_dir =  'D:\\tom\\data\\digits\\testDigits\\'


#开根号函数的向量化
def sqrt_func(x):
    func = np.frompyfunc(math.sqrt, 1, 1)
    return func(x)


#计算欧氏距离
def euclidean_distance(test_data, train_data):
    #训练数据总数
    count = train_data.shape[0]
    #计算[x1-x2, y1-y2]
    diff_a = np.tile(test_data, (count, 1)) - train_data
    #计算[(x1-x2)^2, (y1-y2)^2]
    square_dist_a = np.sum(np.square(diff_a), axis=1)
    #计算sqrt((x1-x2)^2, (y1-y2)^2)
    dist_a = sqrt_func(square_dist_a)
    return dist_a


#KNN classify
def knn_classify(test_data, train_data, train_label, k=3):  
    #计算欧氏距离
    dist = euclidean_distance(test_data, train_data)
    
    #依次返回数值从小到大元素的下标
    #如果dist:100,400,300,200,600,500
    #那么index:0,3,2,1,5,4
    index = np.argsort(dist)
    
    #用字典统计每个标签的投票总数
    #{'A':0, 'B':3}表示标签A投票0次,标签B投票3次
    label_cnt_dict={}
    for i in range(k):
        #获取第index[i]个样本所在的标签voteLabel
        voteLabel = train_label[index[i]]
        #对voteLabel标签的投票数统计加1
        label_cnt_dict[voteLabel] = label_cnt_dict.get(voteLabel,0) + 1
    
    #找到投票次数最多的标签
    max_cnt = 0
    for key,value in label_cnt_dict.items():
        if value > max_cnt:
            max_cnt = value
            winner = key


    #胜利者标签就是测试数据被预测的分类
    return winner


'''
手写数字二值化后处理后的0_1.txt文件内容格式如下所示:
00000000000001100000000000000000
00000000000011111100000000000000
00000000000111111111000000000000
00000000011111111111000000000000
00000001111111111111100000000000
00000000111111100011110000000000
00000001111110000001110000000000
00000001111110000001110000000000
00000011111100000001110000000000
00000011111100000001111000000000
00000011111100000000011100000000
00000011111100000000011100000000
00000011111000000000001110000000
00000011111000000000001110000000
00000001111100000000000111000000
00000001111100000000000111000000
00000001111100000000000111000000
00000011111000000000000111000000
00000011111000000000000111000000
00000000111100000000000011100000
00000000111100000000000111100000
00000000111100000000000111100000
00000000111100000000001111100000
00000000011110000000000111110000
00000000011111000000001111100000
00000000011111000000011111100000
00000000011111000000111111000000
00000000011111100011111111000000
00000000000111111111111110000000
00000000000111111111111100000000
00000000000011111111110000000000
00000000000000111110000000000000
'''
def imgTxt_2_array(file_name):
    ret_img_a = np.zeros((1,1024)) #32 * 32 = 1024
    f = open(file_name)
    line_list = f.readlines()
    line_cnt = len(line_list)
    cnt = 0
    for i in range(line_cnt):
        line_str = line_list[i]
        #len()函数把'\n'也计算在内,真是字符总数要减去1
        line_str_cnt = len(line_str) - 1 
        for j in range(line_str_cnt):
            ret_img_a[0, cnt+j] = int(line_str[j])
        cnt += line_str_cnt
    return ret_img_a


#获取0_1.txt中手写数字的真实数字0(根据文件名结构获取)
def get_real_num(file_name_str = '0_1.txt'):
    #以为分隔符,得到子字符串
    str_list = file_name_str.split('.') #['0_1', 'txt']
    #取出包含num的子字符串'0_1'
    name_str = str_list[0]
    #以_为分隔符,得到子字符串
    int_list = name_str.split('_') #['0', '1']
    #取出第一个数字字符串,就是真实的手写数字
    real_num_str = int_list[0]
    return int(real_num_str)


def knn_classify_handwrite_number():
    #训练样本集所有文件名
    train_file_list = listdir(train_set_dir)#['0_1.txt', '0_2.txt',……]
    #训练样本集文件总数
    train_file_cnt = len(train_file_list)
    #训练样本集初始化
    train_file_a = np.zeros((train_file_cnt,1024))
    #训练标签初始化
    train_labels = []
    #获取训练样本集和训练标签
    for i in range(train_file_cnt):
        file_name_str = train_file_list[i] #'0_1.txt'
        train_labels.append(get_real_num(file_name_str))
        train_file_a[i,:] = imgTxt_2_array(train_set_dir+file_name_str)
        
    #测试样本集所有文件名
    test_file_list = listdir(test_set_dir)#['0_1.txt', '0_2.txt',……]
    #测试样本集文件总睡
    test_file_cnt = len(test_file_list)
    #测试分类错误总数
    err_cnt = 0.0
    #逐个对测试样本集文件进行数字识别
    for i in range(test_file_cnt):
        file_name_str = test_file_list[i]#'0_1.txt'
        test_img_a = imgTxt_2_array(test_set_dir+file_name_str)
        test_result = knn_classify(test_img_a, train_file_a, train_labels, 3)
        if (test_result != get_real_num(file_name_str)):
            err_cnt += 1.0
        print("KNN result: %d, real answer: %d" % (test_result, get_real_num(file_name_str)))
    #错误率
    err_rate = err_cnt / float(test_file_cnt)
    #正确率
    correct_rate = 1 - err_rate
    return err_cnt, err_rate, correct_rate
    
if __name__=='__main__':
    err_cnt, err_rate, correct_rate = knn_classify_handwrite_number()
    print("\nKNN error count  is: %d" % err_cnt)
    print("\nKNN error rate   is: %f" % err_rate)
    print('\nKNN correct rate is: %f' % correct_rate)
    

3.识别结果

3.1中间变量结果

【机器学习】【KNN】线性扫描算法,python实现识别手写数字的系统

3.2运行日志结果

KNN result: 4, real answer: 4
KNN result: 4, real answer: 4
KNN result: 4, real answer: 4
KNN result: 4, real answer: 4
KNN result: 4, real answer: 4
KNN result: 4, real answer: 4
KNN result: 4, real answer: 4
KNN result: 4, real answer: 4
KNN result: 4, real answer: 4
KNN result: 4, real answer: 4
KNN result: 4, real answer: 4
KNN result: 4, real answer: 4
KNN result: 4, real answer: 4
KNN result: 4, real answer: 4
KNN result: 4, real answer: 4
KNN result: 4, real answer: 4
KNN result: 4, real answer: 4
KNN result: 4, real answer: 4
KNN result: 4, real answer: 4
KNN result: 4, real answer: 4
KNN result: 4, real answer: 4
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 3, real answer: 5
KNN result: 6, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 5, real answer: 5
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 6, real answer: 6
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 7, real answer: 7
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 6, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 3, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 1, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 1, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 8, real answer: 8
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 1, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 7, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9
KNN result: 9, real answer: 9

KNN error count  is: 11

KNN error rate   is: 0.011628

KNN correct rate is: 0.988372

4.参考文档

1.【机器学习】k-近邻算法(kNN) 学习笔记:https://blog.csdn.net/fuxuemingzhu/article/details/68982043

2.机器学习(一)——K-近邻(KNN)算法http://www.cnblogs.com/ybjourney/p/4702562.html

enjoy it~

(end)