我正在尝试将从 CSV 文件读取的 pandas 数据帧转换为 pytorch 张量,但出现类型错误。
我尝试这样做:
df = pandas.DataFrame({"spam": [1, 2, 3, 4], "eggs": [5, 6, 7, 8], "ham": [9, 10, 11, 12]})
print(type(df))
t = torch.from_numpy(df.values)
dataframe = pandas.read_csv('dataset.csv')
print(type(dataframe))
tens = torch.from_numpy(dataframe.values)
Run Code Online (Sandbox Code Playgroud)
这对于 df 来说非常有效,但对于 dataframe 会引发类型错误
TypeError: can't convert np.ndarray of type numpy.object_. The only supported types are: float64, float32, float16, complex64, complex128, int64, int32, int16, int8, uint8, and bool.
Run Code Online (Sandbox Code Playgroud)
两种类型完全相同
<class 'pandas.core.frame.DataFrame'>
Run Code Online (Sandbox Code Playgroud)
可能出了什么问题?