Machine learning in action ch02 KNN笔记
来源:互联网 发布:服务器添加域名 编辑:程序博客网 时间:2024/05/22 04:30
1.KNN算法特点描述
优点:精度高、对异常值不敏感、无数据输入假定。
缺点:计算复杂度高、空间复杂度高。
适用数据范围:数值型和标称型。
2.KNN思想
对于一个给定的数据集,对于未知数据分类时,总是能够通过计算该点与其他数据点的距离(这个距离可以是欧式距离,也可以是闵氏距离,不加以限定),通过寻找该点的最邻近的K个数据点中的数量最多的数据点,来确定该数据点的分类。
实际上这是一种朴素贝叶斯的思想。
3.创建数据集
3.1 在书中给出的KNN文件中:
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
3.2 构造第一个分类器
#第一个kNN分类器 inX-测试数据 dataSet-样本数据 labels-标签 k-邻近的k个样本 def classify0(inX,dataSet, labels, k): #计算距离 dataSetSize = dataSet.shape[0] #获取数据的维度 diffMat = tile(inX, (dataSetSize,1))- dataSet #计算差值 sqDiffMat = diffMat ** 2 #计算出绝对距离的平方 sqDistances = sqDiffMat.sum(axis = 1) #将这个距离相加 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 #排序 sortedClassCount = sorted(classCount.iteritems(), key = operator.itemgetter(1),reverse = True) return sortedClassCount[0][0]
阅读全文
0 0
- Machine learning in action ch02 KNN笔记
- MACHINE LEARNING IN ACTION KNN
- Machine Learning In Action:KNN(Python)
- 【Machine Learning in Action】Chap1|Classification|kNN
- Machine Learning in Action 学习笔记-(2)kNN k近邻算法
- Machine Learning In Action -- kNN的python实现
- Machine Learning In Action -- kNN (k Nearest Neighbors)
- Machine Learning in action –kNN(已勘误)
- machine learning in action
- Machine Learning in Action
- Machine Learning In Action
- Machine Learning In Action
- Machine Learning In Action
- Machine Learning In Action
- Machine Learning In Action
- Machine Learning In Action
- Machine Learning In Action
- Machine Learning In Action
- JSP的基本原理
- Mysql总结2
- 合并两个递增链表为一个递增链表
- linux常用命令
- <13>—— Roman to Integer
- Machine learning in action ch02 KNN笔记
- maven 引用自己的工程项目
- 算法详情【插入排序】
- 420小时学习代码之后-如何教你免费自学Python
- 简单爬虫-爬取百度百科1000个页面
- Shell脚本之8种字符串截取方法
- 使用Myeclipse创建Maven项目
- BZOJ 3629: [JLOI2014]聪明的燕姿
- java 位运算符详解 及 实例 ---与(&),或(|),非(~),异或(^)