我的输出如下所示,这是循环迭代的结果。基本上,下面的代码是使用交叉和并集计算来比较不同大小的数组:
import numpy as py
import itertools
from itertools import izip
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]
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])]
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)
union=map(sub,total,zero)
zero=np.array(zero).tolist()
union=np.array(union).tolist()
union=[list(x) for x in union]
sim=[[float(aaa) / bbb for (aaa, bbb) in itertools.izip(aa, bb)] \
for (aa, bb) in itertools.izip(zero, union)]
sim_comb=sum(sim,[])
sum_of_sim=sum(sim_comb)
number_sum=len(sim_comb)
ave=sum_of_sim/number_sum
one_ave=1-ave
print one_ave
输出
>>>
0.9
0.829166666667
0.875
如何将它们保存到列表中,以便输出如下:
>>>
[0.9,0.829166666667,0.875]
任何人都可以帮助我吗?
答案 0 :(得分:1)
好的循环似乎是这个(在代码的末尾):
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)
union=map(sub,total,zero)
zero=np.array(zero).tolist()
union=np.array(union).tolist()
union=[list(x) for x in union]
sim=[[float(aaa) / bbb for (aaa, bbb) in itertools.izip(aa, bb)] \
for (aa, bb) in itertools.izip(zero, union)]
sim_comb=sum(sim,[])
sum_of_sim=sum(sim_comb)
number_sum=len(sim_comb)
ave=sum_of_sim/number_sum
one_ave=1-ave
print one_ave
一种可能的解决方案是写:
output = []
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)
union=map(sub,total,zero)
zero=np.array(zero).tolist()
union=np.array(union).tolist()
union=[list(x) for x in union]
sim=[[float(aaa) / bbb for (aaa, bbb) in itertools.izip(aa, bb)] \
for (aa, bb) in itertools.izip(zero, union)]
sim_comb=sum(sim,[])
sum_of_sim=sum(sim_comb)
number_sum=len(sim_comb)
ave=sum_of_sim/number_sum
one_ave=1-ave
output += [one_ave]
print output
答案 1 :(得分:0)
正如您的评论建议您需要将它们转换为列表项而不是浮点数。
sim=[[float(aaa) / bbb for (aaa, bbb) in itertools.izip(aa, bb)] \
for (aa, bb) in itertools.izip(zero, union)]
在这行代码中,您可以在此明确定义值为float。或者,您可以使用map()函数和split()sim变量。