简体   繁体   English

如何将多个 pandas 数据帧(例如二维矩阵)转换为张量?

[英]How to convert multiple pandas data-frame (e.g. 2D matrices) into a tensor?

My data are currently in Pandas data frame and the shape of each data frame like: Matrix1, matrix2, and matrix3 each having shapes like 4 rows and 19 columns.我的数据目前在 Pandas 数据框中,每个数据框的形状如下:Matrix1、matrix2 和 matrix3,每个数据框的形状都像 4 行和 19 列。 Colum names are different for each matrix.每个矩阵的列名称不同。

How do I convert these into a tensor of a shape 3,4,19?如何将这些转换为形状为 3、4、19 的张量? Can anybody help to solve this?任何人都可以帮助解决这个问题吗?

Let's say you have the following dataframes:假设您有以下数据框:

data = [pd.DataFrame(np.zeros((4,19))) for x in range(3)]  #shape 4, 19 

list_of_matrices = [np.array(df) for df in data]

That is:那是:

[array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0.]]),
 array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0.]]),
 array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0.]])]

Then turning them into one single tensor can be done this way:然后可以通过以下方式将它们变成一个单一的张量:

import torch
list_of_matrices = [np.array(df) for df in data]

torch.tensor(np.stack(list_of_matrices))

list_of_tensors = [torch.tensor(np.array(df)) for df in data]
torch.stack(list_of_tensors)

which gives:这使:

tensor([[[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]],

        [[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]],

        [[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]]],
       dtype=torch.float64)

暂无
暂无

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

相关问题 如何批量解析(例如,提取特定的文本元素).html文件目录,然后将每个元素添加到熊猫数据框? - How to batch parse (e.g., extract specific textual elements) a directory of .html files then add each element to a pandas data frame? 操作 pandas 数据框列中的列表(例如,除以另一列) - Manipulate lists in a pandas data frame column (e.g. divide by another column) 如何在 Mayavi 中使用张量场数据,例如 HyperStreamLine 或 TensorGlyp 可视化? - How do I use tensor field data with Mayavi for e.g. HyperStreamLine or TensorGlyp visualization? 将深度嵌套的Dictionary转换为pandas数据框架? - Convert deeply nested Dictionary into pandas data-frame? 如何从给定的熊猫数据帧创建子数据帧? - How to create a sub data-frame from a given pandas data-frame? 如何将这样的嵌套 JSON 转换为数据框? 我尝试使用熊猫 json_normalize 但仍然没有得到正确的数据框 - How to convert nested JSON like this to a Data-frame? I tried using pandas json_normalize but still doesn't get a proper Data-frame 如何将出生年份的熊猫数据框列转换为年龄? (例如'1991'-> 28) - How can I convert a pandas dataframe column with birth year to age? (e.g. '1991' -> 28) 如何从 pandas 数据帧中的多列创建单个饼图 - How to create a single pie chart from multiple columns in a pandas data-frame 如何在pytorch中将3D张量转换为2D张量? - How to convert 3D tensor to 2D tensor in pytorch? 如何将 Python 功能数据导入 Pandas 数据框 - How to import Python Fuction data into Pandas Data-frame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM