building a pairwise matrix in scipy/numpy in Python from dictionaries
Posted
by user248237
on Stack Overflow
See other posts from Stack Overflow
or by user248237
Published on 2010-05-18T23:23:17Z
Indexed on
2010/05/18
23:30 UTC
Read the original article
Hit count: 222
I have a dictionary whose keys are strings and values are numpy arrays, e.g.:
data = {'a': array([1,2,3]), 'b': array([4,5,6]), 'c': array([7,8,9])}
I want to compute a statistic between all pairs of values in 'data' and build an n by x matrix that stores the result. Assume that I know the order of the keys, i.e. I have a list of "labels":
labels = ['a', 'b', 'c']
What's the most efficient way to compute this matrix?
I can compute the statistic for all pairs like this:
result = []
for elt1, elt2 in itertools.product(labels, labels):
result.append(compute_statistic(data[elt1], data[elt2]))
But I want result to be a n by n matrix, corresponding to "labels" by "labels". How can I record the results as this matrix? thanks.
© Stack Overflow or respective owner