注释写得很清楚了,熟悉了一下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. The 15th Zhejiang University Programming Contest

    a  ZOJ 3860 求和大家不一样的那个数,签到,map水之 #include<cstdio> #include<map> using namespace std; map ...

  2. depthstencil buffer 不支持 msaa

    phyreengine dx11 MRT不支持 depth rendertarget 的msaa 他里面竟然只写着,// not supported yet !!!! 导致hdao 时开msaa的话, ...

  3. WPF编程学习——样式

    本文目录 1.引言 2.怎样使用样式? 3.内联样式 4.已命名样式 5.元素类型样式 6.编程控制样式 7.触发器 1.引言 样式(Style),主要是用来让元素或内容呈现一定外观的属性.WPF中的 ...

  4. 如何实现Windows Phone代码与Unity相互通信(插件方式)

    原地址:http://www.cnblogs.com/petto/p/3915943.html 一些废话 原文地址: http://imwper.com/unity/petto/%E5%A6%82%E ...

  5. Ogre1.8.1编译时大量warning的问题

    本文的编译环境为Windows7_SP1 + VS2010_SP1 :) 当编译Ogre1.8.1的源码时,会出现大量的warning,如图: 虽然没有太大影响,但是程序员都希望自己的程序是没有war ...

  6. Static、final、abstract、接口、构造方法及java语法总结

    Static:定义类的时候一般不用static来修饰,在一定意义上,用static修饰的字段可以作为全局变量,static修饰的字段和方法存储在类的内存区域,所有实例共享.static字段和方法都是属 ...

  7. yum install 与 yum groupinstall 的区别

    原文:http://blog.51yip.com/linux/1171.html yum 提供二种安装软件的方式 1,yum install 它安装单个软件,以及这个软件的依赖关系 2,yum gro ...

  8. POJ 3982 序列(JAVA,简单,大数)

    题目 //在主类中 main 方法必须是 public static void 的,在 main 中调用非static类时会有警告信息, //可以先建立对象,然后通过对象调用方法: import ja ...

  9. LA 3350

    The NASA Space Center, Houston, is less than 200 miles from San Antonio, Texas (the site of the ACM ...

  10. Delphi的时间处理

    这几天因为自己要学习编写一个小程序中要用到一些时间处理.就在网上搜集一些教材学习到一般的应用,做个笔记,加深印象. 用上Delphi中相应的函数,Delphi的时间处理起来还是很容易的. Delphi ...