點選檢視程式碼
import numpy as np
import pandas as pd
import sympy as sp
sp.init_printing(use_unicode=True)
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['Times New Roman + SimSun + WFM Sans SC']
plt.rcParams['mathtext.fontset']='cm'
# Times New Roman + SimSun + WFM Sans SC
# simsum宋體, times new roman -*, simhei黑體, kaiti楷體,
# dengxian等線, fangsong仿宋, Microsoft Yahei微軟雅黑
plt.rcParams['axes.unicode_minus']=False
plt.rcParams['figure.dpi'] = 200
# plt.rcParams['figure.figsize'] = [4, 3]
# plt.rcParams['font.size'] = 12
plt.rcParams['xtick.direction'] = 'in'
plt.rcParams['ytick.direction'] = 'in'
from scipy.sparse.linalg import eigs
L = np.array([[0, 1, 0, 1, 1, 1], [0, 0, 0, 1, 1, 1],
[1, 1, 0, 1, 0, 0], [0, 0, 0, 0, 1, 1],
[0, 0, 1, 0, 0, 1], [0, 0, 1, 0, 0, 0]])
r = np.sum(L, axis=1, keepdims=True)
P = L/r
d = 0.85
Ptilde = (1-d)/len(P)*np.ones_like(P) + d*P
val, vec = eigs(Ptilde.T, 1)
V = vec.flatten()
V = V.real
V /= sum(V)
print(V)
fig, ax = plt.subplots()
ax.bar(range(1, len(V)+1), V, width=0.6)
ax.set_xlabel('Page')
ax.set_ylabel('Rank')
fig.show()