Niv*_*Niv 11 python pandas pytorch
我想将 Torch 张量转换为 Pandas 数据帧,但通过使用pd.DataFrame我得到了一个填充张量而不是数值的数据帧。
import torch
import pandas as  pd
x = torch.rand(4,4)
px = pd.DataFrame(x)
Run Code Online (Sandbox Code Playgroud)
这是我px在变量资源管理器中单击时得到的结果:
0   1   2   3
tensor(0.3880)  tensor(0.4598)  tensor(0.4239)  tensor(0.7376)
tensor(0.4174)  tensor(0.9581)  tensor(0.0987)  tensor(0.6359)
tensor(0.6199)  tensor(0.8235)  tensor(0.9947)  tensor(0.9679)
tensor(0.7164)  tensor(0.9270)  tensor(0.7853)  tensor(0.6921)
Run Code Online (Sandbox Code Playgroud)
    Niv*_*Niv 18
我通过首先将 torch 转换为 numpy 找到了一种可能的方法:
import torch
import pandas as  pd
x = torch.rand(4,4)
px = pd.DataFrame(x.numpy())
Run Code Online (Sandbox Code Playgroud)
        Dis*_*ani 10
您可以使用更改类型 astype
px = pd.DataFrame(x).astype("float")
px
          0         1         2         3
0  0.847408  0.714524  0.286006  0.165475
1  0.136359  0.384073  0.398055  0.437550
2  0.843704  0.301536  0.576983  0.231726
3  0.293576  0.075563  0.811282  0.881705
Run Code Online (Sandbox Code Playgroud)