“Correlation matrix” for strings. Similarity of nominal data

后端 未结 2 1505
我在风中等你
我在风中等你 2021-01-24 07:05

Here is my data frame. df

  store_1      store_2         store_3         store_4     

0 banana      banana           plum            banana
1 orange      ta         


        
2条回答
  •  失恋的感觉
    2021-01-24 07:45

    You can try something like this

    import itertools as it
    corr = lambda a,b: len(set(a).intersection(set(b)))/len(a)
    c = [corr(*x) for x in it.combinations_with_replacement(df.T.values.tolist(),2)]
    
    j = 0
    x = []
    for i in range(4, 0, -1): # replace 4 with df.shape[-1]
        x.append([np.nan]*(4-i) + c[j:j+i])
        j+= i
    pd.DataFrame(x, columns=df.columns, index=df.columns)
    

    Which yields

            store_1 store_2 store_3 store_4
    store_1 1.0     0.4     0.4     0.8
    store_2 NaN     1.0     0.2     0.4
    store_3 NaN     NaN     1.0     0.2
    store_4 NaN     NaN     NaN     1.0
    

提交回复
热议问题