How to compare python array in an array list and return the same or nearest array

Asked

Viewed 792 times

-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

  • 1

    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 yes True or False indicating whether the arrays are "equal" (within a tolerance margin)

  • 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?

  • 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.

  • 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]?

  • 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...

  • Corrected to make it clearer (and easier) instead of values None replaced by -100

Show 1 more comment

1 answer

0


From what I understood from your question: you want to compare element-element a 'search' reference vector with a series of B’s vectors and locate those that meet their error margin for all elements. Do this without using loops with numpy functions.

import numpy as np
search = np.array([(-50,-68,-70,-65,-78,-55)])

B1A = np.array([(-46,-78,-72,-70,-81,-59)])
B1B = np.array([(-100,-82,-85,-100,-76,-55)])
B1C = np.array([(-100,-100,-100,-78,-100,-58)])
B1D = np.array([(-51,-64,-73,-67,-79,-56)])
B4A = np.array([(-77,-100,-84,-75,-72,-72)])
B4B = np.array([(-78,-81,-80,-72,-70,-67)])
B4C = np.array([(-76,-76,-81,-67,-62,-67)])
B5A = np.array([(-100,-76,-80,-67,-61,-69)])
B5B = np.array([(-100,-79,-80,-68,-59,-71)])
B6A = np.array([(-77,-81,-78,-62,-76,-80)])
B6B = np.array([(-68,-75,-76,-54,-73,-74)])
B6C = np.array([(-72,-73,-72,-62,-72,-75)])
B7A = np.array([(-100,-78,-78,-65,-74,-78)])
B7B = np.array([(-100,-81,-77,-69,-74,-76)])

candidatos = [B1A, B1B, B1C, B1D, B4A, B4B, B4C, B5A, B5B, B6A, B6B, B6C, B7A, B7B]
margem_erro = 5.0

distancias = candidatos[::] - search # Avalia a distância de cada vetor para o vetor de busca.
avaliar_dist = np.where(np.absolute(distancias) < margem_erro, True, False) # Localiza em quais posições dos vetores de distância a margem de erro é satisfeita.
vetores_aprovados = avaliar_dist.all(axis=2) # Marca Verdadeiro se toda a linha tem valores Verdadeiros.
posicao_aprovados = np.array(np.where(vetores_aprovados== True)[0]) # Grava a posição dos vetores dentro da margem de erro.

print("Busca: {}".format(search))
print("Resultado: ")
for x in posicao_aprovados:
    print(candidatos[x])
  • Man this solved! I did some tests and got the expected results. For this I only needed to calibrate the margin of error (as expected right) Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.