欢迎使用CSDN-markdown编辑器
来源:互联网 发布:苹果手机电池查询软件 编辑:程序博客网 时间:2024/06/15 01:36
MachineLearning-KNN算法
def createDataSet(): group = array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]]) labels = ['A','B','C','D'] return group,labelsdef classify0(inX,dataSet,labels,k): dataSetSize = dataSet.shape[0] diffMat = tile(inX,(dataSetSize,1))-dataSet sqDiffMat = diffMat**2 sqDistance = sqDiffMat.sum(axis=1) distance = sqDistance**0.5 sortedDistances = distance.argsort() classCount = {} for i in range(k): voteILabel = labels[sortedDistances[i]] classCount[voteILabel] = classCount.get(voteILabel,0)+1 sortedClassCount = sorted(classCount.iteritems(),key=operator.itemgetter(1),reverse=True) print sortedClassCountreturn sortedClassCount[0][0]
tile(A,reps):Construct an array by repeating A the number of times given by reps详情参考numpy.tile
sqDiffMat.sum(axis=1):逐项各自求和参考numpy.sum
#distance.argsort():Array values from small to large index values:按从小到大顺序排序后取排序前索引值,参考numpy.argsort
sortedClassCount = sorted(classCount.iteritems(),key=operator.itemgetter(1),reverse=True):按照classCount中第二个元素排序,reverse=true表示升序。参考sort排序
阅读全文
0 0
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- unity打包出现Unable to list target platforms.Please make sure the android sdk path is correct.
- HDU 4495 Rectangle(前缀hash)
- whl 源
- JMX创建与连接(一)
- 【jquery】图灵机器人接口
- 欢迎使用CSDN-markdown编辑器
- 素数筛【模板】
- windows安装yii2框架步骤
- shell中IF条件判断
- Python爬虫之爬取——解析robots.txt文件
- Java Web学习总结(37)——Filter和Listener详解
- rtmp协议分析
- aes解密
- Toolbar Kotlin 简单使用