繁体   English   中英

如何将火炬张量转换为熊猫数据帧?

[英]How to convert torch tensor to pandas dataframe?

我想将火炬张量转换为pd.DataFrame数据帧,但是通过使用pd.DataFrame我得到了一个用张量而不是数值填充的数据帧。

import torch
import pandas as  pd
x = torch.rand(4,4)
px = pd.DataFrame(x)

这是我在变量资源管理器中单击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)

我通过首先将 torch 转换为 numpy 找到了一种可能的方法:

import torch
import pandas as  pd

x = torch.rand(4,4)
px = pd.DataFrame(x.numpy())

您可以使用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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