【问题标题】:change() missing 1 required positional argument: 'X' while predicting future valuechange() 缺少 1 个必需的位置参数:“X”,同时预测未来值
【发布时间】:2019-06-17 08:46:03
【问题描述】:

我试图用三个输入来预测未来的价值。这里我想根据每一个小时的三个输入来预测未来的价值。这里g=温度,p=湿度,c=风,我想根据这些输入预测下一小时的温度。这就是为什么我把 n_out 设置为 1 的原因,我在 def 类中编写了代码。之后,我尝试将该 def 类值添加为 x,y 值。因为我要把它写成训练和测试值。但是错误是这样的。我将使用 LSTM 预测未来价值。在此之后,我不知道如何将此代码作为训练和测试添加到 LSTM 模型中。谁能帮我解决这个问题? 在这里我粘贴我的代码和 csv 文件。

def change(train,X, n_out=1):
    data = train.reshape((train.shape[0]))
    x, y = list(), list()
    in_start = 0
    # step over the entire history one time step at a time
    for _ in range(len(data)):
        # define the end of the input sequence
        in_end = in_start + X
        out_end = in_end + n_out
        # ensure we have enough data for this instance
        if out_end < len(data):
            x_input = data[in_start:in_end, 0]
            x_input = x_input.reshape((len(x_input), 3))
            x.append(x_input)
            y.append(data[in_end:out_end, 0])
        # move along one time step
        in_start += 1
    return array(x), array(y)


data= pd.DataFrame(data,columns=['g','p','c']) 
data.columns = ['g', 'p', 'c',]
pd.options.display.float_format = '{:,.0f}'.format
data = data.dropna ()
cols=['g', 'p', 'c']
X=data[cols]
x,y = change(data)

错误来了

我的 csv 文件:

在编辑代码后它给了我这个错误:

【问题讨论】:

  • 请将文本作为文本发布,而不是图片。并使您的代码与错误消息匹配。无论如何,您对'DataFrame' object has no attribute 'reshape' 到底有什么不明白的地方?
  • 此外,您想通过train.reshape((train.shape[0])) 实现什么目标?这对我来说没有任何意义。
  • @Goyo LSTM() 层必须指定输入的形状。 ...拟合模型和进行预测时的数据数组,...然后我们可以使用 reshape() 函数。这就是我试图重塑数据值的原因。
  • 我看不出这如何回答我问你的任何问题。你明白我的问题吗?顺便说一句,第一个可能更重要。
  • @Goyo 对于第一个问题 Panda.dataframe 是否已构建重塑?如果是我应该用值更改我的代码吗?

标签: python python-3.x time lstm


【解决方案1】:

在函数的定义中,您有 3 个参数: 训练,X 和 n_out=1

def change(train, X, n_out=1)

当你调用你的函数时,你只提供了 1 个参数(data)

x,y = change(data)

你如何定义n_out为1,你还需要提供x,或者定义你的函数为:

def change(train, n_out=1)

注意:

你需要在调用你的函数时提供 X 例如:

x,y = change(data, 1) 

或定义类似的函数:

def change(train, X=1, n_out=1)

【讨论】:

  • 我想根据这三个输入预测一个值。好的,如果我删除这个 x,y=change(data) ,那么我如何将它添加到训练模型中以使用 LSTM 预测值。你对此有什么想法吗?如果我删除了,那么我如何在代码中编写输入数据。
  • 您需要在调用函数时提供 X,例如:x,y = change(data, 1),或定义它,例如:def change(train, X=1, n_out =1)
  • 是的,我试过这个方法,然后它给了我很长的错误。尝试您的方法后,错误显示(重塑)
  • 你能在这里提供你的数据吗:pd.DataFrame(data,columns=['g','p','c'])
  • g= 温度 ,p=湿度 ,c = 风 根据这些输入我想预测下一小时的温度
猜你喜欢
  • 2021-09-11
  • 2019-06-13
  • 1970-01-01
  • 1970-01-01
  • 2014-09-13
  • 2021-04-16
  • 1970-01-01
  • 2021-04-10
相关资源
最近更新 更多