注释写得很清楚了,熟悉了一下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. 跨平台base64数据传输注意问题

    今天用base64编码传输json串,android端那边始终看不到图片! 首先发现android端接收的json串长度不一致,仔细研究发现android端接收到的json数据里把服务器数据里的&qu ...

  2. Springmvc jar包介绍

    spring.jar是包含有完整发布的单个jar 包,spring.jar中包含除了spring-mock.jar里所包含的内容外其它所有jar包的内容,因为只有在开发环境下才会用到 spring-m ...

  3. JavaScript之数据类型讲解

    JavaScript中有5种简单数据类型(也称为基本数据类型):Undefined.Null.Boolean.Number和String.还有1种复杂数据类型——Object,Object本质上是由一 ...

  4. spring mvc Controller与jquery Form表单提交代码demo

    1.JSP表单 <% String basePath = request.getScheme() + "://" + request.getServerName() +&qu ...

  5. Sqli-labs less 45

    Less-45 同样的,45关与43关的payload是一样的,只不过45关依旧没有报错信息. 登录 username:admin Password:c');create table less45 l ...

  6. Javascript offsetLeft详情

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

  7. Mac Air maven 环境配置

    mave 的配置 检出项目遇到问题: Could not calculate build plan: Failure to transfer org.apache.maven.plugins:mave ...

  8. Android开发者应该深入学习的10个开源应用项目

    Android 开发带来新一轮热潮让很多移动开发者都投入到这个浪潮中去了,创造了许许多多相当优秀的应用.其中也有许许多多的开发者提供了应用开源项目,贡献出他们的 智慧和创造力.学习开源代码是掌握技术的 ...

  9. ASP 中调用函数关于Call使用注意的问题

    Function TestFun(Tstr) TStr = "Fun2" End Function Sub TestSub(TStr) Tstr = "Sub2" ...

  10. linux下top命令查看cpu占用情况

    可以通过 top 命令来查看 CPU 使用状况.运行 top 命令后,CPU 使用状态会以全屏的方式显示,并且会处在对话的模式 -- 用基于 top 的命令,可以控制显示方式等等.退出 top 的命令 ...