马尔科夫模型中转移矩阵的计算

  • 发布日期:2019-09-29
  • 难度:中等
  • 类别:Web挖掘
  • 标签:Python、NetworkX、HITS

1. 问题描述

马尔科夫模型中转移矩阵的计算

2. 程序实现

In [1]:
import numpy as np

#web事务集合
list_web_page = [1,2,3,4,5];
list_web_transactions = [[1,2,3,4],[1,3,5],[1,3,4],[2,3,4],[4],[2,3]];
list_web_transactions_frequence = [2,7,2,3,6,2];

#初始化转移矩阵
array_transition_matrix = np.zeros((len(list_web_page),len(list_web_page)));
array_transition_matrix_denominator = np.zeros((len(list_web_page),1));

#应用马尔科夫模型计算转移矩阵
for num_transaction in range(0,len(list_web_transactions)):
    list_web_transaction = list_web_transactions[num_transaction];
    int_web_transaction_frequence = list_web_transactions_frequence[num_transaction];
    for num_page in range(0,len(list_web_transaction)-1):
        initial_page = list_web_transaction[num_page]-1;
        next_page = list_web_transaction[num_page+1]-1;
        array_transition_matrix[initial_page][next_page] += int_web_transaction_frequence;
        array_transition_matrix_denominator[initial_page] += int_web_transaction_frequence;
    second_end_page = list_web_transaction[len(list_web_transaction)-1]-1;
    array_transition_matrix_denominator[second_end_page] += int_web_transaction_frequence;   

array_transition_matrix = array_transition_matrix/array_transition_matrix_denominator;  
print('转移矩阵array_transition_matrix: \n',array_transition_matrix)
转移矩阵array_transition_matrix: 
 [[0.         0.18181818 0.81818182 0.         0.        ]
 [0.         0.         1.         0.         0.        ]
 [0.         0.         0.         0.4375     0.4375    ]
 [0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.        ]]