上一篇文章讲了k近邻法,以及使用kd树构造数据结构,使得提高最近邻点搜索效率,但是这在数据点N 远大于 2^n 时可以有效的降低算法复杂度,n为数据点的维度,否则,由于需要向上回溯比较距离,使得实际效率总是很低(接近线性扫描)。比如SIFT特征矢量128维,SURF特征矢量64维,维度都比较大,N 远大于 2^n 可能无法满足。此外,由于每个最近邻点都需要回溯到根节点才算结束,那么,在获取k个近邻点时,必然存在大量不必要的回溯点,这些都需要另寻其他查询方法。

一个简单的改进思路就是将“查询路径”上的结点进行排序,如按各自分割超平面(也称bin)与查询点的距离排序,也就是说,回溯检查总是从优先级最高(Best Bin)的树结点开始

所以这篇文章讨论这种改进的方法 Best Bin First(BBF)。

主要思想是,使用一个优先列表,按节点与目标点的距离排序,从优先列表中取出第一项(当前距离最近的)节点,按照某个规则决定是访问其左子节点或者右子节点,同时将另一个子节点(如果存在)存储到优先列表中,循环以上操作,直到优先列表为空。

步骤:

  1. 将根结点store in 优先列表Priority中。声明一个最近节点对象nearest,先令其指向根节点,一个当前节点对象current
  2. 取出第一项,根据访问规则,访问其左子节点或者右子节点,并令current指向它,然后将另一个子节点store in Priority中,递归向下,直到遇到叶节点,此时,比较current和nearest哪个距离目标节点更近,更新nearest,
  3. 如果Priority中还有项,则继续步骤2,否则返回nearest,此为最近邻点

由于比较简单,这里不再详述,直接给出代码。

     private List<Tuple<TreeNode, double>> _priorities = new List<Tuple<TreeNode, double>>();
/// <summary>
/// 按priority升序排序插入
/// </summary>
/// <param name="node"></param>
/// <param name="priority"></param>
private void InsertByPriority(TreeNode node, double priority)
{
if(_priorities.Count == )
{
_priorities.Add(new Tuple<TreeNode, double>(node, priority));
}
else
{
for(int i = ; i < _priorities.Count; i++)
{
if(_priorities[i].Item2 >= priority)
{
_priorities.Insert(i, new Tuple<TreeNode, double>(node, priority));
break;
}
}
}
}
private double GetPriority(TreeNode node, Point p, int axis) => Math.Abs(node.point.vector[axis] - p.vector[axis]); public Point BBFSearchNearestNode(Point p)
{
var rootPriority = GetPriority(root, p, root.axis);
InsertByPriority(root, rootPriority);
var nearest = root; TreeNode topNode = null; // 优先级最高的节点
TreeNode curNode = null;
while(_priorities.Count > )
{
topNode = _priorities[].Item1;
_priorities.RemoveAt(); while(topNode != null)
{
if(topNode.left != null || topNode.right != null)
{
var axis = topNode.axis;
if(p.vector[axis] <= topNode.point.vector[axis])
{
// wanna to go down left child node
if(topNode.right != null) // 将右子节点添加到优先列表
{
InsertByPriority(topNode.right, GetPriority(topNode.right, p, axis));
}
topNode = topNode.left;
}
else
{
// wanna to go down right child node
if(topNode.left != null)
{
InsertByPriority(topNode.left, GetPriority(topNode.left, p, axis));
}
topNode = topNode.right;
}
}
else
{
curNode = topNode;
topNode = null;
} if(curNode != null && p.Distance(curNode.point) < p.Distance(nearest.point)) // find a nearer node
{
nearest = curNode;
}
}
}
return nearest.point;
}

上面的代码仅仅是返回了最近的那一个点,如果要返回k个近邻点,则只需对上面代码稍作修改,将 上面每次的current保存到一个按距离排序的列表中,这样前k个点就是所求的k近邻点,代码如下:

        /// <summary>
/// 最大检测次数
/// </summary>
public int max_nn_chks = 0x1000;
/// <summary>
/// 搜索k近邻点
/// </summary>
/// <param name="p"></param>
/// <param name="k"></param>
/// <returns></returns>
public List<TreeNode> BBFSearchKNearest(Point p, int k)
{
var list = new List<BBFData>(); //
var pq = new MinPQ();
pq.insert(new PQNode(root, ));
int t = ;
while(pq.nodes.Count > && t < max_nn_chks)
{
var expl = pq.pop_min_default().data;
expl = Explore2Leaf(expl, p, pq); var bbf = new BBFData(expl, expl.point.Distance(p));
insert(list, k, bbf); t++;
}
return list.Select(l => l.data).ToList();
}
/// <summary>
/// 向下访问叶节点,并将slide添加到优先列表中
/// </summary>
/// <param name="node"></param>
/// <param name="p"></param>
/// <param name="pq"></param>
/// <returns></returns>
private TreeNode Explore2Leaf(TreeNode node, Point p, MinPQ pq)
{
TreeNode unexpl;
var expl = node;
TreeNode prev;
while(expl != null && (expl.left != null || expl.right != null))
{
prev = expl;
var axis = expl.axis;
var val = expl.point.vector[axis]; if(p.vector[axis] <= val)
{
unexpl = expl.right;
expl = expl.left;
}
else
{
unexpl = expl.left;
expl = expl.right;
}
if(unexpl != null)
{
pq.insert(new PQNode(unexpl, Math.Abs(val - p.vector[axis])));
}
if(expl == null)
{
return prev;
}
}
return expl;
}
/// <summary>
/// 将节点按距离插入列表中
/// </summary>
/// <param name="list"></param>
/// <param name="k"></param>
/// <param name="bbf"></param>
private void insert(List<BBFData> list, int k, BBFData bbf)
{
if(list.Count == )
{
list.Add(bbf);
return;
} int ret = ;
int oldCount = list.Count;
var last = list[list.Count - ];
var df = bbf.d;
var dn = last.d;
if(df >= dn) // bbf will be appended to list
{
if(oldCount == k) // already has k nearest neighbors, nothing should be done
{
return;
}
list.Add(bbf); // append directively
return;
} // bbf will be inserted into list internally if(oldCount < k)
{
// suppose bbf be inserted at idx1, all elements after idx1 should be moved 1 backward respectively
// first we move the last element
list.Add(last);
}
// from backer to former, move related elements
int i = oldCount - ;
while(i > -)
{
if (list[i].d <= df)
break; list[i + ] = list[i]; // move backward
i--;
}
i++;
list[i] = bbf;
}

其中用到的辅助类如下:

    public class BBFData
{
public TreeNode data;
/// <summary>
/// 节点与目标点的距离
/// </summary>
public double d; public BBFData(TreeNode data, double d)
{
this.data = data;
this.d = d;
}
} public class PQNode
{
public TreeNode data;
/// <summary>
/// 目标点与当前节点的超平面的距离
/// </summary>
public double d; public PQNode(TreeNode data, double d)
{
this.data = data;
this.d = d;
}
} public class MinPQ
{
public List<PQNode> nodes;
// 将节点插入优先列表中
public void insert(PQNode node)    
{
nodes.Add(node); int i = nodes.Count - ;
int p = parent(i);
PQNode tmp;
while(i > && p >= && nodes[i].d < nodes[p].d)
{
tmp = nodes[p];
nodes[p] = nodes[i];
nodes[i] = tmp;
i = p;
p = parent(i);
}
} public PQNode get_min_default() => nodes.Count > ? nodes[] : null;
public PQNode pop_min_default()
{
if (nodes.Count == ) return null; var ret = nodes[];
nodes[] = nodes[nodes.Count - ];
nodes.RemoveAt(nodes.Count - );
restore_minpq_order(, nodes.Count); return ret;
} private void restore_minpq_order(int i, int n)
{
int l = left(i);
int r = right(i);
int min = i; if (l < n && nodes[l].d < nodes[i].d)
min = l;
if (r < n && nodes[r].d < nodes[min].d)
min = r;
if(min != i)
{
var tmp = nodes[min];
nodes[min] = nodes[i];
nodes[i] = tmp;
}
} public static int parent(int i) => (i - ) / ;
public static int right(int i) => * (i + );
public static int left(int i) => * i + ;
}

注意,上面代码中,优先列表是使用最小堆实现。

后记:

以上代码搜索k近邻,仅仅是一定程度上得到最大可能的近似k近邻,因为有max_nn_chks的检测次数限制。

假设没有这个限制,则实际上应该对全部训练数据集中的数据点做检测的,而不用k-d树结构存储数据集时也是要检测全部数据点的,不过,两者区别还是有的,也许使用了k-d树后,由于是从优先列表中选择数据点进行检测,导致insert结果列表的操作平均时间复杂度低(当然了,这些我此时并没有去仔细想),而且使用了k-d树后,在数据集数量很大时,需要max_nn_chks限制,此时近似k近邻还是比不使用k-d树得到的近似k近邻更加准确吧(概率意义上)^^!

k近邻法(二)的更多相关文章

  1. 统计学习方法与Python实现(二)——k近邻法

    统计学习方法与Python实现(二)——k近邻法 iwehdio的博客园:https://www.cnblogs.com/iwehdio/ 1.定义 k近邻法假设给定一个训练数据集,其中的实例类别已定 ...

  2. K近邻法(KNN)原理小结

    K近邻法(k-nearst neighbors,KNN)是一种很基本的机器学习方法了,在我们平常的生活中也会不自主的应用.比如,我们判断一个人的人品,只需要观察他来往最密切的几个人的人品好坏就可以得出 ...

  3. 机器学习PR:k近邻法分类

    k近邻法是一种基本分类与回归方法.本章只讨论k近邻分类,回归方法将在随后专题中进行. 它可以进行多类分类,分类时根据在样本集合中其k个最近邻点的类别,通过多数表决等方式进行预测,因此不具有显式的学习过 ...

  4. scikit-learn K近邻法类库使用小结

    在K近邻法(KNN)原理小结这篇文章,我们讨论了KNN的原理和优缺点,这里我们就从实践出发,对scikit-learn 中KNN相关的类库使用做一个小结.主要关注于类库调参时的一个经验总结. 1. s ...

  5. 学习笔记——k近邻法

    对新的输入实例,在训练数据集中找到与该实例最邻近的\(k\)个实例,这\(k\)个实例的多数属于某个类,就把该输入实例分给这个类. \(k\) 近邻法(\(k\)-nearest neighbor, ...

  6. k近邻法

    k近邻法(k nearest neighbor algorithm,k-NN)是机器学习中最基本的分类算法,在训练数据集中找到k个最近邻的实例,类别由这k个近邻中占最多的实例的类别来决定,当k=1时, ...

  7. 机器学习中 K近邻法(knn)与k-means的区别

    简介 K近邻法(knn)是一种基本的分类与回归方法.k-means是一种简单而有效的聚类方法.虽然两者用途不同.解决的问题不同,但是在算法上有很多相似性,于是将二者放在一起,这样能够更好地对比二者的异 ...

  8. 《统计学习方法》笔记三 k近邻法

    本系列笔记内容参考来源为李航<统计学习方法> k近邻是一种基本分类与回归方法,书中只讨论分类情况.输入为实例的特征向量,输出为实例的类别.k值的选择.距离度量及分类决策规则是k近邻法的三个 ...

  9. k近邻法(kNN)

    <统计学习方法>(第二版)第3章 3 分类问题中的k近邻法 k近邻法不具有显式的学习过程. 3.1 算法(k近邻法) 根据给定的距离度量,在训练集\(T\)中找出与\(x\)最邻近的\(k ...

随机推荐

  1. L12 Transformer

    Transformer 在之前的章节中,我们已经介绍了主流的神经网络架构如卷积神经网络(CNNs)和循环神经网络(RNNs).让我们进行一些回顾: CNNs 易于并行化,却不适合捕捉变长序列内的依赖关 ...

  2. CSS 中你应该了解的 BFC

    我们常说的文档流其实分为定位流.浮动流和普通流三种.而普通流其实就是指BFC中的FC.FC是formatting context的首字母缩写,直译过来是格式化上下文,它是页面中的一块渲染区域,有一套渲 ...

  3. 适合新手练习的Python项目有哪些?Python爬虫用什么框架比较好?

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. Python爬虫一般用什么框架比较好?一般来讲,只有在遇到比较大型的需求时 ...

  4. 常见web漏洞整理之进击吧xss!!!

    XSS在线测试环境: http://xss-quiz.int21h.jp/ https://brutelogic.com.br/xss.php 这两个站对xss的理解很有帮助!!! 参考链接: htt ...

  5. python-用户输入和while循环

    函数input() 比较大小要同类型: age=iput() 21 age=int(age) age>=10 true prompt = "If you tell us who you ...

  6. Springboot:IDEA重调安装依赖窗口(二)

    Settings-Plugins 搜索Editstarters: 安装完插件 重启idea: 查看安装是否成功: 在pom.xml 右键: 选择热部署依赖 点击ok进行自动装配: 热部署依赖环境已经配 ...

  7. MySQL笔记总结-TCL语言

    TCL语言 事务 一.含义 事务控制语言 Transaction Control Language 事务:一条或多条sql语句组成一个执行单位,一组sql语句要么都执行要么都不执行 二.特点(ACID ...

  8. python中文语料分词处理,按字或者词cut_sentence

    cut_sentence.py import string import jieba import jieba.posseg as psg import logging #关闭jieba日制 jieb ...

  9. <cstring>中常用的两个函数memset()和memcpy()

    <cstring>是c++对c中的<string.h>进行了重写,这两个头文件中的函数用法是一样的,所以在用的时候包含哪个头文件都行.下面介绍一下 <cstring> ...

  10. 超详细步骤---Linux下的最新Git版本安装

    原文地址:https://blog.csdn.net/u010887744/article/details/53957613 [标注大头] 1.查看当前git版本:git --version 查看最新 ...