python中knn函数 python ncnn( 三 )


对于线性模型来讲 , 标准化更加好,一是符合线性模型对权重的处理,二是保留了异常值的信息
———————————————————————————————————
上篇文章对于此类问题的处理见 datingClassTest 函数
K-近邻算法被称之为 惰性算法,和其python中knn函数他机器学习算法不一样 , 因为他仅仅是对训练数据集有记忆功能,而不是从训练集中通过学习得到一个判别函数,即不需要训练,看过上篇文章的小伙伴应该会有体会 。缺点是计算复杂度会随着样本数量的增长而呈线性增长 , 除非数据集中特征数量有限
新手学习PYTHON中KNN算法的手写识别出现问题 求助参考了其他博主的代码 想试着运行 然后去理解 。结果一直报错,希望大神帮帮忙 。
import numpy as np
import os
import kNN
def img2vector(filename):
"""函数将以文本格式出现的32*32的0-1图片,转变成一维特征数组 , 返回一维数组
Keyword argument:
filename -- 文本格式的图片文件
"""
imgvect = np.zeros((1, 1024))
fr = open(filename)
for i in range(32):
linestr = fr.readline()
for j in range(32):
imgvect[0, 32*i + j] = int(linestr[j])
return imgvect
def handwriteClassfiy(testfile, trainfile, k):
"""函数将trainfile中的文本图片转换成样本特征集和样本类型集,用testfile中的测试样本测试,无返回值
Keyword argument:
testfile -- 测试图片目录
trainfile -- 样本图片目录
"""
trainFileList = os.listdir(trainfile)
trainFileSize = len(trainFileList)
labels = []
trainDataSet = np.zeros((trainFileSize, 1024))
for i in range(trainFileSize):
filenameStr = trainFileList[i]
digitnameStr = filenameStr.split('.')[0]
digitLabels = digitnameStr.split('_')[0]
labels.append(digitLabels)
trainDataSet[i, :] = img2vector(trainfile + '/' + filenameStr)
testFileList = os.listdir(testfile)
testNumber = len(testFileList)
errorcount = 0.0
for testname in testFileList:
testdigit = img2vector(testfile + '/' + testname)
classifyresult = kNN.classfiy(testdigit, trainDataSet, labels, k)
testStr = testname.split('.')[0]
testDigitLabel = testStr.split('_')[0]
if classifyresult != testDigitLabel:
errorcount += 1.0
#print('this test real digit is:%s, and the result is: %s' % (testDigitLabel, classifyresult))
print('k = %d, errorRatio is: %f' % (k, errorcount/float(testNumber)))
return
if __name__ == '__main__':
filename = 'C:/Users/lx/Desktop/MachineLearning-master/kNN/use Python and NumPy/testDigits/0_1.txt'
traindir= 'C:/Users/lx/Desktop/MachineLearning-master/kNN/use Python and NumPy/trainingDigits'
testdir = 'C:/Users/lx/Desktop/MachineLearning-master/kNN/use Python and NumPy/testDigits'
handwriteClassfiy(testdir, traindir, 3)
错误提示Traceback (most recent call last):
File "kNN.py", line 56, in module
handwriteClassfiy(testdir, traindir, 3)
File "kNN.py", line 43, in handwriteClassfiy
classifyresult = kNN.classfiy(testdigit, trainDataSet, labels, k)
AttributeError: module 'kNN' has no attribute 'classfiy'
你这个文件是不是就叫 kNN.py ?如果是的话那你这个里面根本就没有 classfiy 这个属性 , 当然会报错 。
另外 , import kNN 是 import 自己?
python 提示res is not defined应该是哪里格式不对,它提示的不一定是错误所在地方 。
注释函数knn,运行函数distance查错
没有问题就接着运行下面的 , 逐一调试
PyOD主要算法(KNN、IForest 和 MCD)的原理及使用 Python Outlier Detection(PyOD)是当下最流行的Python异常检测工具库(toolkit) 。该工具库的主要亮点包括python中knn函数:

推荐阅读