Python: Array subtract Matrix - TypeError: unsupported operand type(s) for -: 'int' and 'list'

1.1k views Asked by At

I have code below that help me to use array minus matrix but the system return error at this line union = total - zero. zero is actually the count of intersection. If I put a comment # in front of union=total-zero I can manually get my answer in python window by typing total - zero. total and zero are array in this case.

import numpy as np
import itertools
from numpy import matrix,ones
from itertools import product,chain,combinations,permutations,izip
from collections import Counter

####################################################################

def diffs(a,b):
    # collect sliding window differences
    # length of window determined by the shorter array
    # if a,b are not arrays, need to replace b[...]-a with
    # a list comprehension
    n,m=len(a),len(b)
    if n>m:
        # ensure s is the shorter
        b,a=a,b # switch
        n,m=len(a),len(b)
        # may need to correct for sign switch
    result=[]
    for i in range(0,1+m-n):
        result.append(b[i:i+n]-a)
    return result

###################################################################

def alldiffs(a,b):
    # collect all the differences for elements of a and b
    # a,b could be lists or arrays of arrays, or 2d arrays
    result=[]
    for aa in a:
        for bb in b:
            result.append(diffs(aa,bb))
    return result

###################################################################

def count_total(a,b):
    #count the total number of element for two arrays in different list
    #return [sum(map(len, i)) for i in product(a, b)]
    y= lambda x:len(x)
    result=[]
    for a1 in a:
        for b1 in b:
            result.append(y(a1) + y(b1))
    return result

##################################################################

def count_zero(obj):
    #count the total number of zero for two arrays in different list
    if isinstance(obj,list):
        return list(map(count_zero,obj))
    else:
        return Counter(obj)[0]

# define the 3 arrays
# each is a list of 1d arrays

a=[np.array([2,2,1,2]),np.array([1,3])]
b=[np.array([4,2,1])]
c=[np.array([1,2]),np.array([4,3])]

comb_set = list(itertools.combinations([a,b,c],2))
for i, j in itertools.combinations([a,b,c],2):
    all_diffs = alldiffs(i,j)
    total = count_total(i,j)
    zero = count_zero(all_diffs)
    total = np.array(total)
    total = total[: np.newaxis]  
    zero = np.array(zero)
    union = total-zero

Can anyone help me?

1

There are 1 answers

1
AudioBubble On

I can manually get my answer in python window by typing total - zero.

This works, because at that point, you use the last total and zero.

If you print out i and j inside the loop (or examine comb_set carefully), you'll find that j varies from a list with 1 element (an np.ndarray) to a 2-element list and back again to a 1-element list in the last iteration. The 1 element lists don't cause the error, but the 2-element list does.

This is obviously a result from your differently sized input lists a, b and c. You may need to think how to handle a, b and c (and their combinations) differently.