为什么pandas.core.series.Series有时无法在Python中转换为火炬张量?

问题描述 投票:0回答:1

我有一个数据框,其中我选择了两列:

X_train, X_test, y_train, y_test = train_test_split(df["EnergyFront"], df["particle"], test_size=0.2) 

X_train和X_test的类型都是pandas.core.series.Series,结果非常相似:

IMAGE

我可以将X_train转换为火炬张量:

X_train = torch.Tensor(X_train) 

但是,当我尝试对X_test执行相同操作时:

X_test = torch.Tensor(X_test) 

我收到以下错误:

ValueError                                Traceback (most recent call last)
<ipython-input-174-14117eb3ce4e> in <module>()
----> 1 X_test = torch.Tensor(X_test)

ValueError: could not determine the shape of object type 'Series'

我该如何解决?顺便说一下,我正在Google合作实验室上运行。

python pandas pytorch series tensor
1个回答
0
投票

此问题在此处描述:https://github.com/pytorch/pytorch/pull/7583为了确定系列的形状,他们尝试访问索引为0的元素。如果找不到该元素,则会发生此错误。在您的情况下,可能是因为X_test不包含整个系列的第一个元素。

我相信针对您的情况的有效解决方案是将X_test转换为类似这样的数组:

X_test = torch.Tensor(X_test.to_numpy())
© www.soinside.com 2019 - 2024. All rights reserved.