首页 > 解决方案 > 为什么缩放然后转换我的火车数据会导致“ValueError:设置带有序列的数组元素”?

问题描述

我有一个包含每小时时间序列信息的 CSV 数据集。我正在尝试使用长短期记忆(LSTM)机器学习创建一个预测模型。

我正在尝试使用 Sklearn 的 MinMax Scaler 缩放训练数据,然后使用 scale() 转换数据的比例。尝试时,它会返回:“ ValueError: setting an array element with a sequence ”并将这两个步骤作为问题的一部分。

关于如何解决 LSTM 问题中的这个错误的任何想法?

编辑:寻找其他地方表明解决方案可能涉及序列输入的长度不均匀,但我仍然不清楚如何解决这个问题。

这是错误中引用的代码(注释 113 和 42)。

from sklearn.preprocessing import MinMaxScaler

39 # scale train and test data to [-1, 1]  
40 def scale(train, test):
41    scaler = MinMaxScaler(feature_range=(-1, 1))
42    scaler = scaler.fit(train)

...

112 # transform the scale of the data
113 scaler, train_scaled, test_scaled = scale(train, test)

我省略了几个我认为不重要的中间步骤,但也许我错了。错误如下所示:

Using TensorFlow backend.
Traceback (most recent call last):
  File "code.py", line 113, in <module>
   scaler, train_scaled, test_scaled = scale(train, test)
  File "code.py", line 42, in scale
   scaler = scaler.fit(train)
  File "C:\...\sklearn\preprocessing\data.py", line 325, in fit
   return self.partial_fit(X, y)
  File "C:\...\sklearn\preprocessing\data.py", line 353, in partial_fit
   force_all_finite="allow-nan")
  File "C:\...\sklearn\utils\validation.py", line 496, in check_array
   array = np.asarray(array, dtype=dtype, order=order)
  File "C:\...\numpy\core\numeric.py", line 538, in asarray
   return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.

对于为什么代码适用于指南模板而不是我的代码的最佳猜测是,我正在使用的指南只有两列(日期和数字)......

"Month","Sales"
"1-01",266.0
"1-02",145.9
"1-03",183.1
"1-04",119.3

而我的 CSV 有四列(时间戳和三个数字):

timeStamp              demand   precip  temp
2012-01-01  00:00:00    4937.5  0       46.13
2012-01-01  01:00:00    4752.1  0       45.89
2012-01-01  02:00:00    4542.6  0.01    45.04
2012-01-01  03:00:00    4357.7  0       45.03

但最终,我是 ML 的新手并且迷路了。

标签: pythontensorflowmachine-learningscikit-learntime-series

解决方案


推荐阅读