如何将相同的集合组合在一起

时间:2016-01-03 19:37:40

标签: python math probability

我正在随机抽样多个矩阵A,每个矩阵计算Ax的概率质量函数(pmf)。 x是随机的,带有+ -1的元素,A是带有+ -1的元素的矩阵。到目前为止我的代码看起来像:

from collections import Counter
import numpy as np
import itertools

def pmf(L):
    C = Counter(L)
    total = float(sum(C.values()))
    for key in C:
        C[key]/=total
    return C

N = 10

h = 2
n = 2**h
X = np.array(list(itertools.product([-1,1],repeat = n))).T

for _ in xrange(N):
    A = (np.random.randint(2, size=(h,n)))*2-1
    B = np.dot(A,X)
    probs = pmf([tuple(x) for x in B.T.tolist()])
    print probs

这给出了例如:

Counter({(0, 0): 0.375, (2, 2): 0.25, (-2, -2): 0.25, (-4, -4): 0.0625, (4, 4): 0.0625})
Counter({(0, 0): 0.25, (-2, 2): 0.125, (2, 2): 0.125, (2, -2): 0.125, (-2, -2): 0.125, (0, 4): 0.0625, (-4, 0): 0.0625, (4, 0): 0.0625, (0, -4): 0.0625})
Counter({(-2, 0): 0.1875, (0, 2): 0.1875, (2, 0): 0.1875, (0, -2): 0.1875, (-2, -4): 0.0625, (4, 2): 0.0625, (2, 4): 0.0625, (-4, -2): 0.0625})
Counter({(0, 2): 0.1875, (2, 0): 0.1875, (0, -2): 0.1875, (-2, 0): 0.1875, (-4, -2): 0.0625, (-2, -4): 0.0625, (4, 2): 0.0625, (2, 4): 0.0625})
Counter({(0, 0): 0.25, (-2, -2): 0.125, (-2, 2): 0.125, (2, 2): 0.125, (2, -2): 0.125, (-4, 0): 0.0625, (0, 4): 0.0625, (0, -4): 0.0625, (4, 0): 0.0625})
Counter({(0, 0): 0.25, (2, -2): 0.125, (2, 2): 0.125, (-2, -2): 0.125, (-2, 2): 0.125, (-4, 0): 0.0625, (0, 4): 0.0625, (0, -4): 0.0625, (4, 0): 0.0625})
Counter({(-2, 0): 0.1875, (2, 0): 0.1875, (0, -2): 0.1875, (0, 2): 0.1875, (-4, 2): 0.0625, (4, -2): 0.0625, (-2, 4): 0.0625, (2, -4): 0.0625})
Counter({(-2, 0): 0.1875, (2, 0): 0.1875, (0, -2): 0.1875, (0, 2): 0.1875, (-4, 2): 0.0625, (4, -2): 0.0625, (-2, 4): 0.0625, (2, -4): 0.0625})
Counter({(2, 0): 0.1875, (0, -2): 0.1875, (0, 2): 0.1875, (-2, 0): 0.1875, (-4, -2): 0.0625, (-2, -4): 0.0625, (2, 4): 0.0625, (4, 2): 0.0625})
Counter({(0, 0): 0.25, (-2, -2): 0.125, (-2, 2): 0.125, (2, 2): 0.125, (2, -2): 0.125, (-4, 0): 0.0625, (0, 4): 0.0625, (0, -4): 0.0625, (4, 0): 0.0625})

我可以手工收集所有相同的收藏品,并计算每个收藏品的数量。例如,使用上面的输出:

2     Counter({(-2, 0): 0.1875, (2, 0): 0.1875, (0, -2): 0.1875, (0, 2): 0.1875, (-4, 2): 0.0625, (4, -2): 0.0625, (-2, 4): 0.0625, (2, -4): 0.0625})
2     Counter({(0, 0): 0.25, (-2, -2): 0.125, (-2, 2): 0.125, (2, 2): 0.125, (2, -2): 0.125, (-4, 0): 0.0625, (0, 4): 0.0625, (0, -4): 0.0625, (4, 0): 0.0625})
1     Counter({(-2, 0): 0.1875, (0, 2): 0.1875, (2, 0): 0.1875, (0, -2): 0.1875, (-2, -4): 0.0625, (4, 2): 0.0625, (2, 4): 0.0625, (-4, -2): 0.0625})
1     Counter({(2, 0): 0.1875, (0, -2): 0.1875, (0, 2): 0.1875, (-2, 0): 0.1875, (-4, -2): 0.0625, (-2, -4): 0.0625, (2, 4): 0.0625, (4, 2): 0.0625})
1     Counter({(0, 2): 0.1875, (2, 0): 0.1875, (0, -2): 0.1875, (-2, 0): 0.1875, (-4, -2): 0.0625, (-2, -4): 0.0625, (4, 2): 0.0625, (2, 4): 0.0625})
1     Counter({(0, 0): 0.375, (2, 2): 0.25, (-2, -2): 0.25, (-4, -4): 0.0625, (4, 4): 0.0625})
1     Counter({(0, 0): 0.25, (2, -2): 0.125, (2, 2): 0.125, (-2, -2): 0.125, (-2, 2): 0.125, (-4, 0): 0.0625, (0, 4): 0.0625, (0, -4): 0.0625, (4, 0): 0.0625})
1     Counter({(0, 0): 0.25, (-2, 2): 0.125, (2, 2): 0.125, (2, -2): 0.125, (-2, -2): 0.125, (0, 4): 0.0625, (-4, 0): 0.0625, (4, 0): 0.0625, (0, -4): 0.0625})

但是,我想要的是这些相同集合中的每一个都要查看导致它们的矩阵。所以这将是前两组的两个矩阵,其余为1。

这样做的好方法是什么?

1 个答案:

答案 0 :(得分:2)

因为你想按值对每一个进行分组,你可以使用键的pmf值创建一个新的dict,并为你拥有的矩阵的列表/值设置值,并且为了帮助你可以使用带有list /的defaultdict组。像这样

from collections import Counter, defaultdict

test = Counter({(0, 0): 0.25, (-2, -2): 0.125, (-2, 2): 0.125, (2, 2): 0.125, (2, -2): 0.125, (-4, 0): 0.0625, (0, 4): 0.0625, (0, -4): 0.0625, (4, 0): 0.0625})

result = defaultdict(list)
for k,v in test.iteritems():
    result[v].append(k)
print result

#or more readable 
for k,v in result.iteritems():
    print k,v

输出

0.25 [(0, 0)]
0.125 [(2, 2), (2, -2), (-2, -2), (-2, 2)]
0.0625 [(-4, 0), (0, 4), (0, -4), (4, 0)]

修改

现在,如果你想获得产生特定 pmf-value 的矩阵A,那么单独从 pmf-value 获取它是不可能的,所以需要通过跟踪产生特定 pmf-value 的每个矩阵来解决这个问题,使用与以前相同的defaultdict方法,并使用 pmf 作为关键并重视列表具有 pmf 的矩阵,如下所示:

from collections import Counter, defaultdict
import numpy as np
import itertools

def pmf(L):
    C = Counter(L)
    total = float(sum(C.values()))
    for key in C:
        C[key]/=total
    return C

N = 10

h = 2
n = 2**h
X = np.array(list(itertools.product([-1,1],repeat = n))).T

result = defaultdict(list)

for _ in xrange(N):
    A = (np.random.randint(2, size=(h,n)))*2-1
    B = np.dot(A,X)
    probs = pmf([tuple(x) for x in B.T.tolist()])
    print probs
    result[ frozenset(probs.items()) ].append( A ) #append the one you need

现在这部分frozenset(probs.items())是因为 Counter 是一个不可用的对象,因为它是可变的,因此不能使用dict键,所以我需要通过转换它使它成为不可变的到其项目的 frozenset

有了这个,现在我们所有的矩阵都有一个特定的 pmf

输出

>>> for k,v in result.items():
        print k
        for A in v:
            print A
            print ""
        print "--------------------"


frozenset({((-4, 2), 0.0625), ((2, 0), 0.1875), ((4, -2), 0.0625), ((0, 2), 0.1875), ((-2, 4), 0.0625), ((0, -2), 0.1875), ((-2, 0), 0.1875), ((2, -4), 0.0625)})
[[ 1  1 -1  1]
 [-1 -1  1  1]]

[[ 1  1 -1 -1]
 [-1  1  1  1]]

--------------------
frozenset({((2, 0), 0.1875), ((-2, -4), 0.0625), ((2, 4), 0.0625), ((0, 2), 0.1875), ((4, 2), 0.0625), ((0, -2), 0.1875), ((-2, 0), 0.1875), ((-4, -2), 0.0625)})
[[-1  1  1 -1]
 [ 1  1  1 -1]]

[[ 1  1  1 -1]
 [ 1 -1  1 -1]]

[[-1  1 -1  1]
 [-1  1  1  1]]

--------------------
frozenset({((0, 0), 0.25), ((-2, 2), 0.125), ((2, -2), 0.125), ((0, -4), 0.0625), ((0, 4), 0.0625), ((-4, 0), 0.0625), ((2, 2), 0.125), ((-2, -2), 0.125), ((4, 0), 0.0625)})
[[-1 -1 -1  1]
 [ 1  1 -1  1]]

[[-1  1  1 -1]
 [-1 -1 -1 -1]]

[[-1 -1 -1 -1]
 [ 1 -1  1 -1]]

[[ 1  1 -1  1]
 [ 1 -1  1  1]]

[[ 1  1  1  1]
 [-1 -1  1  1]]

--------------------
>>>