# Sc.pp.neighbours not always getting requested n\_neighbors = 5

**URL:** <https://discourse.scverse.org/t/sc-pp-neighbours-not-always-getting-requested-n-neighbors-5/423>\
**Category:** scanpy\
**Created:** [April 26, 2022, 9:30am UTC](https://discourse.scverse.org/t/sc-pp-neighbours-not-always-getting-requested-n-neighbors-5/423 "2022-04-26T09:30:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ViriatoII](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/viriatoii/32/212_2.png) [@ViriatoII](https://discourse.scverse.org/u/ViriatoII)\
**Post date:** [April 26, 2022, 9:30am UTC](https://discourse.scverse.org/t/sc-pp-neighbours-not-always-getting-requested-n-neighbors-5/423/1 "2022-04-26T09:30:45Z")

</div>

Hey,

I have realized that sc.pp.neighbours(adata, n\_neighbors = 5) does not create 4 neighbours for all cells, some of them are empty:

> adata\_train.obsp[‘distances’].tolil().rows

```auto
array([list([223, 280, 316, 5791]), list([3877, 5899, 7766, 7807]),
       list([165, 304, 423, 713]), ..., list([]),
       list([94, 865, 7077, 7666]), list([])], dtype=object)

```

This ends up in an error when using sc.tl.ingest(). The same happens with other number for n\_neighbors). Is this a bug or a feature? My dataset only has 92 genes. How can I avoid this?

I also wrote here: [Ingest won't integrate datasets of different lengths · Issue #2085 · scverse/scanpy · GitHub](https://github.com/scverse/scanpy/issues/2085)

Cheers,

---

<div class="post-metadata">

**Author:** ![ViriatoII](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/viriatoii/32/212_2.png) [@ViriatoII](https://discourse.scverse.org/u/ViriatoII)\
**Post date:** [April 27, 2022, 9:13am UTC](https://discourse.scverse.org/t/sc-pp-neighbours-not-always-getting-requested-n-neighbors-5/423/2 "2022-04-27T09:13:35Z")

</div>

I found what causes this, the closest neighbours are not reported if the distance is 0. That means duplicated rows cause empty lists to be returned. Even cases of almost duplicated rows create the same effect, like these two cells (yes, only dealing with a couple dozen genes and very low gene counts):

```auto
>adata.X[[1662, 3578]]
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 8.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 8.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0.]], dtype=float32)

>adata.obs.iloc[[1662, 3578]]
	celltype n_counts n_genes
2328 hepatocytes 10.0 3
5105 hepatocytes 11.0 4

```
