简体   繁体   English

如何将一系列 numpy ndarrays 作为输入数据来训练 tensorflow 机器学习模型?

[英]How can I have a series of numpy ndarrays as the input data to train a tensorflow machine learning model?

I am trying to build a machine learning model which predicts a single number from a series of numbers.我正在尝试构建一个机器学习模型,该模型可以从一系列数字中预测一个数字。 I am using a Sequential model from the keras API of Tensorflow.我正在使用来自 Tensorflow 的 keras API 的序列模型。

Basically my x data is a Pandas series which contains numpy ndarrays, which contain floats.基本上我的 x 数据是一个 Pandas 系列,其中包含 numpy ndarrays,其中包含浮点数。 My y data is a series of numpy ndarrays of shape (1,1), so basically just a single float value.我的 y 数据是一系列形状为 (1,1) 的 numpy ndarray,所以基本上只是一个浮点值。

You can imagine my dataset to look something like this:你可以想象我的数据集看起来像这样:

Index指数 x data (pandas series) x 数据(熊猫系列) y data (pandas series) y 数据(熊猫系列)
0 0 np.ndarray(shape (1209278,) ) np.ndarray(shape = () )
1 1 np.ndarray(shape (1211140,) ) np.ndarray(shape = () )
2 2 np.ndarray(shape (1418411,) ) np.ndarray(shape = () )
3 3 np.ndarray(shape (1077132,) ) np.ndarray(shape = () )
... ... ... ... ... ...

The type of my x data and y data is, as stated above, a pandas series.如上所述,我的 x 数据和 y 数据的类型是 pandas 系列。 When I try to train my model using the fit function it yields this error:当我尝试使用 fit 函数训练我的模型时,会产生以下错误:

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray) ValueError:无法将 NumPy 数组转换为张量(不支持的对象类型 numpy.ndarray)

I also tried converting the pandas series to a numpy array, but this did not help.我还尝试将 pandas 系列转换为 numpy 数组,但这并没有帮助。 As it seems, the fact that I have a series of differently shaped ndarrays as my input data is the problem itself.看起来,我有一系列不同形状的 ndarrays 作为我的输入数据这一事实本身就是问题所在。

I don't really know what I can do, to fix this error.我真的不知道我能做些什么来修复这个错误。 Which leads me to my question:这引出了我的问题:

How can I have a series of numpy ndarrays as the input data to train a tensorflow machine learning model?如何将一系列 numpy ndarrays 作为输入数据来训练 tensorflow 机器学习模型?

Pandas Data Series does not support a direct conversion to tensors. Pandas 数据系列不支持直接转换为张量。 So I would try first to convert those to list :所以我会先尝试将它们转换为list

X = X.to_list()
Y = Y.to_list()

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

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