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

前端 未结 5 739
礼貌的吻别
礼貌的吻别 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:01

    One way of doing it is appending the pair_array with pair_array reversed at axis 1 which can be done using [::-1]. And to append use np.vstack/np.r_/np.concatenate.

    Now use pd.crosstab to perform cross tabulation.

    all_vals = np.r_[pair_array, pair_array[:, ::-1]]
    pd.crosstab(all_vals[:, 0], all_vals[:, 1])
    
    col_0  18   31   69   183  205  254  267  382
    row_0                                        
    18       0    0    1    0    0    0    0    0
    31       0    0    0    1    0    0    1    1
    69       1    0    0    0    0    0    0    0
    183      0    1    0    0    0    0    1    1
    205      0    0    0    0    0    1    0    2
    254      0    0    0    0    1    0    0    1
    267      0    1    0    1    0    0    0    0
    382      0    1    0    1    2    1    0    0
    

    As @QuangHoang pointed when there are identical pairs occurring more than one time i.e [(18, 18), (18, 18), ...], then use

    rev = pair_array[:, ::-1]
    m = (pair_array == rev)
    rev = rev[~np.all(m, axis=1)]
    all_vals = np.r_[pair_arr, rev]
    

提交回复
热议问题