淘先锋技术网

首页 1 2 3 4 5 6 7

LinkPredict

使用一阶方法

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

根据两个节点之间的共享链接的数量,该度量是在2003年引入的,以预测网络中缺少的链接。

在这里插入图片描述

adjacency = karate_club()
aa = AdamicAdar()
aa.fit(adjacency)

edges = [(0, 5), (4, 7), (15, 23), (17, 30)]
y_true = is_edge(adjacency, edges)
y_true
# [ True, False, False, False]

scores = aa.predict(edges)
y_pred = whitened_sigmoid(scores) > 0.75
accuracy_score(y_true, y_pred)
# 1.0

有向图

graph = painters(metadata=True)
adjacency = graph.adjacency
names = graph.names
names

picasso = 0

aa.fit(adjacency)

scores = aa.predict(picasso)
scores
#[1.24266982, 0.62133491, 0.        , 0.        , 0.        , 0.        , 0.        , 0.62133491, 0.62133491, 0.        , 0.62133491, 1.24266982, 0.62133491, 0.        ]

names[argsort(-scores)]
# ['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']

二部图

graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names = graph.names
names

inception = 0
aa.fit(biadjacency)
scores = aa.predict(inception)
scores
names[argsort(-scores)]
['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']

JaccardIndex

它是由通过邻居总数归一化的普通邻居数量来计算的。它用于测量两个有限样品集之间的相似性,并定义为交点的大小除以样品集的结合大小。

在这里插入图片描述

graph = karate_club(metadata=True)
adjacency = graph.adjacency

ji = JaccardIndex()
ji.fit(adjacency)

edges = [(0, 5), (4, 7), (15, 23), (17, 30)]
y_true = is_edge(adjacency, edges)
y_true

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

accuracy_score(y_true, y_pred)
# 0.5

有向图

graph = painters(metadata=True)
adjacency = graph.adjacency
names = graph.names
names

picasso = 0
ji.fit(adjacency)
scores = ji.predict(picasso)
scores
names[argsort(-scores)]
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')

二部图

graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names = graph.names
names

inception = 0

ji.fit(biadjacency)

scores = ji.predict(inception)
scores

names[argsort(-scores)]
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')