0

I am trying to use this code, which calculates the fraction of overlap at a specific depth, to use at various depths.

def score(list1, list2, depth):
    len_list = len(list1)

    frac = numpy.ceil(depth * len_list)

    frac = int(frac)
    s = slice(0,frac)

    list1 = list1[s]
    list2 = list2[s]

    return len(set(list1)&set(list2)) / float(len(set(list1) | set(list2))) 


if __name__ == "__main__":

    list1 = [2,4,6,8,10]
    list2 = [1,2,3,4,5]

    a = [numpy.arange(.01,1.01,.01)]
    for i in a:
        print(score(list1, list2, i))

However, when I try to run this code I am getting an error:

    frac = int(frac)
TypeError: only length-1 arrays can be converted to Python scalars

Which means that the variable depth is actually a list of the variable a (which is [0.01, 0.02..etc]).

How would I correct this so that the function only takes one argument of the parameter 'depth' at a time instead of what seems like the whole list?

Thanks

Labrat
  • 105
  • 10

1 Answers1

2

Like Kevin said in the comment, your problem is you are creating a list which has another list inside of it in your code a = [numpy.arange(.01,1.01,.01)]. Just remove the extra brackets and it will work.

Jeremy
  • 818
  • 6
  • 19