Label Propagation in networks using Scikit and networkx

Viewed 519

I have never used label propagation before, neither in Python, but now I would need to check if this can be suitable for my problem. I have a dataset like the following

User                    Connection               Score
        xxx.dean.martin       vera.miles           10
        xxx.dean.martin       christopher.sole     5     
        xxx.dean.martin       elis.con             NaN    
        xxx.catherine.rice    vera.miles           NaN
        xxx.vera.miles        NaN                  0

where Score depends only to User and can take values 0, 5, or 10. I would like to build a graph where Users are nodes and Connection are the targets. This means that, for example, xxx.dean.martin is linked to vera.miles. Score should be a value assigned to the node (e.g., xxx.dean.martin). As shown in the example, since some values is missing (NaN), I would like to use label propagation to assign Scores where they are missing. Looking at the last example,

      `xxx.vera.miles        NaN                0.0`

I should expect links between vera.miles, dean.martin and catherine.rice, when I visualise that in a network. Based on neighbor, I would like to assign ('transfer'/'propagate') the score value through the nodes.

Example of output as dataset (that should come from a graph visualization):

 User                    Connection               Score
            xxx.dean.martin       vera.miles         10
            xxx.dean.martin       christopher.sole   5
            xxx.dean.martin       elis.con           5  # just the average of the nodes which User is linked with   
            xxx.catherine.rice    vera.miles         0
            xxx.vera.miles        NaN                0
1 Answers

Based on the setting you're posing, this is not the standard setting for label propagation, since the nodes and the meaning of the labels are somehow mixed.

To get you expected output via a 1-step propagation that calculates the mean, you can simply do:

df.fillna(df.groupby('User', as_index=False).mean()).fillna(0)

which will fill the NaN with the mean, and the leftover NaN with 0.

Related