Skip to content

Link prediction algorithms #2 - #1163

Merged
ysitu merged 30 commits into
networkx:masterfrom
kmkurn:link-prediction
Jun 1, 2014
Merged

ysitu merged 30 commits into
networkx:masterfrom
kmkurn:link-prediction

Conversation

@kmkurn

@kmkurn kmkurn commented May 15, 2014

Copy link
Copy Markdown

This is the sequel of PR #1117. I added Jaccard coefficient, Adamic-Adar index, and preferential attachment score as described here.

I am thinking of adding Katz score too, probably utilizing the existing katz_centrality function, but I am quite busy at the moment. If there's anyone who could help that'll be great. I don't really understand the other algorithms mentioned in the paper so I won't implement it now.

@ysitu

ysitu commented May 15, 2014

Copy link
Copy Markdown
Contributor

https://travis-ci.org/networkx/networkx/jobs/25251378#L2693

I was kinda concerned about this when you put super precise floating-point numbers in docstrings in #1117. Now it finally blows up on Travis.

BTW, please add references to the new functions in the Sphinx source files.

@chebee7i

Copy link
Copy Markdown
Member

Doctests can be painful when they are tested. Nose provides a feature to skip a test on a per line basis by adding # doctest: +SKIP to the end of the line. That's fine when its a single line but when its a whole bunch of lines, it seems to default the purpose of the docstring---which is to demonstrate usage (not validity) and look clean for users. Filling it with +SKIP on a lot of lines might even be confusing. An alternative is that you use things like NumPy's isclose function, but that also defeats the purpose of the docstring, making it more difficult to parse.

I somewhat prefer not to test docstrings, but that has its own problems as well. In documentation (that exists outside the docstrings), if we make use of the IPython Sphinx directive, then you can do things like:

.. ipython::

   In [133]: import numpy.random

   @suppress
   In [134]: numpy.random.seed(2358)

   @doctest float
   In [135]: numpy.random.rand(10,2)
   Out[135]:
   array([[ 0.64524308,  0.59943846],
      [ 0.47102322,  0.8715456 ],
      [ 0.29370834,  0.74776844],
      [ 0.99539577,  0.1313423 ],
      [ 0.16250302,  0.21103583],
      [ 0.81626524,  0.1312433 ],
      [ 0.67338089,  0.72302393],
      [ 0.7566368 ,  0.07033696],
      [ 0.22591016,  0.77731835],
      [ 0.0072729 ,  0.34273127]])

And that will test for approximate equality. The rendered output looks clean, something like:

.. ipython::

   In [1]: import numpy.random

   In [2]: numpy.random.rand(10,2)
   Out[2]:
   array([[ 0.64524308,  0.59943846],
      [ 0.47102322,  0.8715456 ],
      [ 0.29370834,  0.74776844],
      [ 0.99539577,  0.1313423 ],
      [ 0.16250302,  0.21103583],
      [ 0.81626524,  0.1312433 ],
      [ 0.67338089,  0.72302393],
      [ 0.7566368 ,  0.07033696],
      [ 0.22591016,  0.77731835],
      [ 0.0072729 ,  0.34273127]])

So this is another option (but it exists outside docstrings and would only be tested when documentation was built).

@ysitu

ysitu commented May 15, 2014

Copy link
Copy Markdown
Contributor

Reducing the output precision using '%.8f' % x should suffice here.

@hagberg

hagberg commented May 15, 2014

Copy link
Copy Markdown
Member

I prefer to test docstrings. It's pretty frustrating to paste in an example and it doesn't work. Testing helps avoid that. But here is another suggestion. For longer examples on how to use a function or module we can create examples in networkx/examples and make a link.

@kmkurn

kmkurn commented May 16, 2014

Copy link
Copy Markdown
Author

Output precision was fixed and references were added to Sphinx file. Please check.

@rafguns

rafguns commented May 22, 2014

Copy link
Copy Markdown
Contributor

@kemskems How were you planning on implementing Katz score? Currently all link prediction algorithms are implemented like this:

def some_predictor(G, u, v):
    # do stuff
    return score

This works fine for the neighbor-based predictors but Katz score, SimRank and others are most easily implemented as matrix operations that calculate scores for all node pairs in one go (similar to the algorithms in nx.algorithms.centrality):

def some_predictor(G):
    # do stuff
    return dict_of_scores  # e.g. {('a', 'b'): 0.2, ('b', 'c'): 0.5, ...}

So perhaps it makes sense to move to an API like this:

def some_predictor(G, u=None, v=None):
    # do stuff
    return score_or_dict

which returns a single score if u and v are nodes or a dict if u and/or v is None (u and/or v might even be lists or iterables of nodes, but maybe things become too complex then). Thoughts?

@kmkurn

kmkurn commented May 23, 2014

Copy link
Copy Markdown
Author

@rafguns I haven't given it much thought yet, but yes I agree that what you said is an issue.

A less complex approach probably like this (already discussed in PR #1117):

def some_predictor(G, ebunch=None):
    if ebunch is None:
        ebunch = nx.non_edges(G)
    yield tuple_of_scores  # e.g. ('a', 'b', 0.5), ('c', 'd', 0.75), ...

but then we have another issue for community-based link prediction algorithms: exception will be raised while iterating if there are some edges in ebunch whose nodes have no community information. We could return a dictionary instead but, as you may have known, it will suddenly consume a lot of memory.

@ysitu ysitu added this to the networkx-1.9 milestone May 28, 2014
@ysitu

ysitu commented May 28, 2014

Copy link
Copy Markdown
Contributor

I like the ebunch idea. I do not think that you need to worry about exceptions caused by missing community information during iteration. The user is supposed to know that garbage input leads to garbage output. But the code can be a little bit more helpful by letting the user know which node does not have community information in the exception message.

Also, #1117 can improved by allowing the user to specify a data key for community information instead of a hard-coded string ('community').

I am marking this for 1.9 so that any interface change can be applied to #1117 before the latter is released to the wild.

@hagberg hagberg mentioned this pull request May 31, 2014
@kmkurn

kmkurn commented Jun 1, 2014

Copy link
Copy Markdown
Author

I have implemented the interface change and user is now allowed to specify node community attribute name as suggested. As for the Katz score, I already tried to implement it using the old interface, but changing its interface turned out to be a non-trivial task so I removed it for now. Also, there are some issues I'd like to discuss so perhaps it is better to do it in another PR.

Regarding the exception message, I am not sure about the best way to tell user which node that has no community information. Printing the node in the message may not be a good idea since the node could be anything; its string representation might not be meaningful at all. Or the user is supposed to handle this?

Comment thread networkx/algorithms/link_prediction.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to "iterable of node pairs". Also applies to all other occurrences below.

Comment thread networkx/algorithms/link_prediction.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

node_u = G.node[u]
try:
    return node_u[community]
except KeyError:
    raise nx.NetworkXAlgorithmError('No community information', u)

This avoids punishing the common case where community information is present.

@ysitu

ysitu commented Jun 1, 2014

Copy link
Copy Markdown
Contributor

I think that this is ready for merging. We can ignore reporting the node without community information for now and add it later if prominent need pops up.

@kmkurn

kmkurn commented Jun 1, 2014

Copy link
Copy Markdown
Author

OK 👍

ysitu added a commit that referenced this pull request Jun 1, 2014
@ysitu
ysitu merged commit 8b79e52 into networkx:master Jun 1, 2014
@kmkurn
kmkurn deleted the link-prediction branch June 1, 2014 15:58
@Mjh9122 Mjh9122 mentioned this pull request Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

5 participants