Я пишу следующий код с использованием разреженных матриц
def Transition_Matrix(adj,beta):
Iden = sp.eye(adj.shape[0])
adj = sp.coo_matrix(adj)
rowsum = np.array(adj.sum(1))
degree_mat_inv_sqrt = sp.diags(np.power(rowsum, -0.5).flatten()
adj_normalized = adj.dot(degree_mat_inv_sqrt).tocoo()
matrix_=Iden-(beta * adj_normalized)
****print(matrix_)**** this is the result
here beta=0.9
1. -0.3 -0.28460499 ... -0.36742346 0.
0. ]
[-0.225 1. -0.28460499 ... 0. 0.
0. ]
[-0.225 -0.3 1. ... 0. -0.25980762
0. ]
...
[-0.225 0. 0. ... 1. -0.25980762
-0.21828206]
[ 0. 0. -0.28460499 ... -0.36742346 1.
-0.21828206]
[ 0. 0. 0. ... -0.36742346 -0.25980762
1. ]]
matrixinv=np.linalg.pinv(matrix_)
Transition=(1-beta)*matrixinv
return Transition
вход - разреженная матрица: adj = nx.to_scipy_sparse_matrix(G)
Как избежать этих отрицательных значений? любая помощь будет оценена