Search code examples
pythonnumpysequencematcher

How to compare each array in a set of binary arrays to an array that is outside the set


I have a set of arrays. I also have a separate array (T) to compare each array in the set to. I've tried to use SequenceMatcher to do this but can't figure out how to loop it so that each array from the set gets compared to T.

This is for a fitness function for a genetic algorithm. I'm new to python and have tried several things. The code below may be laughable!

import difflib

parents = set()
while len(parents) < 5:
    a = tuple(np.random.choice([0, 1], size=(10)))
    if a not in parents: parents.add(a) # To make them different
parents = np.array([list(x) for x in parents])

print(pop_sp())

T = tuple(np.random.choice([0, 1], size=(20)))

for i in parents:
    fitness=difflib.SequenceMatcher(None,i,T)
    print(fitness.ratio)

I expect the output to be

[[0 0 1 0 0 0 0 1 1 0]  
 [0 0 0 1 1 0 1 0 0 0]  
 [1 0 1 1 1 1 0 0 1 1]  
 [0 0 1 0 0 1 1 0 0 0]  
 [1 1 0 1 1 0 0 1 0 0]] 

and the percent of similarity of each array to T.
but I am getting the following:

[[0 0 1 0 0 0 0 1 1 0]  
 [0 0 0 1 1 0 1 0 0 0]  
 [1 0 1 1 1 1 0 0 1 1]  
 [0 0 1 0 0 1 1 0 0 0]  
 [1 1 0 1 1 0 0 1 0 0]] 
<bound method SequenceMatcher.ratio of <difflib.SequenceMatcher object at 0x1c1ea8ec88>>  
<bound method SequenceMatcher.ratio of <difflib.SequenceMatcher object at 0x1c1dff9438>>  
<bound method SequenceMatcher.ratio of <difflib.SequenceMatcher object at 0x1c1ea8ec88>>  
<bound method SequenceMatcher.ratio of <difflib.SequenceMatcher object at 0x1c1dff9438>>  
<bound method SequenceMatcher.ratio of <difflib.SequenceMatcher object at 0x1c1ea8ec88>>

Solution

  • You have to call the fitness function

    for i in parents:
        fitness=difflib.SequenceMatcher(None,i,T)
        ratios = fitness.ratio()
        print(ratios)