-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
25 lines (24 loc) · 88.6 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!DOCTYPE html><html lang="zh-Hans"><head><!--[if lt IE 9]>
<style>body {display: none; background: none !important} </style>
<meta http-equiv="Refresh" Content="0; url=//outdatedbrowser.com/" />
<![endif]--><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"><meta name="format-detection" content="telephone=no"><meta name="author" content="Porter Pan"><meta name="description" content="摘要 K近邻算法(K-nearest neighbor, k-NN)是一种基本的分类与回归的方法。 k近邻算法: 输入:训练数据集 T=(x1,y1),(x2,y2)...,(xN,yN)T={(x_{1},y_{1}), (x_{2},y_{2})..., (x_{N},y_{N})} T=(x1,y1),(x2,y2)...,(xN,yN) 其中 xiϵχ⊆Rnx_{i}\eps"><meta property="og:type" content="article"><meta property="og:title" content="K近邻算法"><meta property="og:url" content="https://blogs.porterpan.top/kjinglin/index.html"><meta property="og:site_name" content="Porter-聚水渊"><meta property="og:description" content="摘要 K近邻算法(K-nearest neighbor, k-NN)是一种基本的分类与回归的方法。 k近邻算法: 输入:训练数据集 T=(x1,y1),(x2,y2)...,(xN,yN)T={(x_{1},y_{1}), (x_{2},y_{2})..., (x_{N},y_{N})} T=(x1,y1),(x2,y2)...,(xN,yN) 其中 xiϵχ⊆Rnx_{i}\eps"><meta property="og:locale"><meta property="og:image" content="https://blogs.porterpan.top/image2/knn_1.jpg"><meta property="article:published_time" content="2019-01-04T14:55:03.000Z"><meta property="article:modified_time" content="2024-06-14T08:38:29.155Z"><meta property="article:author" content="Porter Pan"><meta property="article:tag" content="Deep Learning"><meta property="article:tag" content="Algorithm"><meta property="article:tag" content="KNN"><meta name="twitter:card" content="summary"><meta name="twitter:image" content="https://blogs.porterpan.top/image2/knn_1.jpg"><link rel="apple-touch-icon" href="/apple-touch-icon.png"><link rel="alternate" href="/atom.xml" title="Porter-聚水渊" type="application/atom+xml"><link rel="shortcut icon" href="/favicon.png"><link href="//cdn.bootcss.com/animate.css/3.5.1/animate.min.css" rel="stylesheet"><link href="//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.css" rel="stylesheet"><script src="//cdn.bootcss.com/pace/1.0.2/pace.min.js"></script><link href="//cdn.bootcss.com/pace/1.0.2/themes/blue/pace-theme-minimal.css" rel="stylesheet"><link rel="stylesheet" href="/css/style.css"><link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet"><title>K近邻算法 | Porter-聚水渊</title><script src="//cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script><script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script><script>var yiliaConfig={fancybox:!0,animate:!0,isHome:!1,isPost:!0,isArchive:!1,isTag:!1,isCategory:!1,fancybox_js:"//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.js",scrollreveal:"//cdn.bootcss.com/scrollReveal.js/3.1.4/scrollreveal.min.js",search:!0}</script><script>yiliaConfig.jquery_ui=[!1]</script><script>yiliaConfig.rootUrl="/"</script><script>var _hmt=_hmt||[];!function(){var e=document.createElement("script");e.src="//hm.baidu.com/hm.js?d71616702a6acb700ae00e15c0b9551b";var t=document.getElementsByTagName("script")[0];t.parentNode.insertBefore(e,t)}()</script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-math@4.0.0/dist/style.css"><meta name="generator" content="Hexo 6.3.0"></head><body><div id="container"><div class="left-col"><div class="overlay"></div><div class="intrude-less"><header id="header" class="inner"><a href="/" class="profilepic"><img src="/img/myportrait1.gif" class="animated zoomIn"></a><hgroup><h1 class="header-author"><a href="/"></a></h1></hgroup><form id="search-form"><input type="text" id="local-search-input" name="q" placeholder="search..." class="search form-control" autocomplete="off" autocorrect="off" searchonload="false"> <i class="fa fa-times" onclick="resetSearch()"></i></form><div id="local-search-result"></div><p class="no-result">No results found <i class="fa fa-spinner fa-pulse"></i></p><div id="switch-btn" class="switch-btn"><div class="icon"><div class="icon-ctn"><div class="icon-wrap icon-house" data-idx="0"><div class="birdhouse"></div><div class="birdhouse_holes"></div></div><div class="icon-wrap icon-ribbon hide" data-idx="1"><div class="ribbon"></div></div><div class="icon-wrap icon-link hide" data-idx="2"><div class="loopback_l"></div><div class="loopback_r"></div></div><div class="icon-wrap icon-me hide" data-idx="3"><div class="user"></div><div class="shoulder"></div></div></div></div><div class="tips-box hide"><div class="tips-arrow"></div><ul class="tips-inner"><li>菜单</li><li>标签</li><li>友情链接</li><li>关于我</li></ul></div></div><div id="switch-area" class="switch-area"><div class="switch-wrap"><section class="switch-part switch-part1"><nav class="header-menu"><ul><li><a href="/">主页</a></li><li><a href="/archives/">所有文章</a></li><li><a href="/diary/">随笔</a></li><li><a href="/tags/">标签云</a></li><li><a href="/./myResume/">关于我</a></li></ul></nav><nav class="header-nav"><ul class="social"><a class="fa Email" href="mailto:porterpan@163.com" title="Email"></a> <a class="fa GitHub" target="_blank" rel="noopener" href="https://github.com/porterpan" title="GitHub"></a> <a class="fa GitBook" target="_blank" rel="noopener" href="https://porter.gitbook.io" title="GitBook"></a> <a class="fa 博客园" target="_blank" rel="noopener" href="https://www.cnblogs.com/pertor/" title="博客园"></a> <a class="fa bilibili" target="_blank" rel="noopener" href="http://space.bilibili.com/240704272/#/" title="bilibili"></a> <a class="fa Twitter" target="_blank" rel="noopener" href="https://twitter.com/zf_Porter" title="Twitter"></a> <a class="fa 微信" href="/./about/contactme.html" title="微信"></a> <a class="fa WebNavigation" href="/./navs/index.html" title="WebNavigation"></a></ul></nav></section><section class="switch-part switch-part2"><div class="widget tagcloud" id="js-tagcloud"><ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Algorithm/" rel="tag">Algorithm</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Bayes/" rel="tag">Bayes</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Blogs/" rel="tag">Blogs</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/C/" rel="tag">C++</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/CMakeLists/" rel="tag">CMakeLists</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/C%E8%AF%AD%E8%A8%80/" rel="tag">C语言</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Deep-Learning/" rel="tag">Deep Learning</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Error/" rel="tag">Error</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/GUI/" rel="tag">GUI</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Gitbook/" rel="tag">Gitbook</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Github/" rel="tag">Github</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Gitment/" rel="tag">Gitment</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Hexo/" rel="tag">Hexo</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/KNN/" rel="tag">KNN</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/LRN/" rel="tag">LRN</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Latex/" rel="tag">Latex</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Library-Project/" rel="tag">Library Project</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Linux/" rel="tag">Linux</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/MNIST/" rel="tag">MNIST</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Machine-Learning/" rel="tag">Machine Learning</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Module/" rel="tag">Module</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Note/" rel="tag">Note</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/OpenCV/" rel="tag">OpenCV</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/PyTorch/" rel="tag">PyTorch</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Python/" rel="tag">Python</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Pythton/" rel="tag">Pythton</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Pytorch/" rel="tag">Pytorch</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Qt5/" rel="tag">Qt5</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/ROS/" rel="tag">ROS</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Socket/" rel="tag">Socket</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Socket5/" rel="tag">Socket5</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/System-Conf/" rel="tag">System Conf</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/TensorFlow/" rel="tag">TensorFlow</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Tensorflow/" rel="tag">Tensorflow</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Tool/" rel="tag">Tool</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/TxtToSpeech/" rel="tag">TxtToSpeech</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/future/" rel="tag">__future__</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/anaconda/" rel="tag">anaconda</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/argparse/" rel="tag">argparse</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/calibration/" rel="tag">calibration</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/css/" rel="tag">css</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/dl/" rel="tag">dl</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/docker/" rel="tag">docker</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/exam/" rel="tag">exam</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/file/" rel="tag">file</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/gcc/" rel="tag">gcc</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/git/" rel="tag">git</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/gitignore/" rel="tag">gitignore</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/git%E5%91%BD%E4%BB%A4/" rel="tag">git命令</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/goldendict/" rel="tag">goldendict</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/gym/" rel="tag">gym</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/gym-gazebo/" rel="tag">gym-gazebo</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/gym-gazabe/" rel="tag">gym_gazabe</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/hexo/" rel="tag">hexo</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/hexo-neat/" rel="tag">hexo-neat</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/java/" rel="tag">java</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/jupyter-notebook/" rel="tag">jupyter notebook</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/keras/" rel="tag">keras</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/kmean/" rel="tag">kmean</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/logistic-regression/" rel="tag">logistic regression</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/matplotlib/" rel="tag">matplotlib</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mysql/" rel="tag">mysql</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/numpy/" rel="tag">numpy</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/opencv/" rel="tag">opencv</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/peek/" rel="tag">peek</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/python-moudle/" rel="tag">python moudle</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/qt5/" rel="tag">qt5</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/recognition/" rel="tag">recognition</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/share/" rel="tag">share</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/sublime-test3/" rel="tag">sublime test3</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/sublime-text/" rel="tag">sublime-text</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/tensorflow/" rel="tag">tensorflow</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/url/" rel="tag">url</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/vision/" rel="tag">vision</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/vscode/" rel="tag">vscode</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/vsftpd/" rel="tag">vsftpd</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/writen/" rel="tag">writen</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E4%B8%8D%E8%83%BD%E4%B8%AD%E6%96%87%E8%BE%93%E5%85%A5/" rel="tag">不能中文输入</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%86%B3%E7%AD%96%E6%A0%91/" rel="tag">决策树</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%8A%A0%E8%BD%BD%E6%9C%AC%E5%9C%B0%E5%9B%BE%E7%89%87/" rel="tag">加载本地图片</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%8F%A4%E8%AF%97%E8%AF%8D%E8%AF%86%E5%88%AB/" rel="tag">古诗词识别</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%AE%89%E5%88%A9/" rel="tag">安利</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%B5%8C%E5%85%A5%E5%BC%8F/" rel="tag">嵌入式</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%B7%A5%E5%85%B7/" rel="tag">工具</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%80%BB%E7%BB%93/" rel="tag">总结</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%97%A5%E8%AE%B0/" rel="tag">日记</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%9C%80%E5%A4%A7%E4%BC%BC%E7%84%B6%E4%BC%B0%E8%AE%A1/" rel="tag">最大似然估计</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%9C%80%E5%A4%A7%E7%86%B5%E6%A8%A1%E5%9E%8B/" rel="tag">最大熵模型</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%9C%89%E9%81%93%E8%AF%8D%E5%85%B8/" rel="tag">有道词典</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%A2%AF%E5%BA%A6%E4%B8%8B%E9%99%8D%E5%92%8C%E6%9C%80%E5%B0%8F%E4%BA%8C%E4%B9%98%E6%B3%95/" rel="tag">梯度下降和最小二乘法</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C/" rel="tag">神经网络</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%AC%94%E8%AF%95/" rel="tag">笔试</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%AE%97%E6%B3%95%E5%B2%97/" rel="tag">算法岗</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%B3%BB%E7%BB%9F%E5%BC%95%E5%AF%BC/" rel="tag">系统引导</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%BA%BF%E6%80%A7%E5%9B%9E%E5%BD%92/" rel="tag">线性回归</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%BD%91%E7%AB%99/" rel="tag">网站</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%BD%91%E7%AB%99%E5%8E%8B%E7%BC%A9/" rel="tag">网站压缩</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%BD%91%E9%A1%B5%E5%B5%8C%E5%85%A5/" rel="tag">网页嵌入</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%8B%B1%E6%96%87%E7%BD%91%E5%9D%80/" rel="tag">英文网址</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%AF%84%E8%AE%BA%E6%8F%92%E4%BB%B6/" rel="tag">评论插件</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%BF%AD%E4%BB%A3%E7%AE%97%E6%B3%95/" rel="tag">迭代算法</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E9%9A%90%E9%A9%AC%E5%B0%94%E7%A7%91%E5%A4%ABHMM/" rel="tag">隐马尔科夫HMM</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E9%9F%B3%E8%A7%86%E9%A2%91%E5%B5%8C%E5%85%A5/" rel="tag">音视频嵌入</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E9%A9%AC%E5%B0%94%E7%A7%91%E5%A4%AB%E6%A8%A1%E5%9E%8B%E5%AE%9E%E6%88%98/" rel="tag">马尔科夫模型实战</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E9%AA%8C%E8%AF%81%E7%A0%81/" rel="tag">验证码</a></li></ul></div></section><section class="switch-part switch-part3"><div id="js-friends"><a class="main-nav-link switch-friends-link" target="_blank" rel="noopener" href="https://www.ncnynl.com/">创客制造</a> <a class="main-nav-link switch-friends-link" target="_blank" rel="noopener" href="http://www.paperweekly.site/rank">paper Weekly</a> <a class="main-nav-link switch-friends-link" target="_blank" rel="noopener" href="https://arxiv.org/">arXiv.org</a> <a class="main-nav-link switch-friends-link" target="_blank" rel="noopener" href="http://muchong.com/bbs/">小木虫</a> <a class="main-nav-link switch-friends-link" target="_blank" rel="noopener" href="https://redstonewill.com/">红色石头博客</a> <a class="main-nav-link switch-friends-link" target="_blank" rel="noopener" href="http://people.csail.mit.edu/leozhu/?from=singlemessage">Leo Zhu</a> <a class="main-nav-link switch-friends-link" target="_blank" rel="noopener" href="https://www.fast.ai/">fast.Ai</a> <a class="main-nav-link switch-friends-link" target="_blank" rel="noopener" href="http://image-net.org/">image-net-ilsvrc</a> <a class="main-nav-link switch-friends-link" target="_blank" rel="noopener" href="https://wx.zsxq.com/dweb2/index/group/48844552425158">知识星球</a> <a class="main-nav-link switch-friends-link" target="_blank" rel="noopener" href="https://medium.com/">medium</a> <a class="main-nav-link switch-friends-link" target="_blank" rel="noopener" href="https://robot.czxy.com/docs/">传智人工智能与机器人实验室</a></div></section><section class="switch-part switch-part4"><div id="js-aboutme">不要怂,干就是了,力量可能跟不上,气势上一定要跟上;专注于深度学习、机器学习、视觉图像处理、嵌入式软/硬件。</div></section></div></div></header></div></div><div class="mid-col"><nav id="mobile-nav"><div class="overlay"><div class="slider-trigger"></div><h1 class="header-author js-mobile-header hide"><a href="/" title="回到主页"></a></h1></div><div class="intrude-less"><header id="header" class="inner"><a href="/" class="profilepic"><img src="/img/myportrait1.gif" class="animated zoomIn"></a><hgroup><h1 class="header-author"><a href="/" title="回到主页"></a></h1></hgroup><nav class="header-menu"><ul><li><a href="/">主页</a></li><li><a href="/archives/">所有文章</a></li><li><a href="/diary/">随笔</a></li><li><a href="/tags/">标签云</a></li><li><a href="/./myResume/">关于我</a></li><div class="clearfix"></div></ul></nav><nav class="header-nav"><ul class="social"><a class="fa Email" target="_blank" href="mailto:porterpan@163.com" title="Email"></a> <a class="fa GitHub" target="_blank" href="https://github.com/porterpan" title="GitHub"></a> <a class="fa GitBook" target="_blank" href="https://porter.gitbook.io" title="GitBook"></a> <a class="fa 博客园" target="_blank" href="https://www.cnblogs.com/pertor/" title="博客园"></a> <a class="fa bilibili" target="_blank" href="http://space.bilibili.com/240704272/#/" title="bilibili"></a> <a class="fa Twitter" target="_blank" href="https://twitter.com/zf_Porter" title="Twitter"></a> <a class="fa 微信" target="_blank" href="/./about/contactme.html" title="微信"></a> <a class="fa WebNavigation" target="_blank" href="/./navs/index.html" title="WebNavigation"></a></ul></nav></header></div><link class="menu-list" tags="标签" friends="友情链接" about="关于我"></nav><div class="body-wrap"><article id="post-im2-深度学习基础-2-9-K近邻算法" class="article article-type-post" itemscope itemprop="blogPost"><div class="article-meta"><a href="/kjinglin/" class="article-date"><time datetime="2019-01-04T14:55:03.000Z" itemprop="datePublished">2019-01-04</time></a></div><div class="article-inner"><input type="hidden" class="isFancy"><header class="article-header"><h1 class="article-title" itemprop="name">K近邻算法</h1></header><div class="article-info article-info-post"><div class="article-category tagcloud"><a class="article-category-link" href="/categories/Deep-Learning/">Deep Learning</a></div><div class="article-tag tagcloud"><ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Algorithm/" rel="tag">Algorithm</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Deep-Learning/" rel="tag">Deep Learning</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/KNN/" rel="tag">KNN</a></li></ul></div><div class="clearfix"></div></div><div class="article-entry" itemprop="articleBody"><h2 id="摘要"><a class="markdownIt-Anchor" href="#摘要"></a> 摘要</h2><p>K近邻算法(K-nearest neighbor, k-NN)是一种基本的分类与回归的方法。</p><p>k近邻算法:</p><p>输入:训练数据集</p><p class="katex-block"><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>T</mi><mo>=</mo><mrow><mo stretchy="false">(</mo><msub><mi>x</mi><mn>1</mn></msub><mo separator="true">,</mo><msub><mi>y</mi><mn>1</mn></msub><mo stretchy="false">)</mo><mo separator="true">,</mo><mo stretchy="false">(</mo><msub><mi>x</mi><mn>2</mn></msub><mo separator="true">,</mo><msub><mi>y</mi><mn>2</mn></msub><mo stretchy="false">)</mo><mi mathvariant="normal">.</mi><mi mathvariant="normal">.</mi><mi mathvariant="normal">.</mi><mo separator="true">,</mo><mo stretchy="false">(</mo><msub><mi>x</mi><mi>N</mi></msub><mo separator="true">,</mo><msub><mi>y</mi><mi>N</mi></msub><mo stretchy="false">)</mo></mrow></mrow><annotation encoding="application/x-tex">T={(x_{1},y_{1}), (x_{2},y_{2})..., (x_{N},y_{N})}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:.68333em;vertical-align:0"></span><span class="mord mathnormal" style="margin-right:.13889em">T</span><span class="mspace" style="margin-right:.2777777777777778em"></span><span class="mrel">=</span><span class="mspace" style="margin-right:.2777777777777778em"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-.25em"></span><span class="mord"><span class="mopen">(</span><span class="mord"><span class="mord mathnormal">x</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:.30110799999999993em"><span style="top:-2.5500000000000003em;margin-left:0;margin-right:.05em"><span class="pstrut" style="height:2.7em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:.15em"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:.16666666666666666em"></span><span class="mord"><span class="mord mathnormal" style="margin-right:.03588em">y</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:.30110799999999993em"><span style="top:-2.5500000000000003em;margin-left:-.03588em;margin-right:.05em"><span class="pstrut" style="height:2.7em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:.15em"><span></span></span></span></span></span></span><span class="mclose">)</span><span class="mpunct">,</span><span class="mspace" style="margin-right:.16666666666666666em"></span><span class="mopen">(</span><span class="mord"><span class="mord mathnormal">x</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:.30110799999999993em"><span style="top:-2.5500000000000003em;margin-left:0;margin-right:.05em"><span class="pstrut" style="height:2.7em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:.15em"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:.16666666666666666em"></span><span class="mord"><span class="mord mathnormal" style="margin-right:.03588em">y</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:.30110799999999993em"><span style="top:-2.5500000000000003em;margin-left:-.03588em;margin-right:.05em"><span class="pstrut" style="height:2.7em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:.15em"><span></span></span></span></span></span></span><span class="mclose">)</span><span class="mord">.</span><span class="mord">.</span><span class="mord">.</span><span class="mpunct">,</span><span class="mspace" style="margin-right:.16666666666666666em"></span><span class="mopen">(</span><span class="mord"><span class="mord mathnormal">x</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:.32833099999999993em"><span style="top:-2.5500000000000003em;margin-left:0;margin-right:.05em"><span class="pstrut" style="height:2.7em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:.10903em">N</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:.15em"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:.16666666666666666em"></span><span class="mord"><span class="mord mathnormal" style="margin-right:.03588em">y</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:.32833099999999993em"><span style="top:-2.5500000000000003em;margin-left:-.03588em;margin-right:.05em"><span class="pstrut" style="height:2.7em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:.10903em">N</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:.15em"><span></span></span></span></span></span></span><span class="mclose">)</span></span></span></span></span></span></p><p>其中</p><p class="katex-block"><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><msub><mi>x</mi><mi>i</mi></msub><mi>ϵ</mi><mi>χ</mi><mo>⊆</mo><msup><mi>R</mi><mi>n</mi></msup></mrow><annotation encoding="application/x-tex">x_{i}\epsilon \chi \subseteq R^{n}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:.8304100000000001em;vertical-align:-.19444em"></span><span class="mord"><span class="mord mathnormal">x</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:.31166399999999994em"><span style="top:-2.5500000000000003em;margin-left:0;margin-right:.05em"><span class="pstrut" style="height:2.7em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">i</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:.15em"><span></span></span></span></span></span></span><span class="mord mathnormal">ϵ</span><span class="mord mathnormal">χ</span><span class="mspace" style="margin-right:.2777777777777778em"></span><span class="mrel">⊆</span><span class="mspace" style="margin-right:.2777777777777778em"></span></span><span class="base"><span class="strut" style="height:.7143919999999999em;vertical-align:0"></span><span class="mord"><span class="mord mathnormal" style="margin-right:.00773em">R</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:.7143919999999999em"><span style="top:-3.113em;margin-right:.05em"><span class="pstrut" style="height:2.7em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">n</span></span></span></span></span></span></span></span></span></span></span></span></span></p><p>为实例的特征向量</p><p class="katex-block"><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><msub><mi>y</mi><mi>i</mi></msub><mi>ϵ</mi><mi>Y</mi><mo>=</mo><mrow><msub><mi>c</mi><mn>1</mn></msub><mo separator="true">,</mo><msub><mi>c</mi><mn>2</mn></msub><mi mathvariant="normal">.</mi><mi mathvariant="normal">.</mi><mi mathvariant="normal">.</mi><mi mathvariant="normal">.</mi><mi mathvariant="normal">.</mi><mi mathvariant="normal">.</mi><mo separator="true">,</mo><msub><mi>c</mi><mi>K</mi></msub><mo separator="true">,</mo></mrow></mrow><annotation encoding="application/x-tex">y_{i} \epsilon Y={c_{1}, c_{2}......, c_{K}, }</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:.8777699999999999em;vertical-align:-.19444em"></span><span class="mord"><span class="mord mathnormal" style="margin-right:.03588em">y</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:.31166399999999994em"><span style="top:-2.5500000000000003em;margin-left:-.03588em;margin-right:.05em"><span class="pstrut" style="height:2.7em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">i</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:.15em"><span></span></span></span></span></span></span><span class="mord mathnormal">ϵ</span><span class="mord mathnormal" style="margin-right:.22222em">Y</span><span class="mspace" style="margin-right:.2777777777777778em"></span><span class="mrel">=</span><span class="mspace" style="margin-right:.2777777777777778em"></span></span><span class="base"><span class="strut" style="height:.625em;vertical-align:-.19444em"></span><span class="mord"><span class="mord"><span class="mord mathnormal">c</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:.30110799999999993em"><span style="top:-2.5500000000000003em;margin-left:0;margin-right:.05em"><span class="pstrut" style="height:2.7em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:.15em"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:.16666666666666666em"></span><span class="mord"><span class="mord mathnormal">c</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:.30110799999999993em"><span style="top:-2.5500000000000003em;margin-left:0;margin-right:.05em"><span class="pstrut" style="height:2.7em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:.15em"><span></span></span></span></span></span></span><span class="mord">.</span><span class="mord">.</span><span class="mord">.</span><span class="mord">.</span><span class="mord">.</span><span class="mord">.</span><span class="mpunct">,</span><span class="mspace" style="margin-right:.16666666666666666em"></span><span class="mord"><span class="mord mathnormal">c</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:.32833099999999993em"><span style="top:-2.5500000000000003em;margin-left:0;margin-right:.05em"><span class="pstrut" style="height:2.7em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:.07153em">K</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:.15em"><span></span></span></span></span></span></span><span class="mpunct">,</span></span></span></span></span></span></p><p>为实例的类别,i=1,2,…,N</p><p>输出:给定实例x,要能输出新给的特征向量所属y中的类</p><p>(1). 根据给定的距离度量,在训练集T中找出与x最邻近的k个点,涵盖这k个点的x的邻域基座 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>N</mi><mi>k</mi></msub><mo stretchy="false">(</mo><mi>x</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">N_{k} (x)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-.25em"></span><span class="mord"><span class="mord mathnormal" style="margin-right:.10903em">N</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:.33610799999999996em"><span style="top:-2.5500000000000003em;margin-left:-.10903em;margin-right:.05em"><span class="pstrut" style="height:2.7em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:.03148em">k</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:.15em"><span></span></span></span></span></span></span><span class="mopen">(</span><span class="mord mathnormal">x</span><span class="mclose">)</span></span></span></span> ;</p><p>(2). 在 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>N</mi><mi>k</mi></msub><mo stretchy="false">(</mo><mi>x</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">N_{k} (x)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-.25em"></span><span class="mord"><span class="mord mathnormal" style="margin-right:.10903em">N</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:.33610799999999996em"><span style="top:-2.5500000000000003em;margin-left:-.10903em;margin-right:.05em"><span class="pstrut" style="height:2.7em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:.03148em">k</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:.15em"><span></span></span></span></span></span></span><span class="mopen">(</span><span class="mord mathnormal">x</span><span class="mclose">)</span></span></span></span> 中根据分类决策规则(如多数表决表,对<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>N</mi><mi>k</mi></msub><mo stretchy="false">(</mo><mi>x</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">N_{k} (x)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-.25em"></span><span class="mord"><span class="mord mathnormal" style="margin-right:.10903em">N</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:.33610799999999996em"><span style="top:-2.5500000000000003em;margin-left:-.10903em;margin-right:.05em"><span class="pstrut" style="height:2.7em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:.03148em">k</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:.15em"><span></span></span></span></span></span></span><span class="mopen">(</span><span class="mord mathnormal">x</span><span class="mclose">)</span></span></span></span> 进行排序,然后取出现频率最高的第k个点的类)决定x的类别y:</p><p class="katex-block"><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>y</mi><mo>=</mo><mi>a</mi><mi>r</mi><mi>g</mi><mtext> </mtext><munderover><mo><mi>max</mi><mo></mo></mo><msub><mi>c</mi><mi>j</mi></msub><mtext> </mtext></munderover><munderover><mo>∑</mo><mrow><msub><mi>x</mi><mi>i</mi></msub><mo>⊆</mo><msub><mi>N</mi><mi>k</mi></msub><mo stretchy="false">(</mo><mi>x</mi><mo stretchy="false">)</mo></mrow><mtext> </mtext></munderover><mi>I</mi><mo stretchy="false">(</mo><msub><mi>y</mi><mi>i</mi></msub><mo>=</mo><msub><mi>c</mi><mi>j</mi></msub><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">y=arg\, \max_{c_{j}}^{\ } \sum_{x_{i}\subseteq N_{k}(x)}^{\ } I(y_{i}=c_{j})</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:.625em;vertical-align:-.19444em"></span><span class="mord mathnormal" style="margin-right:.03588em">y</span><span class="mspace" style="margin-right:.2777777777777778em"></span><span class="mrel">=</span><span class="mspace" style="margin-right:.2777777777777778em"></span></span><span class="base"><span class="strut" style="height:2.86601em;vertical-align:-1.5160049999999998em"></span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:.02778em">r</span><span class="mord mathnormal" style="margin-right:.03588em">g</span><span class="mspace" style="margin-right:.16666666666666666em"></span><span class="mspace" style="margin-right:.16666666666666666em"></span><span class="mop op-limits"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:.73056em"><span style="top:-2.4em;margin-left:0"><span class="pstrut" style="height:3em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight"><span class="mord mathnormal mtight">c</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:.3280857142857143em"><span style="top:-2.357em;margin-left:0;margin-right:.07142857142857144em"><span class="pstrut" style="height:2.5em"></span><span class="sizing reset-size3 size1 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:.05724em">j</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:.2818857142857143em"><span></span></span></span></span></span></span></span></span></span><span style="top:-3em"><span class="pstrut" style="height:3em"></span><span><span class="mop">max</span></span></span><span style="top:-3.63056em;margin-left:0"><span class="pstrut" style="height:3em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mspace mtight"><span class="mtight"> </span></span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:.8973199999999999em"><span></span></span></span></span></span><span class="mspace" style="margin-right:.16666666666666666em"></span><span class="mop op-limits"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:1.3500050000000006em"><span style="top:-1.8089950000000001em;margin-left:0"><span class="pstrut" style="height:3.05em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight"><span class="mord mathnormal mtight">x</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:.3280857142857143em"><span style="top:-2.357em;margin-left:0;margin-right:.07142857142857144em"><span class="pstrut" style="height:2.5em"></span><span class="sizing reset-size3 size1 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">i</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:.143em"><span></span></span></span></span></span></span><span class="mrel mtight">⊆</span><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:.10903em">N</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:.3448em"><span style="top:-2.3487714285714287em;margin-left:-.10903em;margin-right:.07142857142857144em"><span class="pstrut" style="height:2.5em"></span><span class="sizing reset-size3 size1 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:.03148em">k</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:.15122857142857138em"><span></span></span></span></span></span></span><span class="mopen mtight">(</span><span class="mord mathnormal mtight">x</span><span class="mclose mtight">)</span></span></span></span><span style="top:-3.050005em"><span class="pstrut" style="height:3.05em"></span><span><span class="mop op-symbol large-op">∑</span></span></span><span style="top:-4.3000050000000005em;margin-left:0"><span class="pstrut" style="height:3.05em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mspace mtight"><span class="mtight"> </span></span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:1.5160049999999998em"><span></span></span></span></span></span><span class="mspace" style="margin-right:.16666666666666666em"></span><span class="mord mathnormal" style="margin-right:.07847em">I</span><span class="mopen">(</span><span class="mord"><span class="mord mathnormal" style="margin-right:.03588em">y</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:.31166399999999994em"><span style="top:-2.5500000000000003em;margin-left:-.03588em;margin-right:.05em"><span class="pstrut" style="height:2.7em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">i</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:.15em"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:.2777777777777778em"></span><span class="mrel">=</span><span class="mspace" style="margin-right:.2777777777777778em"></span></span><span class="base"><span class="strut" style="height:1.036108em;vertical-align:-.286108em"></span><span class="mord"><span class="mord mathnormal">c</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:.311664em"><span style="top:-2.5500000000000003em;margin-left:0;margin-right:.05em"><span class="pstrut" style="height:2.7em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:.05724em">j</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:.286108em"><span></span></span></span></span></span></span><span class="mclose">)</span></span></span></span></span></p><p>其中I为指示函数,为$$y_{i}=c_{j}$$情况下, I = 1 否则I = 0</p><ul><li>[x] Edit By Porter, 积水成渊,蛟龙生焉。</li></ul><span id="more"></span><p>更通俗的理解可以为如下:</p><h2 id="knn是通过测量不同特征值之间的距离进行分类"><a class="markdownIt-Anchor" href="#knn是通过测量不同特征值之间的距离进行分类"></a> KNN是通过测量不同特征值之间的距离进行分类</h2><ul><li>它的思路是:</li></ul><p>如果选择一个待分类的样本,其在特征空间中有k个最相似的样本值(即特征空间中和这个待分类的点为最邻近点集)。</p><p>这k个样本集中的绝大多数属于某一类别,则该待分类的样本也属于这个绝大多数的同一类别。</p><p>其中K通常是不大于20的整数。</p><p>KNN算法中,所选择的邻居都是已经正确分类的对象。该方法在定类决策上只依据最邻近的一个或者几个样本的类别来决定待分样本所属的类别。</p><h3 id="例子"><a class="markdownIt-Anchor" href="#例子"></a> 例子</h3><p>下面通过一个简单的例子说明一下:如下图,绿色圆要被决定赋予哪个类,是红色三角形还是蓝色四方形?如下图所示</p><ul><li><p>如果K=3,最小的圆, 由于红色三角形所占比例为2/3,绿色圆将被赋予红色三角形那个类。</p></li><li><p>如果K=5,虚线内,由于蓝色四方形比例为3/5,因此绿色圆被赋予蓝色四方形类。</p></li></ul><p><img src="/image2/knn_1.jpg" alt="knn算法例子"></p><p>由此也说明了KNN算法的结果很大程度取决于K的选择。</p><p>在KNN中,通过计算对象间距离来作为各个对象之间的非相似性指标,避免了对象之间的匹配问题,在这里距离一般使用欧氏距离或曼哈顿距离:</p><h3 id="欧氏距离"><a class="markdownIt-Anchor" href="#欧氏距离"></a> 欧氏距离:</h3><p class="katex-block"><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>d</mi><mo stretchy="false">(</mo><mi>x</mi><mo separator="true">,</mo><mi>y</mi><mo stretchy="false">)</mo><mo>=</mo><msqrt><mrow><munderover><mo>∑</mo><mrow><mi>k</mi><mo>=</mo><mn>1</mn></mrow><mi>n</mi></munderover><mo stretchy="false">(</mo><msub><mi>x</mi><mi>k</mi></msub><mo>−</mo><msub><mi>y</mi><mi>k</mi></msub><msup><mo stretchy="false">)</mo><mn>2</mn></msup></mrow></msqrt></mrow><annotation encoding="application/x-tex">d(x,y)=\sqrt{\sum_{k=1}^{n}(x_{k}-y_{k})^{2}}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-.25em"></span><span class="mord mathnormal">d</span><span class="mopen">(</span><span class="mord mathnormal">x</span><span class="mpunct">,</span><span class="mspace" style="margin-right:.16666666666666666em"></span><span class="mord mathnormal" style="margin-right:.03588em">y</span><span class="mclose">)</span><span class="mspace" style="margin-right:.2777777777777778em"></span><span class="mrel">=</span><span class="mspace" style="margin-right:.2777777777777778em"></span></span><span class="base"><span class="strut" style="height:3.1812600000000004em;vertical-align:-1.302113em"></span><span class="mord sqrt"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:1.8791470000000003em"><span class="svg-align" style="top:-5.141260000000001em"><span class="pstrut" style="height:5.141260000000001em"></span><span class="mord" style="padding-left:1.056em"><span class="mop op-limits"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:1.6513970000000002em"><span style="top:-1.8478869999999998em;margin-left:0"><span class="pstrut" style="height:3.05em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:.03148em">k</span><span class="mrel mtight">=</span><span class="mord mtight">1</span></span></span></span><span style="top:-3.0500049999999996em"><span class="pstrut" style="height:3.05em"></span><span><span class="mop op-symbol large-op">∑</span></span></span><span style="top:-4.300005em;margin-left:0"><span class="pstrut" style="height:3.05em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">n</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:1.302113em"><span></span></span></span></span></span><span class="mopen">(</span><span class="mord"><span class="mord mathnormal">x</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:.33610799999999996em"><span style="top:-2.5500000000000003em;margin-left:0;margin-right:.05em"><span class="pstrut" style="height:2.7em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:.03148em">k</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:.15em"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:.2222222222222222em"></span><span class="mbin">−</span><span class="mspace" style="margin-right:.2222222222222222em"></span><span class="mord"><span class="mord mathnormal" style="margin-right:.03588em">y</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:.33610799999999996em"><span style="top:-2.5500000000000003em;margin-left:-.03588em;margin-right:.05em"><span class="pstrut" style="height:2.7em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:.03148em">k</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:.15em"><span></span></span></span></span></span></span><span class="mclose"><span class="mclose">)</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:.740108em"><span style="top:-2.9890000000000003em;margin-right:.05em"><span class="pstrut" style="height:2.7em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span></span></span></span></span></span></span><span style="top:-3.8391470000000005em"><span class="pstrut" style="height:5.141260000000001em"></span><span class="hide-tail" style="min-width:.742em;height:3.2212600000000005em"><svg width="400em" height="3.2212600000000005em" viewbox="0 0 400000 3221" preserveaspectratio="xMinYMin slice"><path d="M702 80H40000040
H742v3087l-4 4-4 4c-.667.7 -2 1.5-4 2.5s-4.167 1.833-6.5 2.5-5.5 1-9.5 1
h-12l-28-84c-16.667-52-96.667 -294.333-240-727l-212 -643 -85 170
c-4-3.333-8.333-7.667-13 -13l-13-13l77-155 77-156c66 199.333 139 419.667
219 661 l218 661zM702 80H400000v40H742z"/></svg></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:1.302113em"><span></span></span></span></span></span></span></span></span></span></p><h3 id="曼哈顿距离"><a class="markdownIt-Anchor" href="#曼哈顿距离"></a> 曼哈顿距离:</h3><p class="katex-block"><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>d</mi><mo stretchy="false">(</mo><mi>x</mi><mo separator="true">,</mo><mi>y</mi><mo stretchy="false">)</mo><mo>=</mo><msqrt><mrow><munderover><mo>∑</mo><mrow><mi>k</mi><mo>=</mo><mn>1</mn></mrow><mi>n</mi></munderover><mi mathvariant="normal">∣</mi><msub><mi>x</mi><mi>k</mi></msub><mo>−</mo><msub><mi>y</mi><mi>k</mi></msub><mi mathvariant="normal">∣</mi></mrow></msqrt></mrow><annotation encoding="application/x-tex">d(x,y)=\sqrt{\sum_{k=1}^{n}|x_{k}-y_{k}|}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-.25em"></span><span class="mord mathnormal">d</span><span class="mopen">(</span><span class="mord mathnormal">x</span><span class="mpunct">,</span><span class="mspace" style="margin-right:.16666666666666666em"></span><span class="mord mathnormal" style="margin-right:.03588em">y</span><span class="mclose">)</span><span class="mspace" style="margin-right:.2777777777777778em"></span><span class="mrel">=</span><span class="mspace" style="margin-right:.2777777777777778em"></span></span><span class="base"><span class="strut" style="height:3.1812600000000004em;vertical-align:-1.302113em"></span><span class="mord sqrt"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:1.8791470000000003em"><span class="svg-align" style="top:-5.141260000000001em"><span class="pstrut" style="height:5.141260000000001em"></span><span class="mord" style="padding-left:1.056em"><span class="mop op-limits"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:1.6513970000000002em"><span style="top:-1.8478869999999998em;margin-left:0"><span class="pstrut" style="height:3.05em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:.03148em">k</span><span class="mrel mtight">=</span><span class="mord mtight">1</span></span></span></span><span style="top:-3.0500049999999996em"><span class="pstrut" style="height:3.05em"></span><span><span class="mop op-symbol large-op">∑</span></span></span><span style="top:-4.300005em;margin-left:0"><span class="pstrut" style="height:3.05em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">n</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:1.302113em"><span></span></span></span></span></span><span class="mspace" style="margin-right:.16666666666666666em"></span><span class="mord">∣</span><span class="mord"><span class="mord mathnormal">x</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:.33610799999999996em"><span style="top:-2.5500000000000003em;margin-left:0;margin-right:.05em"><span class="pstrut" style="height:2.7em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:.03148em">k</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:.15em"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:.2222222222222222em"></span><span class="mbin">−</span><span class="mspace" style="margin-right:.2222222222222222em"></span><span class="mord"><span class="mord mathnormal" style="margin-right:.03588em">y</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:.33610799999999996em"><span style="top:-2.5500000000000003em;margin-left:-.03588em;margin-right:.05em"><span class="pstrut" style="height:2.7em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:.03148em">k</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:.15em"><span></span></span></span></span></span></span><span class="mord">∣</span></span></span><span style="top:-3.8391470000000005em"><span class="pstrut" style="height:5.141260000000001em"></span><span class="hide-tail" style="min-width:.742em;height:3.2212600000000005em"><svg width="400em" height="3.2212600000000005em" viewbox="0 0 400000 3221" preserveaspectratio="xMinYMin slice"><path d="M702 80H40000040
H742v3087l-4 4-4 4c-.667.7 -2 1.5-4 2.5s-4.167 1.833-6.5 2.5-5.5 1-9.5 1
h-12l-28-84c-16.667-52-96.667 -294.333-240-727l-212 -643 -85 170
c-4-3.333-8.333-7.667-13 -13l-13-13l77-155 77-156c66 199.333 139 419.667
219 661 l218 661zM702 80H400000v40H742z"/></svg></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:1.302113em"><span></span></span></span></span></span></span></span></span></span></p><p>同时,KNN通过依据k个对象中占优的类别进行决策,而不是单一的对象类别决策。这两点就是KNN算法的优势。</p><hr><h2 id="接下来对knn算法的思想总结一下"><a class="markdownIt-Anchor" href="#接下来对knn算法的思想总结一下"></a> 接下来对KNN算法的思想总结一下</h2><p>就是在训练集中数据和标签已知的情况下,输入测试数据,将测试数据的特征与训练集中对应的特征进行相互比较,找到训练集中与之最为相似的前K个数据,则该测试数据对应的类别就是K个数据中出现次数最多的那个分类,其算法的描述为:</p><ul><li><p>1)计算测试数据与各个训练数据之间的距离;</p></li><li><p>2)按照距离的递增关系进行排序;</p></li><li><p>3)选取距离最小的K个点;</p></li><li><p>4)确定前K个点所在类别的出现频率;</p></li><li><p>5)返回前K个点中出现频率最高的类别作为测试数据的预测分类。</p></li></ul><p>python 代码实现:</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#coding:utf-8</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> numpy <span class="keyword">import</span> *</span><br><span class="line"><span class="keyword">import</span> operator</span><br><span class="line"></span><br><span class="line"><span class="comment">##给出训练数据以及对应的类别</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">createDataSet</span>():</span><br><span class="line"> group = array([[<span class="number">1.0</span>,<span class="number">2.0</span>],[<span class="number">1.2</span>,<span class="number">0.1</span>],[<span class="number">0.1</span>,<span class="number">1.4</span>],[<span class="number">0.3</span>,<span class="number">3.5</span>]])</span><br><span class="line"> labels = [<span class="string">'A'</span>,<span class="string">'A'</span>,<span class="string">'B'</span>,<span class="string">'B'</span>]</span><br><span class="line"> <span class="keyword">return</span> group,labels</span><br><span class="line"></span><br><span class="line"><span class="comment">###通过KNN进行分类</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">classify</span>(<span class="params"><span class="built_in">input</span>,dataSe t,label,k</span>):</span><br><span class="line"> dataSize = dataSet.shape[<span class="number">0</span>]</span><br><span class="line"> <span class="comment">####计算欧式距离</span></span><br><span class="line"> diff = tile(<span class="built_in">input</span>,(dataSize,<span class="number">1</span>)) - dataSet</span><br><span class="line"> sqdiff = diff ** <span class="number">2</span></span><br><span class="line"> squareDist = <span class="built_in">sum</span>(sqdiff,axis = <span class="number">1</span>)<span class="comment">###行向量分别相加,从而得到新的一个行向量</span></span><br><span class="line"> dist = squareDist ** <span class="number">0.5</span></span><br><span class="line"> </span><br><span class="line"> <span class="comment">##对距离进行排序</span></span><br><span class="line"> sortedDistIndex = argsort(dist)<span class="comment">##argsort()根据元素的值从大到小对元素进行排序,返回下标</span></span><br><span class="line"></span><br><span class="line"> classCount={}</span><br><span class="line"> <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(k):</span><br><span class="line"> voteLabel = label[sortedDistIndex[i]]</span><br><span class="line"> <span class="comment">###对选取的K个样本所属的类别个数进行统计</span></span><br><span class="line"> classCount[voteLabel] = classCount.get(voteLabel,<span class="number">0</span>) + <span class="number">1</span></span><br><span class="line"> <span class="comment">###选取出现的类别次数最多的类别</span></span><br><span class="line"> maxCount = <span class="number">0</span></span><br><span class="line"> <span class="keyword">for</span> key,value <span class="keyword">in</span> classCount.items():</span><br><span class="line"> <span class="keyword">if</span> value > maxCount:</span><br><span class="line"> maxCount = value</span><br><span class="line"> classes = key</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> classes</span><br></pre></td></tr></table></figure><p>测试代码如下:</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#-*-coding:utf-8 -*-</span></span><br><span class="line"><span class="keyword">import</span> sys</span><br><span class="line">sys.path.append(<span class="string">"...文件路径..."</span>)</span><br><span class="line"><span class="keyword">import</span> KNN</span><br><span class="line"><span class="keyword">from</span> numpy <span class="keyword">import</span> *</span><br><span class="line">dataSet,labels = KNN.createDataSet()</span><br><span class="line"><span class="built_in">input</span> = array([<span class="number">1.1</span>,<span class="number">0.3</span>])</span><br><span class="line">K = <span class="number">3</span></span><br><span class="line">output = KNN.classify(<span class="built_in">input</span>,dataSet,labels,K)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">"测试数据为:"</span>,<span class="built_in">input</span>,<span class="string">"分类结果为:"</span>,output)</span><br></pre></td></tr></table></figure><p>回车之后的结果为:</p><p>测试数据为: [ 1.1 0.3] 分类为: A</p><p>答案符合我们的预期,要证明算法的准确性,势必还需要通过处理复杂问题进行验证,之后另行说明。</p><p>【提示】python版本为3.7</p><p>具体的KNN视频教程地址为:</p><div><iframe height="498" width="100%" src="https://player.bilibili.com/player.html?aid=38792500&cid=68183480&page=1" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen></iframe></div><h2 id="参考"><a class="markdownIt-Anchor" href="#参考"></a> 参考</h2><p>参考文档来源1:<a target="_blank" rel="noopener" href="https://www.cnblogs.com/ybjourney/p/4702562.html">Yabea-博客</a></p><p>参考文档来源2:<a href="">李航-统计学习方法</a></p></div></div><div class="copyright"><p><span>本文标题:</span><a href="/kjinglin/">K近邻算法</a></p><p><span>文章作者:</span><a href="/" title="回到主页"></a></p><p><span>发布时间:</span>2019-01-04, 22:55:03</p><p><span>最后更新:</span>2024-06-14, 16:38:29</p><p><span>原始链接:</span><a class="post-url" href="/kjinglin/" title="K近邻算法">https://blogs.porterpan.top/kjinglin/</a> <span class="copy-path" data-clipboard-text="原文: https://blogs.porterpan.top/kjinglin/ 作者: " title="点击复制文章链接"><i class="fa fa-clipboard"></i></span><script>var clipboard=new Clipboard(".copy-path")</script></p><p><span>许可协议:</span><i class="fa fa-creative-commons"></i> <a rel="license noopener" target="_blank" href="http://creativecommons.org/licenses/by-nc-sa/4.0/" title="CC BY-NC-SA 4.0 International" target="_blank">"署名-非商用-相同方式共享 4.0"</a> 转载请保留原文链接及作者。</p></div><nav id="article-nav"><div id="article-nav-newer" class="article-nav-title"><a href="/PrioritizedEP/">Prioritized Experience Replay</a></div><div id="article-nav-older" class="article-nav-title"><a href="/maerkefu21/">2.1 马尔科夫链</a></div></nav></article><div id="toc" class="toc-article"><strong class="toc-title">文章目录</strong><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%91%98%E8%A6%81"><span class="toc-number">1.</span> <span class="toc-text">摘要</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#knn%E6%98%AF%E9%80%9A%E8%BF%87%E6%B5%8B%E9%87%8F%E4%B8%8D%E5%90%8C%E7%89%B9%E5%BE%81%E5%80%BC%E4%B9%8B%E9%97%B4%E7%9A%84%E8%B7%9D%E7%A6%BB%E8%BF%9B%E8%A1%8C%E5%88%86%E7%B1%BB"><span class="toc-number">2.</span> <span class="toc-text">KNN是通过测量不同特征值之间的距离进行分类</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BE%8B%E5%AD%90"><span class="toc-number">2.1.</span> <span class="toc-text">例子</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%AC%A7%E6%B0%8F%E8%B7%9D%E7%A6%BB"><span class="toc-number">2.2.</span> <span class="toc-text">欧氏距离:</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9B%BC%E5%93%88%E9%A1%BF%E8%B7%9D%E7%A6%BB"><span class="toc-number">2.3.</span> <span class="toc-text">曼哈顿距离:</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8E%A5%E4%B8%8B%E6%9D%A5%E5%AF%B9knn%E7%AE%97%E6%B3%95%E7%9A%84%E6%80%9D%E6%83%B3%E6%80%BB%E7%BB%93%E4%B8%80%E4%B8%8B"><span class="toc-number">3.</span> <span class="toc-text">接下来对KNN算法的思想总结一下</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%82%E8%80%83"><span class="toc-number">4.</span> <span class="toc-text">参考</span></a></li></ol></div><style>.left-col .switch-area,.left-col .switch-btn{display:none}.toc-level-4 i,.toc-level-4 ol{display:none!important}</style><input type="button" id="tocButton" value="隐藏目录" title="点击按钮隐藏或者显示文章目录"><script>yiliaConfig.toc=["隐藏目录","显示目录",!0]</script><div class="share"><div class="bdsharebuttonbox"><a href="#" class="fa fa-twitter bds_twi" data-cmd="twi" title="分享到推特"></a> <a href="#" class="fa fa-weibo bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a> <a href="#" class="fa fa-qq bds_sqq" data-cmd="sqq" title="分享给 QQ 好友"></a> <a href="#" class="fa fa-files-o bds_copy" data-cmd="copy" title="复制网址"></a> <a href="#" class="fa fa fa-envelope-o bds_mail" data-cmd="mail" title="通过邮件分享"></a> <a href="#" class="fa fa-weixin bds_weixin" data-cmd="weixin" title="生成文章二维码"></a> <a href="#" class="fa fa-share-alt bds_more" data-cmd="more"></a></div><script>with(window._bd_share_config={common:{bdSnsKey:{},bdText:"K近邻算法 | Porter-聚水渊 ",bdMini:"2",bdMiniList:!1,bdPic:"",bdStyle:"0",bdSize:"24"},share:{}},document)(getElementsByTagName("head")[0]||body).appendChild(createElement("script")).src="http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion="+~(-new Date/36e5)</script></div><div id="gitalk-container" style="padding:0 30px"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css"><script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script><script type="text/javascript">var gitalk=new Gitalk({clientID:"8810e0ed40066eee0ea4",clientSecret:"5035d100854226605dd381db52a7e0327d22f3d4",repo:"porterpan.github.io",owner:"porterpan",admin:["porterpan"],id:"Fri Jan 04 2019 22:55:03 GMT+0800",distractionFreeMode:"true"});gitalk.render("gitalk-container")</script><div class="scroll" id="post-nav-button"><a href="/PrioritizedEP/" title="上一篇: Prioritized Experience Replay"><i class="fa fa-angle-left"></i> </a><a title="文章列表"><i class="fa fa-bars"></i><i class="fa fa-times"></i></a> <a href="/maerkefu21/" title="下一篇: 2.1 马尔科夫链"><i class="fa fa-angle-right"></i></a></div><ul class="post-list"><li class="post-list-item"><a class="post-list-link" href="/ufldv2/">UFLDv2论文阅读笔记</a></li><li class="post-list-item"><a class="post-list-link" href="/lines_gauss_algorithm/">steger算法lines_gauss,提取激光线</a></li><li class="post-list-item"><a class="post-list-link" href="/ransac-algorithm/">RANSAC算法</a></li><li class="post-list-item"><a class="post-list-link" href="/pnp_solve_theory/">pnp 算法求解</a></li><li class="post-list-item"><a class="post-list-link" href="/lidar_tocar_lidar_to_lidar_calibration/">激光雷达和车体的标定+激光雷达和激光雷达的标定</a></li><li class="post-list-item"><a class="post-list-link" href="/camera-imu-calibration/">camera和imu联合标定</a></li><li class="post-list-item"><a class="post-list-link" href="/ROS_camera_lidar_calibration_gazebo/">ROS中在gazebo里实现相机和激光雷达的标定demo</a></li><li class="post-list-item"><a class="post-list-link" href="/camera_lidar_calibration/">相机和激光雷达联合标定</a></li><li class="post-list-item"><a class="post-list-link" href="/hexo-reinstall-localimage/">1.30 hexo 重新部署笔记环境+实现本地图片加载</a></li><li class="post-list-item"><a class="post-list-link" href="/binocular-camera-calibration/">双目相机标定理论</a></li><li class="post-list-item"><a class="post-list-link" href="/letcode_train/">力扣刷题</a></li><li class="post-list-item"><a class="post-list-link" href="/opencv-handeye-calibration/">opencv 手眼标定理论</a></li><li class="post-list-item"><a class="post-list-link" href="/monocular-camera-instric-calibration/">单目相机内参标定理论</a></li><li class="post-list-item"><a class="post-list-link" href="/yolov1/">2.19 YOLOV1</a></li><li class="post-list-item"><a class="post-list-link" href="/robot-arm-DH-paremeter/">机械臂DH参数</a></li><li class="post-list-item"><a class="post-list-link" href="/vscodenewproject/">2.17vscode新建工程</a></li><li class="post-list-item"><a class="post-list-link" href="/vscodegcc/">2.18gcc相关命令</a></li><li class="post-list-item"><a class="post-list-link" href="/cmakelistsnote/">2.16CMakeLists学习笔记.md</a></li><li class="post-list-item"><a class="post-list-link" href="/ubuntu_yolov4_train_01/">yolo进行数据训练2</a></li><li class="post-list-item"><a class="post-list-link" href="/ubuntu_yolov4_train/">yolo进行数据训练</a></li><li class="post-list-item"><a class="post-list-link" href="/ubuntu_opencv_qt/">Ubuntu安装Opencv4和qt开发环境</a></li><li class="post-list-item"><a class="post-list-link" href="/ubuntu_install_cuda_01/">3.16Ubuntu再次安装了CUDA</a></li><li class="post-list-item"><a class="post-list-link" href="/bing_scrap_human_face/">3.15必应网站爬取清的人脸图片</a></li><li class="post-list-item"><a class="post-list-link" href="/face_recognition_dataset_make/">3.13自己制作人脸识别数据集</a></li><li class="post-list-item"><a class="post-list-link" href="/keras_checkpoint_rstore/">3.14keras训练的断点保存和恢复</a></li><li class="post-list-item"><a class="post-list-link" href="/opencv_haar/">3.11opencv中haar检测器的使用</a></li><li class="post-list-item"><a class="post-list-link" href="/keras_face_recognition/">3.12keras人脸识别</a></li><li class="post-list-item"><a class="post-list-link" href="/RMBRecognition/">3.10keras人民币识别</a></li><li class="post-list-item"><a class="post-list-link" href="/cuda_encour_problem/">3.9CUDA使用过程中的问题</a></li><li class="post-list-item"><a class="post-list-link" href="/keras_cat_dogs_model_load/">3.8 keras 入门模型保存和加载之猫狗识别</a></li><li class="post-list-item"><a class="post-list-link" href="/keras_cat_dogs/">3.7 keras入门之猫狗分类</a></li><li class="post-list-item"><a class="post-list-link" href="/keras_basic/">3.5 keras 入门</a></li><li class="post-list-item"><a class="post-list-link" href="/keras_MNIST/">3.6 keras 之手写字识别</a></li><li class="post-list-item"><a class="post-list-link" href="/TxtToSpeech/">语音合成小工具</a></li><li class="post-list-item"><a class="post-list-link" href="/staticstringnum/">输入一串字符串统计字符个数</a></li><li class="post-list-item"><a class="post-list-link" href="/heapAndStack/">堆和栈的相关笔记</a></li><li class="post-list-item"><a class="post-list-link" href="/huawei-qishuiping/">华为软件笔试---汽水瓶编程</a></li><li class="post-list-item"><a class="post-list-link" href="/writenexam/">python在线笔试学习笔记</a></li><li class="post-list-item"><a class="post-list-link" href="/filetosendanyway/">方便的在线高速文件共享</a></li><li class="post-list-item"><a class="post-list-link" href="/argparsenotes/">argparse模块使用学习笔记</a></li><li class="post-list-item"><a class="post-list-link" href="/anacondaError/">anaconda运行出错现象及解决办法</a></li><li class="post-list-item"><a class="post-list-link" href="/htmlyasuo/">hexo-neat网页代码压缩去空格</a></li><li class="post-list-item"><a class="post-list-link" href="/bolgwriteformat/">基于本修改过主题的blog写作格式</a></li><li class="post-list-item"><a class="post-list-link" href="/zjtdbs/">字节跳动笔试-算法岗(2019/07/31)</a></li><li class="post-list-item"><a class="post-list-link" href="/hexoYeleeUrl/">hexo Yelee网址修改为简短的英文</a></li><li class="post-list-item"><a class="post-list-link" href="/hexoFitment/">hexo添加Gitment评论功能</a></li><li class="post-list-item"><a class="post-list-link" href="/jupyterGrammer/">jupyter notebook 使用</a></li><li class="post-list-item"><a class="post-list-link" href="/cifar10notes/">CIFAR-10 图像分类Notes</a></li><li class="post-list-item"><a class="post-list-link" href="/pdfqianruyulan/">PDF在线预览嵌入到文章中的模板</a></li><li class="post-list-item"><a class="post-list-link" href="/gushiciyanzheng/">古诗词验证码使用百度api自动识别</a></li><li class="post-list-item"><a class="post-list-link" href="/hexoloadlocalimage/">hexo加载本地图片的方法</a></li><li class="post-list-item"><a class="post-list-link" href="/kmeans/">2.14 kmeans算法</a></li><li class="post-list-item"><a class="post-list-link" href="/dockerCommand/">1.20 docker 基础</a></li><li class="post-list-item"><a class="post-list-link" href="/dockerConf/">docker 配置权限问题</a></li><li class="post-list-item"><a class="post-list-link" href="/GBDT/">2.15 机器学习算法GBDT</a></li><li class="post-list-item"><a class="post-list-link" href="/tfCNN7/">第七章 TensorFlow 卷积神经网络</a></li><li class="post-list-item"><a class="post-list-link" href="/tfshouxiezi/">第五章 TensorFlow MNIST 手写字识别笔记</a></li><li class="post-list-item"><a class="post-list-link" href="/tfyouhua4/">第四章个人笔记-神经网络优化笔记</a></li><li class="post-list-item"><a class="post-list-link" href="/linuxPDF/">跨平台好用且小巧的pdf阅读器</a></li><li class="post-list-item"><a class="post-list-link" href="/musicplug/">web中插入视频和音乐播放</a></li><li class="post-list-item"><a class="post-list-link" href="/tfnotesp1/">TensorFlow的相关学习笔记 part1</a></li><li class="post-list-item"><a class="post-list-link" href="/ml&dlcomference/">机器学习-深度学习顶级会议罗列</a></li><li class="post-list-item"><a class="post-list-link" href="/installopencv/">1.18 ubuntu下安装OpenCV 3.4.3</a></li><li class="post-list-item"><a class="post-list-link" href="/gymGazabeInstall/">1.19 gym_gazabe安装配置</a></li><li class="post-list-item"><a class="post-list-link" href="/gym-gazeboInstall/">gym-gazebo安装后的测试</a></li><li class="post-list-item"><a class="post-list-link" href="/ubuntuinstallGym/">ubuntu16 安装gym-gazebo</a></li><li class="post-list-item"><a class="post-list-link" href="/DQNgym/">基于DQN的gym_gazebo运行代码演示</a></li><li class="post-list-item"><a class="post-list-link" href="/gymError/">gym 运行常见错误及解决办法</a></li><li class="post-list-item"><a class="post-list-link" href="/ros&gazeboInstall/">ros and gazebo and gym_gazebo安装</a></li><li class="post-list-item"><a class="post-list-link" href="/liberarytool/">图书馆占座小工具使用手册</a></li><li class="post-list-item"><a class="post-list-link" href="/linuxyoudaodict/">1.17 ubuntu下安装有道词典</a></li><li class="post-list-item"><a class="post-list-link" href="/goldendictsetting/">1.15 ubuntu下goldendict有道爬虫小程序</a></li><li class="post-list-item"><a class="post-list-link" href="/inputU18/">1.16 ubuntu18升级后部分应用不能中文输入的问题</a></li><li class="post-list-item"><a class="post-list-link" href="/vsftpdconf/">1.12 vsftpd 文件配置</a></li><li class="post-list-item"><a class="post-list-link" href="/peektool/">1.14 ubuntu16_18安装peek工具录制gif</a></li><li class="post-list-item"><a class="post-list-link" href="/sublimeIntro/">1.11 sublime text3插件介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/mysqlnotes/">1.13 mysql 笔记</a></li><li class="post-list-item"><a class="post-list-link" href="/python_future/">Module篇使用__future__</a></li><li class="post-list-item"><a class="post-list-link" href="/pythonHelp/">Python 帮助文档检索方法</a></li><li class="post-list-item"><a class="post-list-link" href="/decisiontree/">2.4 Decision tree</a></li><li class="post-list-item"><a class="post-list-link" href="/tiduxiajiang/">2.5 梯度下降和最小二乘法</a></li><li class="post-list-item"><a class="post-list-link" href="/webcustomization/">1.10 hexo网页定制</a></li><li class="post-list-item"><a class="post-list-link" href="/hexolocalimage/">1.9 hexo 实现本地图片加载</a></li><li class="post-list-item"><a class="post-list-link" href="/logisticRe/">2.12 logistic regression</a></li><li class="post-list-item"><a class="post-list-link" href="/LRN/">2.13 局部响应归一化</a></li><li class="post-list-item"><a class="post-list-link" href="/pusubeiyesi/">朴素贝叶斯法</a></li><li class="post-list-item"><a class="post-list-link" href="/jidasiranguji/">2.11 极大似然估计</a></li><li class="post-list-item"><a class="post-list-link" href="/rosSimulink/">ROS室内仿真环境</a></li><li class="post-list-item"><a class="post-list-link" href="/zuidashangmoxing/">2.1.2 最大熵模型</a></li><li class="post-list-item"><a class="post-list-link" href="/yingmaerkefu/">2.1.3 隐马尔科夫HMM</a></li><li class="post-list-item"><a class="post-list-link" href="/xianxinghuigui23/">2.3 线性回归</a></li><li class="post-list-item"><a class="post-list-link" href="/diguiyudiedai/">2.6 递归算法与迭代算法</a></li><li class="post-list-item"><a class="post-list-link" href="/shenjingwanguo/">2.7 神经网络浅学笔记</a></li><li class="post-list-item"><a class="post-list-link" href="/PrioritizedEP/">Prioritized Experience Replay</a></li><li class="post-list-item"><a class="post-list-link" href="/kjinglin/">K近邻算法</a></li><li class="post-list-item"><a class="post-list-link" href="/maerkefu21/">2.1 马尔科夫链</a></li><li class="post-list-item"><a class="post-list-link" href="/maerkefuyuce/">2.1.1 马尔科夫简单模型预测实战笔记</a></li><li class="post-list-item"><a class="post-list-link" href="/freelog/">简单的方法,获取在线付费设计log的方法</a></li><li class="post-list-item"><a class="post-list-link" href="/webdessignnotes/">网页设计基础笔记</a></li><li class="post-list-item"><a class="post-list-link" href="/gitIgnore/">Git中忽略提交项</a></li><li class="post-list-item"><a class="post-list-link" href="/matplotlibF/">3.3 matplotlib函数</a></li><li class="post-list-item"><a class="post-list-link" href="/usefullwebsite/">值得收藏的网站</a></li><li class="post-list-item"><a class="post-list-link" href="/stm32chuankou/">STM32串口通信配置(USART1+USART2+USART3+UART4)</a></li><li class="post-list-item"><a class="post-list-link" href="/stm32fuwei/">STM32复位及通过函数判断是何种条件出发的复位</a></li><li class="post-list-item"><a class="post-list-link" href="/tkinternote/">python之tkinter入坑Pack</a></li><li class="post-list-item"><a class="post-list-link" href="/cplusMFC/">C++ MFC 界面实现套接字(socket) 通信</a></li><li class="post-list-item"><a class="post-list-link" href="/helloindex/">Hello World</a></li><li class="post-list-item"><a class="post-list-link" href="/pytorchNotes/">3.1 PyTorch 学习</a></li><li class="post-list-item"><a class="post-list-link" href="/numpyFunc/">3.4 numpy函数</a></li><li class="post-list-item"><a class="post-list-link" href="/DLindex/">第三章 深度学习框架学习</a></li><li class="post-list-item"><a class="post-list-link" href="/hexoDeploy/">Hexo 每次写好后deploy博客</a></li><li class="post-list-item"><a class="post-list-link" href="/hexoHello/">Hello World</a></li><li class="post-list-item"><a class="post-list-link" href="/sublime_text3/">ubuntu 中安装sublime_text3</a></li><li class="post-list-item"><a class="post-list-link" href="/vultrFreenom/">vultr+freenom实现主机域名的绑定</a></li><li class="post-list-item"><a class="post-list-link" href="/thefirstarticle/">第一篇博文</a></li><li class="post-list-item"><a class="post-list-link" href="/installplugbychrome/">解决Chrome67版本以后不能离线安装插件的情况</a></li><li class="post-list-item"><a class="post-list-link" href="/gitpush/">git 推送新的文章到github其他分支上</a></li><li class="post-list-item"><a class="post-list-link" href="/searchskill/">搜索技巧</a></li><li class="post-list-item"><a class="post-list-link" href="/fanqiangss/">ubuntu18 正确 安装ShadowSocket</a></li><li class="post-list-item"><a class="post-list-link" href="/pyqttkinter/">python Tkinter 界面实现套接字(socket) 通信</a></li><li class="post-list-item"><a class="post-list-link" href="/DLenviremment/">1.8 深度学习环境搭建</a></li><li class="post-list-item"><a class="post-list-link" href="/gitbookplugset/">1.7 gitbook 插件等相关设置</a></li><li class="post-list-item"><a class="post-list-link" href="/doubleSysRepair/">1.6 win10+ubuntu双系统修复ubuntu启动引导</a></li><li class="post-list-item"><a class="post-list-link" href="/pytorchMNIST/">3.1.2 Pytorch 之MNIST手写字识别分类.md</a></li><li class="post-list-item"><a class="post-list-link" href="/tfComFunction/">3.2 tensorflow1.x常用函数使用</a></li><li class="post-list-item"><a class="post-list-link" href="/tfMNIST/">3.2.1 tensorflow之MNIST</a></li><li class="post-list-item"><a class="post-list-link" href="/linuxcommontool/">1.5 Ubuntu下常用工具软件配置安装</a></li><li class="post-list-item"><a class="post-list-link" href="/latexnotes/">1.4 Latex 排版使用笔记</a></li><li class="post-list-item"><a class="post-list-link" href="/gitcommand/">1.3 Ubuntu18 git命令使用总结</a></li><li class="post-list-item"><a class="post-list-link" href="/installgitbook/">1.2 Ubuntu18安装Gitbook</a></li><li class="post-list-item"><a class="post-list-link" href="/youdaoconf/">1.1 Ubuntu18下有道词典的配置</a></li></ul><script></script></div><footer id="footer"><div class="outer"><div id="footer-info"><div class="footer-left"><i class="fa fa-copyright"></i> 2018-2024 Porter Pan</div><div class="footer-right"><a href="http://hexo.io/" target="_blank" title="Hexo">Hexo</a> Theme <a href="https://github.com/MOxFIVE/hexo-theme-yelee" target="_blank" title="hexo-theme-yelee">Yelee</a> by Porter <i class="fa fa-heart animated infinite flash"></i></div></div><div class="visit"><span id="busuanzi_container_site_pv" style="display:none"><span id="site-visit" title="本站到访数"><i class="fa fa-user" aria-hidden="true"></i><span id="busuanzi_value_site_uv"></span> </span></span><span>| </span><span id="busuanzi_container_page_pv" style="display:none"><span id="page-visit" title="本页阅读量"><i class="fa fa-eye animated infinite pulse" aria-hidden="true"></i><span id="busuanzi_value_page_pv"></span></span></span></div></div></footer></div><script data-main="/js/main.js" src="//cdn.bootcss.com/require.js/2.2.0/require.min.js"></script><script>$(document).ready(function(){var a,c,d;-1<window.navigator.userAgent.indexOf("iPad")||"none"===$(".left-col").css("display")?(a=["#9db3f4","#414141","#e5a859","#f5dfc6","#c084a0","#847e72","#cd8390","#996731"],c=Math.ceil(Math.random()*(a.length-1)),$("body").css({"background-color":a[c],"background-size":"cover"})):(d="url(/background/bg-x.jpg)".replace(/x/gi,Math.ceil(5*Math.random())),$("body").css({background:d,"background-attachment":"fixed","background-size":"cover"}))})</script><script type="text/javascript">!function(e,a,t,n,g,c){e.GoogleAnalyticsObject=n,e.ga=e.ga||function(){(e.ga.q=e.ga.q||[]).push(arguments)},e.ga.l=+new Date,g=a.createElement(t),c=a.getElementsByTagName(t)[0],g.async=1,g.src="//www.google-analytics.com/analytics.js",c.parentNode.insertBefore(g,c)}(window,document,"script","ga"),ga("create","UA-132871432-3","auto"),ga("send","pageview")</script><script type="text/x-mathjax-config">MathJax.Hub.Config({
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
processEscapes: true,
skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
}
});
MathJax.Hub.Queue(function() {
var all = MathJax.Hub.getAllJax(), i;
for(i=0; i < all.length; i += 1) {
all[i].SourceElement().parentNode.className += ' has-jax';
}
});</script><script src="//cdn.bootcss.com/mathjax/2.6.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><div class="scroll" id="scroll"><a href="#" title="返回顶部"><i class="fa fa-arrow-up"></i></a> <a href="#comments" onclick="load$hide()" title="查看评论"><i class="fa fa-comments-o"></i></a> <a href="#footer" title="转到底部"><i class="fa fa-arrow-down"></i></a></div><script>var x,oOpenInNew={archives:".archive-article-title",miniArchives:"a.post-list-link",menu:".header-menu a",friends:"#js-friends a",socail:".social a"};for(x in oOpenInNew)$(oOpenInNew[x]).attr("target","_blank")</script><script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>