ajk*_*hol 4 python numpy matrix
我正在尝试计算具有以下方程式的矩阵。
S = (D^?1/2) * W * (D^?1/2)
Run Code Online (Sandbox Code Playgroud)
其中D是这种形式的对角矩阵:
array([[ 0.59484625, 0. , 0. , 0. ],
[ 0. , 0.58563893, 0. , 0. ],
[ 0. , 0. , 0.58280472, 0. ],
[ 0. , 0. , 0. , 0.58216725]])
Run Code Online (Sandbox Code Playgroud)
和W:
array([[ 0. , 0.92311635, 0.94700586, 0.95599748],
[ 0.92311635, 0. , 0.997553 , 0.99501248],
[ 0.94700586, 0.997553 , 0. , 0.9995501 ],
[ 0.95599748, 0.99501248, 0.9995501 , 0. ]])
Run Code Online (Sandbox Code Playgroud)
我试图D^-1/2通过使用numpyfunction linalg.matrix_power(D,-1/2)和numpy.power(D,-1/2)and matrix_power函数引发TypeError: exponent must be an integer以及numpy.power函数引发进行计算RuntimeWarning: divide by zero encountered in power。
如何计算对角矩阵的负功率-1/2。请帮忙。
如果您可以更新D(例如您自己的答案),则只需更新其对角线索引处的项目,然后致电np.dot:
>>> D[np.diag_indices(4)] = 1/ (D.diagonal()**0.5)
>>> np.dot(D, W).dot(D)
array([[ 0. , 0.32158153, 0.32830723, 0.33106193],
[ 0.32158153, 0. , 0.34047794, 0.33923936],
[ 0.32830723, 0.34047794, 0. , 0.33913717],
[ 0.33106193, 0.33923936, 0.33913717, 0. ]])
Run Code Online (Sandbox Code Playgroud)
或创建一个新的零点数组,然后用对角元素填充1/ (D.diagonal()**0.5):
>>> arr = np.zeros(D.shape)
>>> np.fill_diagonal(arr, 1/ (D.diagonal()**0.5))
>>> np.dot(arr, W).dot(arr)
array([[ 0. , 0.32158153, 0.32830723, 0.33106193],
[ 0.32158153, 0. , 0.34047794, 0.33923936],
[ 0.32830723, 0.34047794, 0. , 0.33913717],
[ 0.33106193, 0.33923936, 0.33913717, 0. ]])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1927 次 |
| 最近记录: |