Python:为字典的每个键绘制相似性值

Python:为字典的每个键绘制相似性值,python,dictionary,matplotlib,Python,Dictionary,Matplotlib,我提取了图像,并构建了两个字典,其中文件路径作为键,列表包含图像摘要数据作为列表 举个例子,假设我们有字典a={},它最终看起来像{'fileName':['x=1,2,3','y=1,2,3','z=1,2,3'…]} 以下是我的实际词典的外观: {'/Users/cannon/AmazonBasics-Wireless-Remote-Control-for-Nikon-P700030004040x50500051006070700070s80-and90igital-SLR-Cameras.

我提取了图像,并构建了两个字典,其中文件路径作为键,列表包含图像摘要数据作为列表

举个例子,假设我们有字典
a={}
,它最终看起来像
{'fileName':['x=1,2,3','y=1,2,3','z=1,2,3'…]}

以下是我的实际词典的外观:

{'/Users/cannon/AmazonBasics-Wireless-Remote-Control-for-Nikon-P700030004040x50500051006070700070s80-and90igital-SLR-Cameras.jpg': [
'fcolor=2,3,0,80,125,15,5,0,0,0,0,0,0,0,0,0,0,0,0,0,2,4,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0', 'edge=1,224,3,98,48,4,66,72,70,32,4,34,16,14,161,9,68,128,7', 'texture=1,173,20,9,5,3,3,4,34'],

'/Users/cannon/Sony-50mm-f-1.4-Lens-for-Sony-Alphaigital-SLR-Camera.jpg': [
'fcolor=2,111,1,18,100,1,0,0,0,0,0,0,0,0,0,1,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0', 'edge=1,252,35,64,99,236,127,0,17,0,1,16,139,241,24,128,2,132,31', 'texture=1,92,52,25,17,14,9,7,34']}
我已经编写了一个函数,它接受上面提到的两个字典,对于一个字典中的每个键,对第二个字典中的每个键进行迭代,并计算余弦值

字典有三种类型的值转储到列表中:fcolor、edge和texture。My函数计算两个字典中每个键列表中每个值的点积

问题: 我要做的是为每个关键Ka字典A绘制字典B,Kb的所有值

这是我目前的代码:

def cosignSimilarity(aImageVectDict, eImageVectDict):
    #cx = lambda a, b : round(np.inner(a, b)/(LA.norm(a)*LA.norm(b)), 3)
    fo = open('/Users/AbovePointNine.txt','w')
    f1 = open('/Users/AbovePoint8.txt','w')
    pltfColor = []
    pltEdgeColor = []
    pltTexture = []
    #converting the string of vectors into numpy arrays and calculate cosine similarity
    for (fn1, lst1), (fn2, lst2) in product(aImageVectDict.iteritems(), eImageVectDict.iteritems()):
        print fn1
        print fn2
        fcolorCosine = None
        edgeColorCosine = None
        textureColorCosine = None
        for vector1, vector2 in zip(lst1, lst2):
            cx = None
            cx = lambda a, b : round(np.inner(a, b)/(LA.norm(a)*LA.norm(b)), 3)
            v1fColor = vector1.split('=')[1]
            debug1 = v1fColor
            v2fColor = vector2.split('=')[1]
            debug2 = v2fColor
            if (vector1.split('=')[0] == 'fcolor' and vector2.split('=')[0] == 'fcolor'):
                v1fColor =  np.array(map(int,v1fColor.split(',')))
                v2fColor =  np.array(map(int,v2fColor.split(',')))
                fcolorCosine = cx(v1fColor, v2fColor)
                print fcolorCosine
                pltfColor.append(fcolorCosine)
            v1Edge = vector1.split('=')[1]
            v2Edge = vector2.split('=')[1]
            if (vector1.split('=')[0] == 'edge' and vector2.split('=')[0] == 'edge'):
                v1Edge = np.array(map(int,v1Edge.split(',')))
                v2Edge = np.array(map(int,v2Edge.split(',')))
                edgeColorCosine = cx(v1Edge, v2Edge)
                print edgeColorCosine
                pltEdgeColor.append(edgeColorCosine)
            v1Texture = vector1.split('=')[1]
            v2Texture = vector2.split('=')[1]
            if (vector1.split('=')[0] == 'texture' and vector2.split('=')[0] == 'texture'):
                v1Texture = np.array(map(int,v1Texture.split(',')))
                v2Texture =  np.array(map(int,v2Texture.split(',')))
                textureColorCosine = cx(v1Texture, v2Texture)
                print textureColorCosine
                pltTexture.append(textureColorCosine)
            if (fcolorCosine > 0.9 and edgeColorCosine > 0.9 and textureColorCosine > 0.9):
                fo.write(fn1)
                fo.write('\n')
                fo.write(fn2)
                fo.write('\n')
                fo.write(str(fcolorCosine))
                fo.write('\n')
                fo.write(str(edgeColorCosine))
                fo.write('\n')
                fo.write(str(textureColorCosine))
            if (fcolorCosine > 0.8 and edgeColorCosine > 0.8 and textureColorCosine > 0.8):
                f1.write(fn1)
                f1.write('\n')
                f1.write(fn2)
                f1.write('\n')
                f1.write(str(fcolorCosine))
                f1.write('\n')
                f1.write(str(edgeColorCosine))
                f1.write('\n')
                f1.write(str(textureColorCosine))
    fo.close()
    f1.close()

    #Plotting 
    plt.plot(pltfColor, 'r.')
    #plt.plot(pltEdgeColor, 'b.')
    #plt.plot(pltTexture, 'g.')
    plt.ylabel('cosine values')
    #plt.yscale('log')
    plt.show()
    fo.close()

再次确认我真正想要的是字典
A
中的每个键,我想找出字典
B

@mgilson中的所有结果。过去,您提供了很好的指导,您有机会看看吗problem@david-罗宾逊:你能看看这个问题吗?