Python: how to compute the jaccard index between two networks?

1.7k Views Asked by At

I have two dataframes df1 and df2 that contains the edgelist of two networks g1 and g2 containing the same nodes but different connections. For each node I want to compare the jaccard index between the two networks.

I define the function that compute the jaccard index

def compute_jaccard_index(set_1, set_2):
    n = len(set_1.intersection(set_2))
    return n / float(len(set_1) + len(set_2) - n) 

df1  
     i   j
0    0   2
1    0   5
2    1   2
3    2   3
4    2   4
5    2   7


df2  
     i   j
0    0   2
1    0   5
2    0   1
3    1   3
4    2   4
5    2   7

what I am doing is the following:

tmp1 = pd.unique(df1['i'])
tmp2 = pd.unique(df2['i'])

JI = []
for i in tmp1:
    tmp11 = df1[df1['i']==i]
    tmp22 = df2[df2['i']==i]
    set_1 = list(tmp11['j'])
    set_2 = list(tmp22['j'])

    JI.append(compute_jaccard_index(set_1, set_2))

I am wondering if there is a more efficient way

1

There are 1 best solutions below

0
On

I've always found it faster to take advantage of scipy's sparse matrices and vectorize the operations rather than depending on python's set functions. Here is a simple function that coverts DataFrame edge lists into sparse matrices (both directed and undirected):

import scipy.sparse as spar

def sparse_adjmat(df, N=None, directed=False, coli='i', colj='j'):
    # figure out size of matrix if not given
    if N is None:
        N = df[[coli, colj]].max() + 1

    # make a directed sparse adj matrix
    adjmat = spar.csr_matrix((np.ones(df.shape[0],dtype=int), (df[coli].values, df[colj].values)), shape = (N,N))

    # for undirected graphs, force the adj matrix to be symmetric
    if not directed:
        adjmat[df[colj].values, df[coli].values] = 1

    return adjmat

then it is just simple vector operations on the binary adjacency matrices:

def sparse_jaccard(m1,m2):

    intersection = m1.multiply(m2).sum(axis=1)
    a = m1.sum(axis=1)
    b = m2.sum(axis=1)
    jaccard = intersection/(a+b-intersection)

    # force jaccard to be 0 even when a+b-intersection is 0
    jaccard.data = np.nan_to_num(jaccard.data)
    return np.array(jaccard).flatten() 

For comparison, I've made a random pandas edge list function and wrapped your code into the following functions:

def erdos_renyi_df(N=100,m=400):
    df = pd.DataFrame(np.random.randint(0,N, size=(m,2)), columns = ['i','j'])
    df.drop_duplicates(['i','j'], inplace=True)
    df.sort_values(['i','j'], inplace=True)
    df.reset_index(inplace=True, drop=True)
    return df

def compute_jaccard_index(set_1, set_2):
    n = len(set_1.intersection(set_2))
    return n / float(len(set_1) + len(set_2) - n) 

def set_based_jaccard(df1,df2):
    tmp1 = pd.unique(df1['i'])
    tmp2 = pd.unique(df2['i'])
    JI = []
    for i in tmp1:
        tmp11 = df1[df1['i']==i]
        tmp22 = df2[df2['i']==i]
        set_1 = set(tmp11['j'])
        set_2 = set(tmp22['j'])

        JI.append(compute_jaccard_index(set_1, set_2))

    return JI

We can then compare the runtime by making two random networks:

N = 10**3
m = 4*N

df1 = erdos_renyi_df(N,m)
df2 = erdos_renyi_df(N,m)

And calculating the Jaccard similarity for each node using your set based method:

%timeit set_based_jaccard(df1,df2)
1.54 s ± 113 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

And the sparse method (including the overhead of converting to sparse matrices):

%timeit sparse_jaccard(sparse_adjmat(df1, N=N, directed=True, coli='i', colj='j'),sparse_adjmat(df2, N=N, directed=True, coli='i', colj='j'))
1.71 ms ± 109 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

As you can see, the sparse matrix code is about 1000 times faster.