注释写得很清楚了,熟悉了一下python的一些基本语法和numpy中的一些操作。

 from numpy import *
import operator def createDataSet():
# generate the samples and labels.
group = array([[1.0,1.1], [1.0,1.0], [0,0], [0,0.1]])
labels = ['A', 'A', 'B', 'B']
print group
return group, labels def classify(inX, dataSet, labels, k):
dataSetSize = dataSet.shape[0] # get the size of one dimension.
                            # calculate the distance between inX and samples.
diffMat = tile(inX, (dataSetSize, 1)) - dataSet # repeat inX to generate a dataSetSize * 1 matrix. Then subtract the corresponding number in dataSet.
sqDiffMat = diffMat ** 2 # get the square of each D-value.
sqDistances = sqDiffMat.sum(axis=1) # get the sum of each pair of numbers.
distances = sqDistances ** 0.5 # get the square root of each sum. Those are distances between inX and samples. sortedDistIndicies = distances.argsort() # return the index if 'distances' is sorted.
classCount = {} # make a directory {label:display times}.
for i in range(k): # get first kth nearest samples.
voteIlabel = labels[sortedDistIndicies[i]] # get the ith's label.
classCount[voteIlabel] = classCount.get(voteIlabel, 0) + 1 # count the number of this label.
sortedClassCount = sorted(classCount.iteritems(), # get the most frequent label.
key=operator.itemgetter(1), reverse=True)
return sortedClassCount[0][0] # return the most frequent label. dataSet, labels = createDataSet()
print classify([-100.0,-100.1], dataSet, labels, 1)

python实现简单kNN的更多相关文章

  1. 教你用Python实现简单监督学习算法

    教你用Python实现简单监督学习算法 监督学习作为运用最广泛的机器学习方法,一直以来都是从数据挖掘信息的重要手段.即便是在无监督学习兴起的近日,监督学习也依旧是入门机器学习的钥匙. 这篇监督学习教程 ...

  2. Python爬虫简单实现CSDN博客文章标题列表

    Python爬虫简单实现CSDN博客文章标题列表 操作步骤: 分析接口,怎么获取数据? 模拟接口,尝试提取数据 封装接口函数,实现函数调用. 1.分析接口 打开Chrome浏览器,开启开发者工具(F1 ...

  3. Python 实现简单的 Web

    简单的学了下Python, 然后用Python实现简单的Web. 因为正在学习计算机网络,所以通过编程来加强自己对于Http协议和Web服务器的理解,也理解下如何实现Web服务请求.响应.错误处理以及 ...

  4. 用 python实现简单EXCEL数据统计

    任务: 用python时间简单的统计任务-统计男性和女性分别有多少人. 用到的物料:xlrd 它的作用-读取excel表数据 代码: import xlrd workbook = xlrd.open_ ...

  5. python开启简单webserver

    python开启简单webserver linux下面使用 python -m SimpleHTTPServer 8000 windows下面使用上面的命令会报错,Python.Exe: No Mod ...

  6. Python开发简单爬虫 - 慕课网

    课程链接:Python开发简单爬虫 环境搭建: Eclipse+PyDev配置搭建Python开发环境 Python入门基础教程 用Eclipse编写Python程序   课程目录 第1章 课程介绍 ...

  7. python使用简单http协议来传送文件

    python使用简单http协议来传送文件!在ubuntu环境下,局域网内可以使用nc来传送文件,也可以使用基于Http协议的方式来下载文件我们可以使用python -m SimpleHTTPServ ...

  8. Python超简单的HTTP服务器

    Python超简单的HTTP服务器 安装了python就可以 python -m SimpleHTTPServer 执行这一个命令即可实现一个HTTP服务器,将当前目录设为HTTP服务目录,可以通过h ...

  9. 教学项目之-通过Python实现简单的计算器

    教学项目之-通过Python实现简单的计算器   计算器开发需求 实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/ ...

随机推荐

  1. HDU 2196 求树上所有点能到达的最远距离

    其实我不是想做这道题的...只是今天考试考了一道类似的题...然后我挂了... 但是乱搞一下还是有80分....可惜没想到正解啊! 所以今天的考试题是: 巡访 (path.pas/c/cpp) Cha ...

  2. JAVA Hashmap不能用基本的数据类型

    今天开始学习Java... 转载:http://moto0421.iteye.com/blog/1143777 今天试了一下HahsMap, 采用如下形似定义 (这个下面是用了csdn的一位同仁的文章 ...

  3. matrix_last_acm_4

    2013 ACM-ICPC吉林通化全国邀请赛 A http://acm.hust.edu.cn/vjudge/contest/view.action?cid=97654#problem/A 题意:输入 ...

  4. razor GPU

    抓之前要把设置里面 Setting -Debug Setting -Graphic -PA DEBUG yes -RAZOR GPU yes replay是个很有用的功能,要设置 -Debug Set ...

  5. 使用feof()函数判断文件是否结束

    课本上时这样写的:(用putchar(ch);代表对取出来的字符的处理.) while(!feof(fp)) { ch=fgetc(fp); putchar(ch); } 但是,这样写的话,fgetc ...

  6. svn 分支与合并的使用

      在使用svn的时候我们往往有这样的需求.我们修改某些代码,因为对某项技术不是非常的熟悉,担心自己当前的修改(或者叫测试)会影响到服务器中版本库代码的崩溃等.传统做法我们会手动复制一份代码,然后修改 ...

  7. 一些实用的 jQuery 技巧

    jQuery如今已经成为Web开发中最流行的JavaScript库,通过jQuery和大量的插件,你可以轻松实现各种绚丽的效果. 本文将为你介绍一些实用的技巧,希望可以帮助你更加高效地使用jQuery ...

  8. uva 11294

    Problem E: Wedding Up to thirty couples will attend a wedding feast, at which they will be seated on ...

  9. JavaScript call和apply的用法

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  10. NameNode HA滚动升级方案

    Hadoop 滚动升级非常方便,只需要在配置中增加一些选项就可以通过Hadoop自身的代码进行完成. 步骤: 1.首先到需要升级的NameService的Active NameNode上面,比如我们1 ...