命题一:
已知的1000个整数的数组,给定一个整数,要求查证是否在数组中出现?
命题二:
已知1000个整数的数组,给定一个整数,要求查找数组中与之最接近的数字?
命题三:
已知1000个Point(包含X与Y坐标)结构的数组,给定一个Point,要求查找数组中与之最接近(比如:欧氏距离最短)的点。
命题四:
已知1,000,000个向量,每个向量为128维;给定一个向量,要求查找数组中与之最接近的K个向量
- 对于命题一,如果不考虑桶式、哈希等方式,常用的方法应该是排序后,使用折半查找。
- 对于命题二,与命题一类似,比较折半查找得出的结果,以及附近的各一个元素,即可。整个过程相当于是把这个包含1000个数组的数据结构做成一颗二叉树,最后只需比较叶子节点与其父节点即可。
- 对于命题三、四其中命题三和四就是所谓的Nearest Neighbor问题。一种近似解决的方法就是KD-TREE
高维向量的KNN检索问题,在图像等多媒体内容搜索中是相当关键的。关于高维向量的讨论,网上资料比较少;在此,我将一些心得分享给大家。
与二叉树相比,KD-TREE也采用类似的划分方式,只不过树中的各节点均是高维向量,因此划分的方式,采用随机或指定的方式选取一个维度,在该指定维度上进行划分;整体的思想就是采用多个超平面对数据集空间进行两两切分,这一点,有点类似于数据挖掘中的决策树。
一个运用KD-TREE分割二维平面的DEMO如下:
KD-Tree build的代码如下:
private ClusterKDTree(Clusterable[] points, int height, boolean randomSplit){
if ( points.length == 1 ){
cluster = points[0];
}
else {
splitIndex = chooseSplitDimension//选取切分维度
(points[0].getLocation().length,height,randomSplit);
splitValue = chooseSplit(points,splitIndex);//选取切分值
Vector<Clusterable> left = new Vector<Clusterable>();
Vector<Clusterable> right = new Vector<Clusterable>();
for ( int i = 0; i < points.length; i++ ){
double val = points[i].getLocation()[splitIndex];
if ( val == splitValue && cluster == null ){
cluster = points[i];
}
else if ( val >= splitValue ){
right.add(points[i]);
} else {
left.add(points[i]);
}
}
if ( right.size() > 0 ){
this.right = new ClusterKDTree(right.toArray(new
Clusterable[right.size()]),
randomSplit ? splitIndex : height+1, randomSplit);
}
if ( left.size() > 0 ){
this.left = new ClusterKDTree(left.toArray(new
Clusterable[left.size()]),randomSplit ? splitIndex : height+1,
randomSplit);
}
}
}
private int chooseSplitDimension(int dimensionality,int height,boolean random){
if ( !random ) return height % dimensionality;
int rand = r.nextInt(dimensionality);
while ( rand == height ){
rand = r.nextInt(dimensionality);
}
return rand;
}
private double chooseSplit(Clusterable points[],int splitIdx){
double[] values = new double[points.length];
for ( int i = 0; i < points.length; i++ ){
values[i] = points[i].getLocation()[splitIdx];
}
Arrays.sort(values);
return values[values.length/2];//选取中间值以保持树的平衡
}
构建完一颗KD-TREE之后,如何使用它来做KNN检索呢?我用下面的图来表示(20s的GIF动画):
使用KD-TREE,经过一次二分查找可以获得Query的KNN(最近邻)贪心解,代码如下:
private Clusterable restrictedNearestNeighbor(Clusterable point, SizedPriorityQueue<ClusterKDTree> values){
if ( splitIndex == -1 ) {
return cluster; //已近到叶子节点
}
double val = point.getLocation()[splitIndex];
Clusterable closest = null;
if ( val >= splitValue && right != null || left == null ){
//沿右边路径遍历,并将左边子树放进队列
if ( left != null ){
double dist = val - splitValue;
values.add(left,dist);
}
closest = right.restrictedNearestNeighbor(point,values);
}
else if ( val < splitValue && left != null || right == null ) {
//沿左边路径遍历,并将右边子树放进队列
if ( right != null ){
double dist = splitValue - val;
values.add(right,dist);
}
closest = left.restrictedNearestNeighbor(point,values);
}
//current distance of the 'ideal' node
double currMinDistance = ClusterUtils.getEuclideanDistance(closest,point);
//check to see if the current node we've backtracked to is closer
double currClusterDistance = ClusterUtils.getEuclideanDistance(cluster,point);
if ( closest == null || currMinDistance > currClusterDistance ){
closest = cluster;
currMinDistance = currClusterDistance;
}
return closest;
}
事实上,仅仅一次的遍历会有不小的误差,因此采用了一个优先级队列来存放每次决定遍历走向时,另一方向的节点。SizedPriorityQueue代码的实现,可参考我的另一篇文章:
http://grunt1223.iteye.com/blog/909739
一种减少误差的方法(BBF:Best Bin First)是回溯一定数量的节点:
public Clusterable restrictedNearestNeighbor(Clusterable point, int numMaxBinsChecked){
SizedPriorityQueue<ClusterKDTree> bins = new SizedPriorityQueue<ClusterKDTree>(50,true);
Clusterable closest = restrictedNearestNeighbor(point,bins);
double closestDist = ClusterUtils.getEuclideanDistance(point,closest);
//System.out.println("retrieved point: " + closest + ", dist: " + closestDist);
int count = 0;
while ( count < numMaxBinsChecked && bins.size() > 0 ){
ClusterKDTree nextBin = bins.pop();
//System.out.println("Popping of next bin: " + nextBin);
Clusterable possibleClosest = nextBin.restrictedNearestNeighbor(point,bins);
double dist = ClusterUtils.getEuclideanDistance(point,possibleClosest);
if ( dist < closestDist ){
closest = possibleClosest;
closestDist = dist;
}
count++;
}
return closest;
}
可以用如下代码进行测试:
public static void main(String args[]){
Clusterable clusters[] = new Clusterable[10];
clusters[0] = new Point(0,0);
clusters[1] = new Point(1,2);
clusters[2] = new Point(2,3);
clusters[3] = new Point(1,5);
clusters[4] = new Point(2,5);
clusters[5] = new Point(1,1);
clusters[6] = new Point(3,3);
clusters[7] = new Point(0,2);
clusters[8] = new Point(4,4);
clusters[9] = new Point(5,5);
ClusterKDTree tree = new ClusterKDTree(clusters,true);
//tree.print();
Clusterable c = tree.restrictedNearestNeighbor(new Point(4,4),1000);
System.out.println(c);
}
- 大小: 24.5 KB
- 大小: 112.3 KB
分享到:
相关推荐
-kdtree_nearest_neighbor:最近的邻居查询(一个或多个点) -kdtree_k_nearest_neighbors:单个查询点的kNN -kdtree_range_query:矩形范围查询 -kdtree_ball_query:查询距离某点有距离增量的样本
2.最近邻域搜索(Nearest-Neighbor Lookup)3.构造方法4.案例分析4.1 树结构的建立4.2 最近领域的搜索4.2.1 查找点(2.1,3.1)4.2.2 查找点(2,4.5)5.总结KD-Tree案例癌症的数据建立模型 KNN算法的模型提升KD-Tree k...
c++实现的KNN库:建立高维度的K-d tree,实现K邻域搜索,最小半径搜索
of the k nearest neighbors is then pooled by means of Dempster's rule of combination. This approach provides a global treatment of such issues as ambiguity and distance rejection, and imperfect ...
c++实现的一个类库,用于数字几何处理 A Library for Approximate Nearest Neighbor Searching 1 源码 2.win下测试vs工程 3.pdf文档
Instance-based learning: nearest neighbours Data structures for multidimensional data kD-trees 关于kd-tree的基本教程,作者是Steve Renals
网站上的图片带有“ fulltest.m”字样此实现提供以下功能: - kdtree_build: kd 树构造 O( n log^2(n) ) - kdtree_delete:释放由 kdtree 分配的内存- kdtree_nearest_neighbor:最近邻查询(针对一个或多个点) - ...
k-nearest-neighbors-from-global-to-local
基于K-Nearest Neighbor和神经网络的糖尿病分类研究.pdf
这是一个小而有效的程序来执行的K -近邻搜索算法,此算法利用JIT 理论加速循环,比向量化有效解决了大量数据的精度问题。甚至比kd-tree效果要佳。 K-nearest neighbor search已经广泛应用在科学与工程上,比如模式...
基于K近邻的聚类有效性指标,肖宇,于剑,聚类有效性是聚类分析中的一个重要问题。这篇文章中提出了一个基于K近邻的聚类有效性指标,NNI。文献中已提出好多聚类有效性指标��
主要介绍了Python机器学习k-近邻算法(K Nearest Neighbor),结合实例形式分析了k-近邻算法的原理、操作步骤、相关实现与使用技巧,需要的朋友可以参考下
深度学习资料
coding for k nearest neighbor algorithm
Samet Hanan经典文章Trans on PAMI,K近邻模式识别分类器新方法。
Code and Data for K-nearest neighbor classification.
Nearest Neighbor Pattern Classification. 1967. 欢迎下载。
该资源是Cover T和Hart P提出的一种基本分类与回归方法的开山之作,简称KNN,有兴趣的朋友请自行下载,没有积分的朋友,请联系博主,谢谢大家的支持。
基于证据理论的盲隐写分析。Evidence-theoretic k nearest neighbor rule