Question on pp.neighbors command

If use this Python code
x= np.ramdon.randn(100,5)
adata= sc.AnnData(x)
adata.obsm[‘emb’]= x

sc.pp.neighbors(adata, n_neighbors= 51, use_rep= ‘emb’, knn= True, method= ‘umap’, metric= ‘minkowski’, metric_kwds= {‘p’: 0.1}, copy= False, random_state= 1969)

I noticed that distances and connectivities both have shape of (100,100):
np.shape(adata.obsp[‘distances’])
np.shape(adata.obsp[‘connectivities’])

I was expecting an output of size (100,51). Can someone explain how the pp.neighbors computes the size of its output?

Many thanks,
Ivan

Hi Ivan,

The neighbors function returns the distances and weighted connectivities of each data point pairwise, so the output will be a square matrix with the number of rows and columns equaling the number of observations. Each entry in either matrix represents the distance or weighted connectivity between data point i and j. So querying distance_matrix[i,j] will return the neighbors distance between observation i and j.

Hope this clarifies it a bit,
Chuck

Thanks for your clarification!

1 Like