【问题标题】:Too many positional arguments for method call with numpy使用 numpy 进行方法调用的位置参数过多
【发布时间】:2020-06-19 06:23:33
【问题描述】:

我收到 Too many positional arguments for method callx_train = np.array(x_train).reshape(-1, SIZE, SIZE, 1) 的错误。有关如何解决此问题的任何想法?

python3==3.8.3

pylint==2.5.3

astroid==2.5.3

numpy==1.18.5

【问题讨论】:

  • 不要传递太多位置参数?查看the documentation 以了解该方法期望的参数类型。
  • reshape() 只接受两个位置参数,但您传递了三个。你为什么要通过SIZE 两次?
  • 您可以尝试通过添加一对额外的括号将元组传递给reshapereshape((a, b, c, d))
  • 这似乎已经解决了这个问题。谢谢!

标签: python python-3.x numpy pylint


【解决方案1】:

【讨论】:

  • 很有趣,因为我遵循的教程将 4 个参数传递给了 reshape 方法。不过谢谢!
  • numpy 方法允许我经常使用np.arange(24).reshape(2,3,4) 的单独形状值。这里发生了其他事情。
【解决方案2】:

使用numpy 方法,允许任意数量的位置。就是需要元组的函数形式。

In [20]: np.arange(24).reshape(2,3,4)                                           
Out[20]: 
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])
In [21]: np.reshape(np.arange(24),(2,3,4))                                      
Out[21]: 
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])

或者更接近你的表达方式

In [25]: np.array(np.ones((3,4),int)).reshape(-1,2,2,1).shape                   
Out[25]: (3, 2, 2, 1)

这让我想知道您的 x_train 对象的性质。带有回溯的完整错误消息也可能会有所帮助。


这真的是 Python/numpy 错误,还是 pylint 或 astroid 警告?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-05
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 2013-09-23
    • 1970-01-01
    相关资源
    最近更新 更多