python3 环境下import KNN 后报错
Traceback (most recent call last):
File “”, line 1, in
ModuleNotFoundError: No module named ‘KNN’
解决:没有保存成.py 文件
kNN.py文件如下:
from numpy import*
import operator
def createDataSet():
group=array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]])
labels=['A','A','B','B']
return group, labels
def classify0(inX,dataSet,labels,k):
//计算距离
dataSetSize=dataSet.shape[0]
diffMat=tile(inX,(dataSetSize,1))-dataSet
sqDiffMat=diffMat**2
sqDistances=sqDiffMat.sum(axis=1)//对每行求和 axis=0 对列求和
distances=sqDistances**0.5
sortedDistIndicies=distances.argsort()
classCount={}
//选择距离最小的k个点
for i in range(k):
voteIlabel=labels[sortedDistIndicies[i]]
classCount[voteIlabel]=classCount.get(voteIlabel,0)+1
//排序
sortedClas