First-order methods

[1]:
from numpy import argsort

from sknetwork.classification import accuracy_score
from sknetwork.data import karate_club, painters, movie_actor
from sknetwork.linkpred import JaccardIndex, AdamicAdar, is_edge, whitened_sigmoid

Adamic-Adar

[2]:
adjacency = karate_club()
[3]:
aa = AdamicAdar()
aa.fit(adjacency)
[3]:
AdamicAdar()
[4]:
edges = [(0, 5), (4, 7), (15, 23), (17, 30)]
y_true = is_edge(adjacency, edges)

scores = aa.predict(edges)
y_pred = whitened_sigmoid(scores) > 0.75

accuracy_score(y_true, y_pred)
[4]:
1.0
[5]:
# directed graph
graph = painters(metadata=True)
adjacency = graph.adjacency
names = graph.names
[6]:
picasso = 0
[7]:
aa.fit(adjacency)
[7]:
AdamicAdar()
[8]:
scores = aa.predict(picasso)

names[argsort(-scores)]
[8]:
array(['Pablo Picasso', 'Paul Cezanne', 'Claude Monet', 'Edgar Degas',
       'Vincent van Gogh', 'Henri Matisse', 'Pierre-Auguste Renoir',
       'Michel Angelo', 'Edouard Manet', 'Peter Paul Rubens', 'Rembrandt',
       'Gustav Klimt', 'Leonardo da Vinci', 'Egon Schiele'], dtype='<U21')
[9]:
# bipartite graph
graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names = graph.names
[10]:
inception = 0
[11]:
aa.fit(biadjacency)
[11]:
AdamicAdar()
[12]:
scores = aa.predict(inception)

names[argsort(-scores)]
[12]:
array(['Inception', 'The Dark Knight Rises', 'The Great Gatsby',
       'Aviator', 'Midnight In Paris', 'The Big Short', 'Drive',
       'La La Land', 'Crazy Stupid Love', 'Vice',
       'The Grand Budapest Hotel', '007 Spectre', 'Inglourious Basterds',
       'Murder on the Orient Express', 'Fantastic Beasts 2'], dtype='<U28')

Jaccard Index

[13]:
adjacency = karate_club()
[14]:
ji = JaccardIndex()
ji.fit(adjacency)
[14]:
JaccardIndex()
[15]:
edges = [(0, 5), (4, 7), (15, 23), (17, 30)]
y_true = is_edge(adjacency, edges)

scores = ji.predict(edges)
y_pred = whitened_sigmoid(scores) > 0.75

accuracy_score(y_true, y_pred)
[15]:
0.5
[16]:
# directed graph
graph = painters(metadata=True)
adjacency = graph.adjacency
names = graph.names
[17]:
picasso = 0
[18]:
ji.fit(adjacency)
[18]:
JaccardIndex()
[19]:
scores = ji.predict(picasso)

names[argsort(-scores)]
[19]:
array(['Pablo Picasso', 'Paul Cezanne', 'Claude Monet',
       'Pierre-Auguste Renoir', 'Henri Matisse', 'Edgar Degas',
       'Vincent van Gogh', 'Michel Angelo', 'Edouard Manet',
       'Peter Paul Rubens', 'Rembrandt', 'Gustav Klimt',
       'Leonardo da Vinci', 'Egon Schiele'], dtype='<U21')
[20]:
# bipartite graph
graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names = graph.names
[21]:
inception = 0
[22]:
ji.fit(biadjacency)
[22]:
JaccardIndex()
[23]:
scores = ji.predict(inception)

names[argsort(-scores)]
[23]:
array(['Inception', 'The Dark Knight Rises', 'The Great Gatsby',
       'Aviator', 'Midnight In Paris', 'The Big Short', 'Drive',
       'La La Land', 'Crazy Stupid Love', 'Vice',
       'The Grand Budapest Hotel', '007 Spectre', 'Inglourious Basterds',
       'Murder on the Orient Express', 'Fantastic Beasts 2'], dtype='<U28')