【问题标题】:Appending numpy array to class list changes the shape of class list将 numpy 数组附加到类列表会改变类列表的形状
【发布时间】:2021-10-19 17:25:29
【问题描述】:

我从我的训练集和测试集开始。它们是 NumPy 数组。

然后我为火车中的 x 创建一个变量 history = x。这是一个班级列表。

对于范围 len(test) 中的 i,我进行预测。在这个 forecast() 函数中,历史被制作成一个形状为 (30, 72, 7) 的数组,然后展平为 (2160, 7)。在循环中,在迭代进入下一步之前,测试被附加到历史记录中,如下所示:history.append(test[i, :])。

下一次迭代运行时,再次尝试运行 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

【问题讨论】:

    标签: python arrays numpy tensorflow time-series


    【解决方案1】:

    如果你有一个列表(列表或数组)可以变成一个

    (30, 72, 7)
    

    数组。您唯一可以添加到该列表中的是一个

    (1, 72, 7) 
    

    shape 数组,会生成一个 (31, 72, 7) 数组。

    任何其他情况都会导致 (31,) 对象 dtype 数组(或错误)。如果numpy 足够新,我也会期待“参差不齐的数组”警告。

    所以在检查shape 时也要检查dtype

    【讨论】:

    • 非常感谢您的简单直接回答!我唯一的问题是“如果 numpy 足够新”是什么意思?
    • 在我的数据拆分和重塑之后,测试看起来像 (30, 4 ,7) 的形状。我认为那是不对的。在重塑之前是 (148, 7)。关于如何解决这个问题的任何提示?
    猜你喜欢
    • 1970-01-01
    • 2021-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-15
    • 2017-10-19
    • 1970-01-01
    • 2013-09-26
    相关资源
    最近更新 更多