将 numpy 数组附加到 class 列表会更改 class 列表的形状
Appending numpy array to class list changes the shape of class list
我从我的训练集和测试集开始。它们是 NumPy 数组。
然后我创建一个变量 history = x for x in train。这是一个 class 列表。
对于 len(test) 范围内的 i,我进行预测。在这个 forecast() 函数中,历史被制成一个形状为 (30, 72, 7) 的数组,然后展平为 (2160, 7)。在循环中,在迭代进入下一步之前,测试被附加到历史记录中:history.append(test[i, :]).
下次迭代运行s时,再次尝试运行 forecast()时停止,因为数组历史的形状为(31,),无法展平。
我怀疑是类型问题还是 history.append(test[i, :])。但它是哪一个?我该如何解决这个问题?
相关函数如下:
# evaluate a single model
def evaluate_model(train, test, n_input):
print("ENTERED EVALUATE_MODEL!")
# fit model
model = build_model(train, n_input)
# history is a list of weekly data
history = [x for x in train]
print("HISTORY TYPE after declaration: ", type(history))
# walk-forward validation over each week
predictions = list()
for i in range(len(test)):
print("Test.shape(): ", test.shape)
print("Round: ", i+1)
# predict the week
yhat_sequence = forecast(model, history, n_input)
print("yhat_sequence type: ", type(yhat_sequence))
# store the predictions
predictions.append(yhat_sequence)
# get real observation and add to history for predicting the next week
#test = np.array(test)
# print("TEST SHAPE :", test.shape)
print("TEST ", test)
print("History ", history)
print("HISTORY TYPE BEFORE APPEND: ", type(history))
print("TEST TYPE BEFORE APPEND: ", type(test))
history.append(test[i, :])
#test = test.tolist()
print("TEST after: ", type(test))
print("HISTORY after: ", type(history))
# evaluate predictions days for each week
predictions = array(predictions)
score, scores = evaluate_forecasts(test[:, :, 0], predictions)
return score, scores
# Make a forecast.
def forecast(model, history, n_input):
print("forecast()")
# flatten data
data = array(history) #History is entered again each time. But for the second round this is (31,) in shape...
print("data(history) shape in forecast(): ")
print(data.shape)
data = data.reshape((data.shape[0]*data.shape[1], data.shape[2])) #...so then this reshape doesn't work.
print(data.shape)
# retrieve last observations for input data
# For multivariate, make sure to use all features.
input_x = data[-n_input:, :]
# reshape into [1, n_input, 1]
# We need to change the shape as well to take all features.
input_x = input_x.reshape((1, input_x.shape[0], input_x.shape[1]))
# forecast the next week
yhat = model.predict(input_x, verbose=0)
# we only want the vector forecast
yhat = yhat[0]
return yhat
如果你有一个列表(列表或数组)可以变成一个
(30, 72, 7)
数组。您唯一可以添加到该列表的是
(1, 72, 7)
形状数组,这将构成一个 (31, 72, 7) 数组。
任何其他内容都会导致 (31,) object dtype 数组(或错误)。如果 numpy
足够新,我还会收到“参差不齐的数组”警告。
因此在检查 shape
时还要检查 dtype
。
我从我的训练集和测试集开始。它们是 NumPy 数组。
然后我创建一个变量 history = x for x in train。这是一个 class 列表。
对于 len(test) 范围内的 i,我进行预测。在这个 forecast() 函数中,历史被制成一个形状为 (30, 72, 7) 的数组,然后展平为 (2160, 7)。在循环中,在迭代进入下一步之前,测试被附加到历史记录中:history.append(test[i, :]).
下次迭代运行s时,再次尝试运行 forecast()时停止,因为数组历史的形状为(31,),无法展平。
我怀疑是类型问题还是 history.append(test[i, :])。但它是哪一个?我该如何解决这个问题?
相关函数如下:
# evaluate a single model
def evaluate_model(train, test, n_input):
print("ENTERED EVALUATE_MODEL!")
# fit model
model = build_model(train, n_input)
# history is a list of weekly data
history = [x for x in train]
print("HISTORY TYPE after declaration: ", type(history))
# walk-forward validation over each week
predictions = list()
for i in range(len(test)):
print("Test.shape(): ", test.shape)
print("Round: ", i+1)
# predict the week
yhat_sequence = forecast(model, history, n_input)
print("yhat_sequence type: ", type(yhat_sequence))
# store the predictions
predictions.append(yhat_sequence)
# get real observation and add to history for predicting the next week
#test = np.array(test)
# print("TEST SHAPE :", test.shape)
print("TEST ", test)
print("History ", history)
print("HISTORY TYPE BEFORE APPEND: ", type(history))
print("TEST TYPE BEFORE APPEND: ", type(test))
history.append(test[i, :])
#test = test.tolist()
print("TEST after: ", type(test))
print("HISTORY after: ", type(history))
# evaluate predictions days for each week
predictions = array(predictions)
score, scores = evaluate_forecasts(test[:, :, 0], predictions)
return score, scores
# Make a forecast.
def forecast(model, history, n_input):
print("forecast()")
# flatten data
data = array(history) #History is entered again each time. But for the second round this is (31,) in shape...
print("data(history) shape in forecast(): ")
print(data.shape)
data = data.reshape((data.shape[0]*data.shape[1], data.shape[2])) #...so then this reshape doesn't work.
print(data.shape)
# retrieve last observations for input data
# For multivariate, make sure to use all features.
input_x = data[-n_input:, :]
# reshape into [1, n_input, 1]
# We need to change the shape as well to take all features.
input_x = input_x.reshape((1, input_x.shape[0], input_x.shape[1]))
# forecast the next week
yhat = model.predict(input_x, verbose=0)
# we only want the vector forecast
yhat = yhat[0]
return yhat
如果你有一个列表(列表或数组)可以变成一个
(30, 72, 7)
数组。您唯一可以添加到该列表的是
(1, 72, 7)
形状数组,这将构成一个 (31, 72, 7) 数组。
任何其他内容都会导致 (31,) object dtype 数组(或错误)。如果 numpy
足够新,我还会收到“参差不齐的数组”警告。
因此在检查 shape
时还要检查 dtype
。