[MachineLearning]KNN
2024-08-27 11:40:52
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 18 11:46:15 2014 @author: hp
""" import numpy as np
import operator def createDataSet():
group=np.random.rand(4,2)
labels=['a','b','c','d']
return group,labels def classify0(inX,dataSet,labels,k):
dataSetSize=dataSet.shape[0]
diffMat=np.tile(inX,(dataSetSize,1))-dataSet
sqDiffMat=diffMat**2
sqDistances=sqDiffMat.sum(axis=1)
distances=sqDistances**0.5
sortedDistIndicies=distances.argsort()
classCount={}
for i in range(k):
voteLabel=labels[sortedDistIndicies[i]]
classCount[voteLabel]=classCount.get(voteLabel,0)+1
sortedClassCount=sorted(classCount.iteritems(),key=operator.itemgetter(1),reverse=True)
return sortedClassCount[0][0] group,labels=createDataSet()
result=classify0([0,1],group,labels,3)
print '对坐标0,0的分类为%s'%(result)
最新文章
- .NET面试题系列[7] - 委托与事件
- 相机标定:Matlab标定工具箱使用要点
- android 自定义alertdialog和取消dialog
- HDFS深入浅析
- eclipse启动问题
- Seek the Name, Seek the Fame - POJ 2752(next运用)
- Cannot instantiate the type List&;lt;Integer&;gt;
- 关于write()和fsync()
- python2.7 使用super关键词 报错 TypeError: must be type, not&;n
- 线性表的顺序存储设计和实现 - API函数实现
- cocos2dx-lua中handler解析
- ExtJS5入门
- H5图片预览、压缩、上传
- 基于Vue实现图片在指定区域内移动
- PHP TS 和 NTS 版本选择
- 利用 Create React Native App 快速创建 React Native 应用
- angularjs ng-app=";angular_app"; ng-controller=";angular_controller"; ng-init=";findAll()";
- mysql 处理日期格式
- WebApp之Meta标签总结
- Flash文件在asp页面无法播放,网页上面的Flash文件在火狐浏览器不播放
热门文章
- 我的Android进阶之旅------&;gt;Android字符串资源中的单引號问题error: Apostrophe not preceded by 的解决的方法
- Unity3D性能优化小tips——把this.transform缓存缓存起来
- IntelliJ IDEA15,PhpStorm10,WebStorm11激活破解
- OpenERP|ODOO高德地图应用
- [机器学习实战] 决策树ID3算法
- Linux常用指令总结
- 面试、笔试中常用的SQL语句(数据库知识必杀)一共50个!!!
- Spring Boot(三):logback打印日志
- 点滴积累【JS】---JS小功能(onmousemove鼠标移动坐标接龙DIV)
- Atitit .h5文件上传 v3