Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ValueError:无法将输入数组从形状(1,3)广播到形状(3,1)_Python_Numpy_Matrix_Linear Regression - Fatal编程技术网

Python ValueError:无法将输入数组从形状(1,3)广播到形状(3,1)

Python ValueError:无法将输入数组从形状(1,3)广播到形状(3,1),python,numpy,matrix,linear-regression,Python,Numpy,Matrix,Linear Regression,我正在处理一个线性回归编码问题,我在尝试编码特征矩阵部分时遇到了这个错误。你能帮我纠正一下吗 回溯(最近一次呼叫最后一次): 文件“C:\Users\visha\AppData\Local\Continuum\anaconda3\lib\site packages\nose\case.py”,第197行, 运行中 自检(*self.arg) 文件“C:\Users\visha\machinelearning\test.py”,第22行,在test\u compute\u Phi中 φ=计算φ(x

我正在处理一个线性回归编码问题,我在尝试编码特征矩阵部分时遇到了这个错误。你能帮我纠正一下吗

回溯(最近一次呼叫最后一次): 文件“C:\Users\visha\AppData\Local\Continuum\anaconda3\lib\site packages\nose\case.py”,第197行, 运行中 自检(*self.arg) 文件“C:\Users\visha\machinelearning\test.py”,第22行,在test\u compute\u Phi中 φ=计算φ(x,2) 文件“C:\Users\visha\machinelearning\linear\u regression.py”,第30行,在compute\u Phi中 φ[:,i]=np.幂(x,i).重塑(x.形[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 

您的
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)
现在我们可以分配列了

现在(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的尺寸被缩放以匹配其他尺寸。

您的
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)
现在我们可以分配列了

现在(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的维度被缩放以匹配其他维度。

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

def计算φ(x,p):
x=np.asmatrix(x)
φ=np.零(形状=(x.形状[0],p))
φ=np.asmatrix(φ)
对于范围(0,p)内的i:
打印(Phi[:,i])
打印(np.功率(x,i))
φ[:,i]=np.幂(x,i)
返回φ
计算φ(np.mat('1,2,3'),2)
输出

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

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

def计算φ(x,p):
x=np.asmatrix(x)
φ=np.零(形状=(x.形状[0],p))
φ=np.asmatrix(φ)
对于范围(0,p)内的i:
打印(Phi[:,i])
打印(np.功率(x,i))
φ[:,i]=np.幂(x,i)
返回φ
计算φ(np.mat('1,2,3'),2)
输出

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

使用
np.asarray(x)
。避免
asmatrix
你为x和pHi传递了什么值@Dev,在测试用例中x是np.mat('1..2..3'),p是2。嗨@hpaulj,是的,我更改了它,但仍然给我相同的错误。你创建了
pHi
作为2d
ndarray
。为什么要在循环中通过
np.matrix
<代码>np。矩阵在索引时表现不同。这会产生混乱,这也是为什么不鼓励在新代码中使用它们的部分原因。请使用
np.asarray(x)
。避免
asmatrix
你为x和pHi传递了什么值@Dev,在测试用例中x是np.mat('1..2..3'),p是2。嗨@hpaulj,是的,我更改了它,但仍然给我相同的错误。你创建了
pHi
作为2d
ndarray
。为什么要在循环中通过
np.matrix
<代码>np。矩阵在索引时表现不同。这会产生混乱,这也是为什么不鼓励在新代码中使用它们的部分原因。您好@Dev,您给出的上述代码现在给了我一个类型错误,而不是广播错误。它表示,断言类型(Phi)=np.matrixlib.defmatrix。matrix@VishwaV在哪一行,代码为我工作,用python3@VishwaV如果您的函数调用方需要矩阵类型,请将return语句更改为“return np.asmatrix(Phi)”即可,但不应执行此操作,因为在赋值中不应更改给定的return语句。你能告诉我实现上述想法的另一种方法吗?我在上面编辑了答案,请检查这是否适用于yourHi@Dev,你给我的上述代码现在给出的是一个类型错误,而不是广播错误。它表示,断言类型(Phi)=np.matrixlib.defmatrix。matrix@VishwaV在哪一行,代码为我工作,用python3@VishwaV如果您的函数调用方需要矩阵类型,请将return语句更改为“return np.asmatrix(Phi)”即可,但不应执行此操作,因为在赋值中不应更改给定的return语句。你能告诉我实现你上述想法的另一种方法吗?我在上面编辑了答案,请检查这是否适用于你的想法