Numpy数组整形:ValueError:无法将大小为360的数组整形为形状(1,60,1)

bejyjqdl  于 2023-05-07  发布在  其他
关注(0)|答案(1)|浏览(241)

我有一个np数组,看起来像这样:
[[-4.67069069e+02 -6.36386712e+00 -2.04863793e-02 -6.60781838e+00 -6.23597525e+00 -6.36389266e+00] [-4.67069069e+02 -6.36387170e+00 -2.04863796e-02 -6.60782580e+00 -6.23597425e+00 -6.36388226e+00]
长度为60。
我的代码如下:

from sklearn.preprocessing import MinMaxScaler
...
scaler = MinMaxScaler(feature_range=(0, 1))
...
window_size = 60
last_sequence = val_data[-window_size:]
last_sequence = scaler.transform(last_sequence)
last_sequence = last_sequence.reshape((1, window_size, 1))

错误消息:
last_sequence = last_sequence.reshape((1,window_size,1))ValueError:无法将大小为360的数组整形为形状(1,60,1)
我做错了什么?
我已经尝试创建一个新的numpy数组,它没有工作。

cbeh67ev

cbeh67ev1#

这个错误消息在某种程度上是自我解释的,但是我给了您一些注解和解决方法,它们可能适合您的用例,也可能不适合您的用例

window_size = 60

# `last_sequence` will have size 60 in the first dimension, other dimensions can be different
last_sequence = val_data[-window_size:]

# check the size of all dimensions
print(last_sequence.shape)  # notice that the shape is (60, other_dim, ...)

last_sequence = scaler.transform(last_sequence)  # the dimension will still be 60 in first dim, but there are other dimensions of your data

# this line wont work as you cannot squeeze 360 values in a array that has only 60 entries

last_sequence = last_sequence.reshape((1, window_size, 1))

# you could do this instead:
last_sequence = last_sequence.reshape((1, window_size, -1))  # passing `-1` will cause this dimension to be automatically of the necessary size to fit to the data. If this is what you want depends on your application

相关问题