简体   繁体   中英

How to calculate -1/2 power of the matrix in python

I want to calculate the -1/2 power of the degree matrix in python. I know there is a great package to calculate the normalized graph laplacian(L_norm = I - D^{-1/2}AD^{-1/2}, A is the adjacency matrix) in networkx . But I only need the D^{-1/2}.

I tried numpy.linalg.matrix_power , but it supports only integer.

raise TypeError("exponent must be an integer") TypeError: exponent must be an integer

is there any way to calculate the -1/2 power of the matrix?

You can use scipy.linalg.fractional_matrix_power for fractional power of a matrix.
Example from Docs

>>> from scipy.linalg import fractional_matrix_power
>>> a = np.array([[1.0, 3.0], [1.0, 4.0]])
# fractional power of a matrix
>>> b = fractional_matrix_power(a, 0.5)
>>> b
array([[ 0.75592895,  1.13389342],
       [ 0.37796447,  1.88982237]])
>>> np.dot(b, b)      # Verify square root
array([[ 1.,  3.],
       [ 1.,  4.]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM