I want to calculate cosine similarity for words in two dictionaries. The words are keys, arrays are values. In order to do this, I need to convert them to 2D array first, I did some research and found x = x.reshape(1,-1)
I successfully converted it with a single value in dictionary, however, I don't know how to convert the whole dictionary's values by using for loops.
The data
D
{'A': array([ 4.80625004e-01, -1.40245005e-01, -9.99999046e-03]),
'B': array([-0.46553 , -0.1519755 , 0.41836]),
'C': array([0.0090175 , 0.05817001, -0.09712502])}
D2 (same format as D)
{'D': ([8.11059952e-01, 6.84859991e-01, 1.01619996e-01]),
'E':([-0.82868 , 0.49513 , 0.67581]),
'F':([-0.17093 , 0.88746 , 0.0931135])}
I tried
for i in D2.items():
D2[i] = D2[i].reshape(1, -1) #Error on this line
but received error: TypeError: unhashable type: 'numpy.ndarray'
Some advice please? thank you in advanced!
Dict.items() returns tuples of the key, value pairs. Therefore in your case i
is a tuple of the key and the value. Try:
for i, value in D2.items():
D2[i] = value.reshape(1, -1)
try this
from numpy import array
d = dict({'A': array([ 4.80625004e-01, -1.40245005e-01, -9.99999046e-03]),
'B': array([-0.46553 , -0.1519755 , 0.41836]),
'C': array([0.0090175 , 0.05817001, -0.09712502])})
reshaped_arrays = dict()
for x, j in d.items():
dd[x]=j.reshape(1,-1)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.