【问题标题】:ValueError: could not broadcast input array from shape (1,3) into shape (3,1)ValueError:无法将输入数组从形状(1,3)广播到形状(3,1)
【发布时间】:2020-01-21 01:49:35
【问题描述】:

我正在研究线性回归编码问题,但在尝试对特征矩阵部分进行编码时出现此错误。你能帮我纠正一下吗?

Traceback(最近一次调用最后一次): 文件“C:\Users\visha\AppData\Local\Continuum\anaconda3\lib\site-packages\nose\case.py”,第 197 行, 在运行测试中 self.test(*self.arg) 文件“C:\Users\visha\machinelearning\test.py”,第 22 行,在 test_compute_Phi Phi = 计算_Phi(x,2) 文件“C:\Users\visha\machinelearning\linear_regression.py”,第 30 行,在 compute_Phi Phi[:,i] = np.power(x,i).reshape(x.shape[0],) ValueError: 无法将输入数组从形状 (1,3) 广播到形状 (3,1)

[代码]

def compute_Phi(x,p):
    x = np.asmatrix(x)
    Phi = np.zeros(shape = (x.shape[0],p))
    for i in range(0,p):
        Phi[:,i] = np.power(x,i).reshape(x.shape[0],)
        Phi = np.asmatrix(Phi)
return Phi 

【问题讨论】:

  • 使用np.asarray(x)。避免asmatrix
  • 你为 x 和 p 传递了什么值
  • 嗨@Dev,在测试用例中,x 是 np.mat('1.;2.;3') 并且 p 是 2。
  • 嗨@hpaulj,是的,我改变了它,但仍然给我同样的错误。
  • 您将Phi 创建为二维ndarray。为什么在循环中通过np.matrix 传递它? np.matrix 在被索引时表现不同。这会产生混乱,这也是不鼓励在新代码中使用它们的部分原因。

标签: python numpy matrix linear-regression


【解决方案1】:

你的x,没有np.mat

In [225]: x = np.array([1,2,3])[:,None]                                                
In [226]: x                                                                            
Out[226]: 
array([[1],
       [2],
       [3]])
In [227]: p = 2                                                                        
In [228]: Phi = np.zeros((3,2))                                                        
In [229]: Phi[:,0] = np.power(x,0)                                                     
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-229-f8ff29de133c> in <module>
----> 1 Phi[:,0] = np.power(x,0)

ValueError: could not broadcast input array from shape (3,1) into shape (3)

为什么? x 具有形状 (3,1)(定义)。但是Phi[:,0] 的形状为 (3,)。通过广播规则, (3,1) 不能放在 (3,) 空间中。 A (1,3) 可以。 A (3,) 可以。

让我们制作x (3,):

In [230]: x = np.array([1,2,3])                                                        
In [231]: Phi[:,0] = np.power(x,0)                                                     
In [232]: Phi[:,1] = np.power(x,1)                                                     
In [233]: Phi                                                                          
Out[233]: 
array([[1., 1.],
       [1., 2.],
       [1., 3.]])

现在我们可以分配列了。

现在 (3,1) 形状 x 可以同时使用多个幂:

In [234]: np.power(x[:,None],[0,1,2,3])                                                
Out[234]: 
array([[ 1,  1,  1,  1],
       [ 1,  2,  4,  8],
       [ 1,  3,  9, 27]])

这里 (3,1) x 使用 (4,) p 广播以产生 (3,4) 结果。

广播步骤为:(3,1), (4,) => (3,1), (1,4) => (3,4), (3,4)

关键是 - 尺寸 1 尺寸可以自动添加到前导位置。并且尺寸 1 尺寸被缩放以匹配其他尺寸。

【讨论】:

    【解决方案2】:

    实际上,错误是因为在 for 循环的第一次迭代中,您 Phi 是 np.array,而在第二次迭代中它被更改为矩阵。如果您将行 Phi = np.asmatrix(Phi) 移到循环之外,它就可以工作

    def compute_Phi(x,p):
        x = np.asmatrix(x)
        Phi = np.zeros(shape = (x.shape[0],p))
        Phi = np.asmatrix(Phi)
        for i in range(0,p):
            print(Phi[:,i])
            print(np.power(x,i))
            Phi[:,i] = np.power(x,i)
    
        return Phi 
    
    compute_Phi(np.mat('1.;2.;3'), 2)
    

    输出

    matrix([[1., 1.],
            [1., 2.],
            [1., 3.]])
    

    【讨论】:

    • 嗨@Dev,你给的上面代码现在给了我一个类型错误而不是广播错误。它说,断言类型(Phi)==np.matrixlib.defmatrix.matrix
    • @VishwaV 在哪一行,代码对我有用,在 python 3 中
    • @VishwaV 可能是您期望矩阵类型的函数调用者,将返回语句更改为“return np.asmatrix(Phi)”
    • 这行得通,但不应该这样做,因为不应在分配中更改给定的 return 语句。你能告诉我实现上述想法的另一种方法吗?
    • 我编辑了上面的答案,请检查它是否适合你
    猜你喜欢
    • 2018-06-30
    • 2018-06-08
    • 2020-10-18
    • 2018-09-25
    • 2018-04-21
    • 2020-10-09
    • 2021-03-30
    • 2021-08-24
    • 2019-11-28
    相关资源
    最近更新 更多