[英]Reshape dictionary values from 1D to 2D array
我想計算兩個字典中單詞的余弦相似度。 單詞是鍵,數組是值。 為此,我需要先將它們轉換為2D數組,我進行了一些研究,發現x = x.reshape(1,-1)
我成功地用字典中的單個值將其轉換了,但是,我沒有知道如何使用for循環轉換整個字典的值。
數據
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])}
我試過了
for i in D2.items():
D2[i] = D2[i].reshape(1, -1) #Error on this line
但收到錯誤: TypeError:無法散列的類型:'numpy.ndarray'
請問一些建議嗎? 在此先感謝您!
Dict.items()返回鍵值對的元組。 因此,在您的情況下, i
是鍵和值的元組。 嘗試:
for i, value in D2.items():
D2[i] = value.reshape(1, -1)
嘗試這個
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.