Max
Max

Reputation: 23

How to make a numpy matrix from the values of a dictionary of tuples?

I have a dictionary with tuples consisting of pairs of words, and probabilities as values, for example d = {('a','b'): 0.5, ('b', 'c'): 0.5, ('a', 'd'): 0.25 ...} and so forth where every word in a tuple has a pair with another one. So for example if there are 4 words total, the length of the dictionary would be 16.

I am trying to put the values in a numpy array, in the format

///  a  b  c  d

a 

b 

c

d

However, I am having a hard time doing this. Any help would be appreciated. Thanks in advance!

Upvotes: 2

Views: 443

Answers (1)

Joe Kington
Joe Kington

Reputation: 284830

The easiest way to think about this is that your letters/words are indices. You want to convert the letter a to the index 0 and the letter b to the index 1.

With that in mind, a simple way to do this is to use the index method of a list. For example, if we have a list with unique values like x = ['cat', 'dog', 'panda'] we can do x.index('dog') to get the index 1 where 'dog' occurs in the list x.

With that in mind, let's generate some data similar to what you described:

import numpy as np

# I'm going to cheat a bit and use some numpy tricks to generate the data
x = np.random.random((5, 5))

values = ['alpha', 'beta', 'gamma', 'delta', 'epsilon']

your_data = {}
for (i, j), prob in np.ndenumerate(x):
    your_data[(values[i], values[j])] = prob

print(your_data)

This gives something like:

{('alpha', 'beta'): 0.8066925762434737, ('alpha', 'gamma'): 0.7778280007471104, ...}

So far, we've just generated some example data. Now let's do the inverse to solve your problem:

values = ['alpha', 'beta', 'gamma', 'delta', 'epsilon']

output = np.zeros((len(values), len(values)), dtype=float)
for key in your_data:
    i = values.index(key[0])
    j = values.index(key[1])
    output[i, j] = your_data[key]

print(output)

That will give us a numpy array with values similar to what you described.

Upvotes: 3

Related Questions