PageRank

This notebook illustrates the classification of the nodes of a graph by PageRank, based on the labels of a few nodes.

[1]:
from IPython.display import SVG
[2]:
import numpy as np
[3]:
from sknetwork.data import karate_club, painters, movie_actor
from sknetwork.classification import PageRankClassifier, get_accuracy_score
from sknetwork.visualization import svg_graph, visualize_bigraph

Graphs

[4]:
graph = karate_club(metadata=True)
adjacency = graph.adjacency
position = graph.position
labels_true = graph.labels
[5]:
labels = {i: labels_true[i] for i in [0, 33]}
[6]:
pagerank = PageRankClassifier()
labels_pred = pagerank.fit_predict(adjacency, labels)
[7]:
accuracy = get_accuracy_score(labels_true, labels_pred)
np.round(accuracy, 2)
[7]:
np.float64(0.97)
[8]:
image = svg_graph(adjacency, position, labels=labels_pred, seeds=labels)
SVG(image)
[8]:
../../_images/tutorials_classification_pagerank_10_0.svg
[9]:
# probability distribution over labels
label = 1
probs = pagerank.predict_proba()
scores = probs[:,label]
[10]:
image = svg_graph(adjacency, position, scores=scores, seeds=labels)
SVG(image)
[10]:
../../_images/tutorials_classification_pagerank_12_0.svg

Directed graphs

[11]:
graph = painters(metadata=True)
adjacency = graph.adjacency
position = graph.position
names = graph.names
[12]:
rembrandt = 5
klimt = 6
cezanne = 11
labels = {cezanne: 0, rembrandt: 1, klimt: 2}
[13]:
pagerank = PageRankClassifier()
labels_pred = pagerank.fit_predict(adjacency, labels)
[14]:
image = svg_graph(adjacency, position, names, labels=labels_pred, seeds=labels)
SVG(image)
[14]:
../../_images/tutorials_classification_pagerank_17_0.svg
[15]:
# probability distribution over labels
probs = pagerank.predict_proba()
scores = probs[:,0]
[16]:
image = svg_graph(adjacency, position, names, scores=scores, seeds=[cezanne])
SVG(image)
[16]:
../../_images/tutorials_classification_pagerank_19_0.svg

Bipartite graphs

[17]:
graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names_row = graph.names_row
names_col = graph.names_col
[18]:
inception = 0
drive = 3
budapest = 8
[19]:
labels_row = {inception: 0, drive: 1, budapest: 2}
[20]:
pagerank = PageRankClassifier()
pagerank.fit(biadjacency, labels_row)
labels_row_pred = pagerank.labels_row_
labels_col_pred = pagerank.labels_col_
[21]:
image = visualize_bigraph(biadjacency, names_row, names_col, labels_row_pred, labels_col_pred, seeds_row=labels_row)
SVG(image)
[21]:
../../_images/tutorials_classification_pagerank_25_0.svg
[22]:
# probability distribution over labels
probs_row = pagerank.predict_proba()
probs_col = pagerank.predict_proba(columns=True)
[23]:
label = 1
scores_row = probs_row[:,label]
scores_col = probs_col[:,label]
[24]:
image = visualize_bigraph(biadjacency, names_row, names_col, scores_row=scores_row, scores_col=scores_col,
                    seeds_row=labels_row)
SVG(image)

[24]:
../../_images/tutorials_classification_pagerank_28_0.svg