In [1]:
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
Creating a network:
In [2]:
maint = 10
np.random.seed(0)
A = np.random.choice((0, 1), size=(maint, maint))
A
Out[2]:
Normaleiddio i'w wneud yn matrics stochastig.
In [3]:
symiau_rhes = A.sum(axis=1)
M = A / symiau_rhes[:, np.newaxis]
np.round(M, 2)
Out[3]:
Creu graff o'r rhwydwaith.
In [4]:
G = nx.from_numpy_array(M, create_using=nx.DiGraph())
In [5]:
plt.figure()
nx.draw(G)
plt.savefig("graph.pdf")
Cyfrifo'r pagerank:
In [6]:
nx.pagerank(G, alpha=1)
Out[6]:
Codi'r matrix i bwer uchel.
In [7]:
np.round(np.linalg.matrix_power(M, 2000000), 2)
Out[7]:
Esbonio sut mae hyn yn cyfateb â'r problem gwerthoedd eigen.
In [8]:
gwerthoedd_e, fectorau_e = np.linalg.eig(M.T)
np.real(np.round(fectorau_e[:,0] / sum(fectorau_e[:,0]), 2))
Out[8]:
In [ ]: