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

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

    If you are okay to add pandas as a dependency you can use this implementation

    >>> import pandas as pd
    >>> df = pd.DataFrame(pair_array)
    >>> pd.crosstab(df[0], df[1])
    1    69   183  254  267  382
    0
    18     1    0    0    0    0
    31     0    1    0    1    1
    183    0    0    0    1    1
    205    0    0    1    0    2
    254    0    0    0    0    1
    

提交回复
热议问题