用转置版本填充矩阵 [英] Fill matrix with transposed version

查看:108
本文介绍了用转置版本填充矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个成对矩阵:

>>> m
     a    b    c   d
a  1.0  NaN  NaN NaN
b  0.5  1.0  NaN NaN
c  0.6  0.0  1.0 NaN
d  0.5  0.4  0.3 1.0

我想用与左下角相同的值替换右上角的NaN:

I want to replace the NaN in the the top right with the same values as in the bottom left:

>>> m2
     a    b    c    d
a  1.0  0.5  0.6  0.5
b  0.5  1.0  0.0  0.4
c  0.6  0.0  1.0  0.3
d  0.5  0.4  0.3  1.0

我可以通过交换列和索引来做到这一点:

I can do it by swapping columns and indexes:

cols = m.columns
idxs = m.index

for c in cols:
    for i in idxs:
        m[i][c] = m[c][i]

但是我的实际数据太慢了,我敢肯定有一种方法可以一步一步完成.我知道我可以用"m.T"生成右上角的版本,但是我不知道如何用非NaN值替换NaN以获得完整的矩阵.在numpy中可能有一种单步执行此操作的方法,但是我不知道矩阵代数.

But that's slow with my actual data, and I'm sure there's a way to do it in one step. I know I can generate the upper right version with "m.T" but I don't know how to replace NaN with non-NaN values to get the complete matrix. There's probably a single-step way to do this in numpy, but I don't know from matrix algebra.

推荐答案

如何( 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆