Compute co-occurrence matrix by counting values in cells

后端 未结 4 475
醉梦人生
醉梦人生 2021-01-01 21:07

I have a dataframe like this

df = pd.DataFrame({\'a\' : [1,1,0,0], \'b\': [0,1,1,0], \'c\': [0,0,1,1]})

I want to get

  a         


        
4条回答
  •  旧时难觅i
    2021-01-01 21:34

    Numpy matmul

    np.matmul(df.values.T,df.values)
    Out[87]: 
    array([[2, 1, 0],
           [1, 2, 1],
           [0, 1, 2]], dtype=int64)
    
    #pd.DataFrame(np.matmul(df.values.T,df.values), df.columns, df.columns)
    

提交回复
热议问题