How can I convert a two column array to a matrix with counts of occurences?

前端 未结 5 817
礼貌的吻别
礼貌的吻别 2021-02-02 08:21

I have the following numpy array:

import numpy as np

pair_array = np.array([(205, 254), (205, 382), (254, 382), (18, 69), (205, 382), 
                       (31         


        
5条回答
  •  自闭症患者
    2021-02-02 09:08

    You could create a data frame of the appropriate size with zeros beforehand and just increment the appropriate cells by looping over the pairs:

    import numpy as np
    import pandas as pd
    
    pair_array = np.array([(205, 254), (205, 382), (254, 382), (18, 69), (205, 382),
                           (31, 183), (31, 267), (31, 82), (183, 267), (183, 382)])
    
    vals = sorted(set(pair_array.flatten()))
    n = len(vals)
    
    df = pd.DataFrame(np.zeros((n, n), dtype=np.int), columns=vals, index=vals)
    
    for r, c in pair_array:
        df.at[r, c] += 1
        df.at[c, r] += 1
    
    print(df)
    

    Output:

         18   31   69   82   183  205  254  267  382
    18     0    0    1    0    0    0    0    0    0
    31     0    0    0    1    1    0    0    1    0
    69     1    0    0    0    0    0    0    0    0
    82     0    1    0    0    0    0    0    0    0
    183    0    1    0    0    0    0    0    1    1
    205    0    0    0    0    0    0    1    0    2
    254    0    0    0    0    0    1    0    0    1
    267    0    1    0    0    1    0    0    0    0
    382    0    0    0    0    1    2    1    0    0
    

提交回复
热议问题