Nearest neighbor graph | 近邻图

最近在开发一套自己的单细胞分析方法,所以copy paste事业有所停顿。

 

实例:

R eNetIt v0.1-1

data(ralu.site)
# Saturated spatial graph
 sat.graph <- knn.graph(ralu.site, row.names=ralu.site@data[,"SiteName"])
  head(sat.graph@data)
 
 # Distanced constrained spatial graph
 dist.graph <- knn.graph(ralu.site, row.names=ralu.site@data[,"SiteName"], max.dist = 5000)

 par(mfrow=c(1,2))	
plot(sat.graph, col="grey")
  points(ralu.site, col="red", pch=20, cex=1.5)
     box()
     title("Saturated graph")	
plot(dist.graph, col="grey")
  points(ralu.site, col="red", pch=20, cex=1.5)
     box()
     title("Distance constrained graph")

  

  

 

一下来自wiki

The nearest neighbor graph (NNG) for a set of n objects P in a metric space (e.g., for a set of points in the plane with Euclidean distance) is a directed graph with P being its vertex set and with a directed edge from p to q whenever q is a nearest neighbor of p (i.e., the distance from p to q is no larger than from p to any other object from P).[1]

NNG图,在多维空间里我有很多个点,如上例,在17维空间里,我有31个点,一个常见的距离度量就是欧氏距离,NNG是有方向的,因为q是p的邻居并不代表p是q的邻居!

In many discussions, the directions of the edges are ignored and the NNG is defined as an ordinary (undirected) graph. However, the nearest neighbor relation is not a symmetric one, i.e., p from the definition is not necessarily a nearest neighbor for q.

In some discussions, in order to make the nearest neighbor for each object unique, the set P is indexed and in the case of a tie the object with, e.g., the largest index is taken for the nearest neighbor.[2]

The k-nearest neighbor graph (k-NNG) is a graph in which two vertices p and q are connected by an edge. if the distance between p and q is among the k-th smallest distances from p to other objects from P. The NNG is a special case of the k-NNG, namely, it is the 1-NNG. k-NNGs obey a separator theorem: they can be partitioned into two subgraphs of at most n(d + 1)/(d + 2) vertices each by the removal of O(k1/dn1 − 1/d) points.[3]

在k-NNG里,就不是最近邻了,而是考虑k-th,就是把k-th内的点都当做邻居。

Another special case is the (n − 1)-NNG. This graph is called the farthest neighbor graph (FNG).

如果k=n-1,那么就是FNG图。

In theoretical discussions of algorithms a kind of general position is often assumed, namely, the nearest (k-nearest) neighbor is unique for each object. In implementations of the algorithms it is necessary to bear in mind that this is not always the case.

NNGs for points in the plane as well as in multidimensional spaces find applications, e.g., in data compressionmotion planning, and facilities location. In statistical analysis, the nearest-neighbor chain algorithm based on following paths in this graph can be used to find hierarchical clusterings quickly. Nearest neighbor graphs are also a subject of computational geometry.

 

### k-近邻图的概念及其实现 #### 1. k-近邻图的定义 k-近邻图(k-Nearest Neighbor Graph, k-NNG)是一种表示数据点之间关系的图形结构。具体来说,它是一个无向加权图 \( G(V,E) \),其中节点集 \( V \) 表示数据点集合,边集 \( E \) 则由每一点与其最近的 \( k \) 个邻居之间的连接构成[^1]。 #### 2. 构建k-近邻图的过程 构建k-近邻图通常涉及以下几个方面: - **计算距离矩阵** 首先需要为数据集中所有的点两两计算相似度或距离。常用的欧几里得距离可以用来衡量两个点间的接近程度。假设数据集大小为 \( n \),则此阶段会生成一个 \( n \times n \) 的距离矩阵[^4]。 - **选取前k个邻居** 对于每一个数据点,在上述的距离矩阵中找到离自己最近的 \( k \) 个其他点,并记录它们以及对应的权重(即距离倒数或其他形式)。这一步决定了哪些点会被连入当前点所代表的节点之中[^3]。 - **创建稀疏矩阵/列表存储结构** 使用某种高效的数据结构保存这些信息非常重要。一种常见的方式是以稀疏矩阵的形式储存整个图的信息;另一种方式则是采用邻接表来表达每个顶点直接相连的所有其它顶点及关联权重。 #### 3. 数据结构的选择 为了有效地管理和操作大规模的k-近邻图,选择合适的数据结构至关重要: - **邻接矩阵** 如果图较为稠密或者规模较小,则可以直接利用二维数组作为邻接矩阵来进行管理。然而当面对高维空间下的大数据集时,这种方法可能会因为内存占用过高而变得不可行。 - **压缩稀疏列(CSC)/压缩稀疏行(CSR)** 当处理大型但相对稀疏的图表时,推荐使用CSR/CSC格式的稀疏矩阵技术。这种方案仅需保留非零元素的位置和数值即可完成对原始密集型阵列的有效替代。 - **哈希映射与链表组合** 另外也可以考虑运用哈希表配合单向或多向链表的方式来动态维护各个结点间的关系网络。这种方式特别适合那些不断变化更新需求场景下快速增删改查的需求。 ```python import numpy as np from scipy.spatial import distance_matrix from sklearn.neighbors import kneighbors_graph # 假设我们有如下一些随机分布的数据点 data_points = np.random.rand(100, 2) # 计算所有点之间的相互距离 dist_mat = distance_matrix(data_points, data_points) # 调用sklearn库函数自动生成k-nn graph (这里取k=5) A_sparse = kneighbors_graph(data_points, 5, mode='distance') print(A_sparse.toarray()) # 输出完整的邻接矩阵视图 ``` 以上代码片段展示了如何借助Python中的科学计算工具包轻松地从一组散乱排列的空间坐标出发建立相应的k-NN图模型。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值