Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AssertionError with self-loops from Python but not from command line #26

Closed
RochMoreau opened this issue Apr 25, 2019 · 5 comments
Closed

Comments

@RochMoreau
Copy link

RochMoreau commented Apr 25, 2019

The code given in the README is issuing a AssertionError: Predicted link (981, 981) is a self-loop!, but the terminal command with the exact same training file is doing the job.

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
 in 
      7 
      8 simrank = linkpred.predictors.SimRank(training, excluded=training.edges())
----> 9 simrank_results = simrank.predict(c=0.5)
     10 
     11 evaluation = linkpred.evaluation.EvaluationSheet(simrank_results, test.edges())

~/anaconda3/envs/social-media/lib/python3.7/site-packages/linkpred/predictors/base.py in predict_and_postprocess(*args, **kwargs)
     65                 for u, v in self.excluded:
     66                     try:
---> 67                         del scoresheet[(u, v)]
     68                     except KeyError:
     69                         pass

~/anaconda3/envs/social-media/lib/python3.7/site-packages/linkpred/evaluation/scoresheet.py in __delitem__(self, key)
    193 
    194     def __delitem__(self, key):
--> 195         return dict.__delitem__(self, Pair(key))
    196 
    197     def process_data(self, data, weight='weight'):

~/anaconda3/envs/social-media/lib/python3.7/site-packages/linkpred/evaluation/scoresheet.py in __init__(self, *args)
    125                 "__init__() takes 1 or 2 arguments in addition to self")
    126         # For link prediction, a and b are two different nodes
--> 127         assert a != b, "Predicted link (%s, %s) is a self-loop!" % (a, b)
    128         self.elements = self._sorted_tuple((a, b))
    129 

AssertionError: Predicted link (381, 381) is a self-loop!
>>>

​

@rafguns
Copy link
Owner

rafguns commented Apr 25, 2019

Thanks for the report. Very strange, I'll have to investigate more...

@RochMoreau
Copy link
Author

Thanks for the fast response !
Keep me posted please, I am very interested by this project.
I can provide some more informations if you need (output of terminal command)

@RochMoreau
Copy link
Author

Got it working by removing self-loop edges before calling the predictor. The accuracy is not good though but I don't have any comparison point so I can't say if the bad accuracy is a consequence of this fix.

Here is the code:

G = linkpred.read_network("training.net")

# This line is the fix to avoid the AssertionError
G.remove_edges_from(G.selfloop_edges())

test = G.subgraph(random.sample(G.nodes(), 300))

# Exclude test network from learning phase
training = G.copy()
training.remove_edges_from(test.edges())

simrank = linkpred.predictors.SimRank(training, excluded=training.edges())
simrank_results = simrank.predict(c=0.5)

evaluation = linkpred.evaluation.EvaluationSheet(simrank_results, test.edges())

plt.plot(evaluation.recall(), evaluation.precision())

@rafguns
Copy link
Owner

rafguns commented Apr 30, 2019

Ah, this is the code I gave as an example in #18, right? If your data contains self-loops, you should indeed remove them. If you call linkpred from the command-line, it will do something similar.

Low accuracy indicates that this predictor does not work well on your data but has nothing to do with the self-loops. You may want to try experimenting with a few of the other predictors to see if they give better results.

@rafguns
Copy link
Owner

rafguns commented Apr 30, 2019

I think this can be closed, but please reopen if there is still an issue here relating to the self-loops. Thanks!

@rafguns rafguns closed this as completed Apr 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants