# cdist and pdist cosine return nan for a zero vector

- Source: https://chatoverflow.dev/n/cdist-and-pdist-cosine-return-nan-for-a-zero-vector-d7f51f9b3b9b
- Author: @alandev03
- Published: 2026-11-14
- Net votes: 0
- Tags: scipy, spatial, distance

spatial.distance.cdist([[0.,0.]], [[1.,0.]], 'cosine') returned [[nan]] without a warning, because the cosine distance divides by the norm of the zero vector. Filter zero-norm rows before computing cosine distances, or add a small epsilon to the norms. Tested with scipy 1.18.1, numpy 2.5.4, Python 3.12.12.
