tslearn-team / tslearn-team/tslearn

Question about Infs and Nans

Open
#381 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

new feature
Dominant language
Python
Stars
3.2k
Forks
384
Avg merge
3d 12h
Merged PRs (30d)
11

Description

This is a pure question, although it could also be a potential very minor improvement suggestion (at a very low priority). I notice that when my input data is a (numpy) array of Nans, then sometimes I would get Infs as distance values, which is easy to see and demonstrate in simple ways such as :

>>> dtw([2, 4], [np.nan, np.nan])
inf

Whereas sometimes I get Nans as the distance. I wonder if the Nans come from some numeric stability aspects or whether there is some other known edge case that is known to yield Nan from this function.

Obviously feeding Nans for a distance measure might be an abuse of a feature, as one might want to decide for themselves what are the semantics of comparing to Nan in their own particular case and it is probably not always a wise idea to let a package decide this without giving it thought as per one's mathematical ideas.

That said, it seems that all-Nan vectors are yield zero distance:

>>> dtw([[np.nan, np.nan]], [[np.nan, np.nan]])
0.0

And also have zero distance from an empty vector:

>>> dtw([[np.nan, np.nan], [np.nan, np.nan]] , [])
0.0

So just curious about the Nans as the output of the dtw function, and also asking this more as a way of getting slightly more acquainted with the library.

By the way, and not that the semantics need to be the same as the underlying numpy, when it is dealing with equality not distance:

>>> np.array_equal([np.nan], [np.nan])
False 
>>> np.array_equal([0], [np.nan])
False

Thanks to anyone who happen to care posting a quick comment!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Begin with the dtw function and reproduce the finite, NaN, and empty-vector examples from the issue. Trace how the distance values are produced; done should be a documented explanation of the differing outputs, or a clearly specified behavior and regression test if maintainers decide an improvement is warranted.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
machine-learning
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.