-2
I would like to know how I can enter with a vector and search in a list of vectors which has more equal or similar values using Python.
Example:
I have the following vector:
search = [-50,-68,-70,-65,-78,-55]
And I want to know which of the vectors below it is closest or identical
B1A = [-46,-78,-72,-70,-81,-59]
B1B = [-100,-82,-85,-100,-76,-55]
B1C = [-100,-100,-100,-78,-100,-58]
B4A = [-77,-100,-84,-75,-72,-72]
B4B = [-78,-81,-80,-72,-70,-67]
B4C = [-76,-76,-81,-67,-62,-67]
B5A = [-100,-76,-80,-67,-61,-69]
B5B = [-100,-79,-80,-68,-59,-71]
B6A = [-77,-81,-78,-62,-76,-80]
B6B = [-68,-75,-76,-54,-73,-74]
B6C = [-72,-73,-72,-62,-72,-75]
B7A = [-100,-78,-78,-65,-74,-78]
B7B = [-100,-81,-77,-69,-74,-76]
I did the following with the help of @Jeanextreme002
search = [-50,-78,-70,-65,-80,-55]
B1A = [-46,-78,-72,-70,-81,-59]
B1B = [-100,-82,-85,-100,-76,-55]
B1C = [-100,-100,-100,-78,-100,-58]
B2A = [-100,-88,-100,-100,-100,-60]
B2B = [-100,-78,-79,-80,-80,-59]
atual = ([], 0,'')
for vetor in [B1A, B1B, B1C, B2A, B2B]:
quantidade = 0
for valor in search:
if valor in vetor:
quantidade += 1
if quantidade > atual[1]:
atual = [vetor, quantidade]
resultado = atual[0]
matchs = atual[1]
print("Busca: {}".format(search))
print("Resultado: {}".format(resultado))
print("Matchs: {}".format(matchs))
Because this case does not answer me?
'Cause when he does
for valor in search:
if valor in vetor:
quantidade += 1
Actually it’s checking if there is that value in the vector, but I need each value to be compared. ie, search[0]
with vetor[0]
and so on.
The margin of error can be between 5 for more or less. that is, if the value I look for in the vector is -55 it means that I accept up to -60 or -50. Whichever is nearest.
I saw something about that function
np.allclose(A,B,...)
where I set an error mean in the array and it returns the nearest value
But I don’t know if you take my case.
One more detail:
If my list is [-55, -75]
I would prefer to [-54, -74]
because it is a wireless signal strength table so the smaller, the better.
To correct null value issue I replace the None
for -100
Good, I count on the help of all! Thanks from now on
If the list is
[-55, -75]
, which is nearest:[-54, -74]
,[-56, -76]
,[-54, -76]
or[-56, -74]
? Or are they all equally close? Another thing, limit the question to just one language (preferably the one you used in the tags, that is, in case it would just be Python), otherwise it may end up getting too wide (and this is reason for closure). Moreover,np.allclose
does not return the nearest value, and yesTrue
orFalse
indicating whether the arrays are "equal" (within a tolerance margin)– hkotsubo
Well, come on! If my list is
[-55, -75]
I would prefer to[-54, -74]
because it is a wireless signal strength table so the smaller, the better.np.allclose()
i know it returns true or false but I could adapt something to fit in my case? or would have some other function similar?– Peterson Medeiros
Then edit the question and put this information in the question, because here the rule is to answer for what is in the question and not in the comments, because the comments are considered disposable and at any time can be removed.
– Augusto Vasques
Peterson, as @Augustovasques said, please edit the question and put all the criteria there, so people don’t need to "hunt" information in the comments (all relevant information should be in the body of the question). Although
-54
is greater that-55
, then your criterion ("the smaller, the better") remains confusing to me. It also wasn’t clear what to do if the arrays have different sizes (can it happen? is there a guarantee that it doesn’t happen?), and what to consider when it has nulls:[-55, -75]
is closest to[None, -74]
or of[-54, None]
?– hkotsubo
As for having some similar function, I don’t know the
numpy
so well, then I suggest you see documentation if there is already something like what you need...– hkotsubo
Corrected to make it clearer (and easier) instead of values
None
replaced by-100
– Peterson Medeiros