我有下面的代碼可以幫助我使用數組減號矩陣,但系統返回錯誤在這一行union = total - zero
。 zero
實際上是交點的計數。如果我在union=total-zero
前面發表評論#我可以通過輸入total - zero
手動在python窗口中獲得我的答案。在這種情況下,total
和zero
是數組。Python:Array減矩陣 - TypeError:不支持的操作數類型爲 - :'int'和'list'
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
任何人都可以幫助我嗎?
我相信你的最後一個'total' 屬性,在片中,有一個很好的逗號。檢查它 –
而且,來自這個'alldiffs'? –
alldiffs是一個已定義的函數。你的意思是'union = total,-zero'?它返回錯誤。 – Xiong89