A
A
Alex Fox2020-02-06 21:12:34
Mathematics
Alex Fox, 2020-02-06 21:12:34

How to divide a list of numbers into groups by a given distance between the numbers in the group?

Hello. I need advice on how to do the following correctly (not correctly / not beautifully, etc. I can think of):
The input is a list / array / set (no difference) of numbers (any data)
25.0 27.04 27.04 26 ,4 27.08 27.04 27 27 26.95 26.91
And the number X is an error, I don’t know how to call it correctly, sorry)))
At the output, you need to get groups:
(27.04 27.04 27.08 27 .04 27 27 26.95 26.91) (26.4) (25.0).
There is an idea: the number X sets the error where the current number falls, i.e.
We take the number 25.0 (K) - the group will be K + X and K-X - if the number X \u003d 5, then the numbers from 24.95 to 25.05 fall into the group.
Yes, the question is related to mathematics (not strong in it :)), it looks like some kind of partition, .
I ask for advice on how to do it correctly (correct algorithm) !! At the same time, I'm thinking about how to do it.
I accept any criticism!
The task, so to speak, from the pet-project at your leisure))!
Thank you all in advance for taking the time to answer my question!!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dmshar, 2020-02-07
@AlexFox2019

There is no "math" here. However, as well as cluster analysis. Everything is trivially simple. If everything is exactly as you described, then you yourself have already described your algorithm. Truth lowering its first step - preliminary sorting.
So:
1. We sort the available data.
2. We start viewing (no matter what - upper or lower) the extreme member of the sequence and compare the next element of the sequence with the boundary of your interval (by the way, there is probably an error in the question, and your X is not 5, but 0.05). Accordingly, we include it in the first group, or "close" the first group and proceed to the formation of the second. We repeat this action cyclically going through all the elements of our sorted set.
If you try to do without preliminary sorting - which, in principle, is also not very difficult - then you need to carefully see if the computational complexity does not increase in this case (I'm not sure about this at a glance).

A
Andy_U, 2020-02-06
@Andy_U

Read about cluster analysis. Well, or take a ready-made module: sklearn.cluster

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question