马尔科夫模型中转移矩阵的计算
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)