只挑重点说:
如果你的代码也这样报错:
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.
这就是在提示你,无法处理csv文件中的字符类型的数据。
这时候你可能会想着使用astype
方法强制转换类型,写出这些句子
self.y = torch.tensor(y.astype(np.float32))
但是牵一发动全身,有时候你可能只需要调整一下你的库的版本,比如:
!pip install pandas==1.4.2
这个版本的pandas同时能够处理字符串类型的数据,不需要再改变数据类型了,直接用它!