Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.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 列向量和矩阵之间的差异_Python_Arrays_Numpy_Matrix_Vector - Fatal编程技术网

Python 列向量和矩阵之间的差异

Python 列向量和矩阵之间的差异,python,arrays,numpy,matrix,vector,Python,Arrays,Numpy,Matrix,Vector,大家早上好, 我有一个列向量 vec=np.array([15.1,7.9,4.5,12.8,10.5],ndmin = 2) 还有一个矩阵 mt = np.matrix([[1,35.5,1.23],[1,40.8,1.89],[1,30.2,1.55],[1,4.3,1.18], [1,10.7,1.68]]) 如果我试着减去向量和矩阵 像 结果在数学上是错误的: array([[ 14.1 , -20.4 , 13.87], [ 6.9 , -32.9

大家早上好, 我有一个列向量

vec=np.array([15.1,7.9,4.5,12.8,10.5],ndmin = 2)   
还有一个矩阵

mt = np.matrix([[1,35.5,1.23],[1,40.8,1.89],[1,30.2,1.55],[1,4.3,1.18], [1,10.7,1.68]])    
如果我试着减去向量和矩阵 像

结果在数学上是错误的:

array([[ 14.1 , -20.4 ,  13.87],
       [  6.9 , -32.9 ,   6.01],
       [  3.5 , -25.7 ,   2.95],
       [ 11.8 ,   8.5 ,  11.62],
       [  9.5 ,  -0.2 ,   8.82]])
所有mt列的值都将被减去,而不仅仅是第一列 我想得到这个结果

array([[ 14.1  , 35.5 ,  1.23],
       [ 6.9  , 40.8 ,  1.89],
       [ 3.5  , 30.2 ,  1.55],
       [ 11.8  ,  4.3 ,  1.18],
       [ 9.5  , 10.7 ,  1.68]])
我怎样才能解决这个问题? 感谢大家:)

切片并减去-

  • 属性是数组的转置
Ex.

import numpy as np

vec=np.array([15.1,7.9,4.5,12.8,10.5],ndmin = 2)
mt = np.matrix([[1,35.5,1.23],[1,40.8,1.89],[1,30.2,1.55],[1,4.3,1.18], [1,10.7,1.68]])

mt[..., 0] = vec.T - mt[..., 0]
#or
#mt.T[0] = np.subtract(vec, mt.T[0])

print(mt)
O/p:

[[14.1  35.5   1.23]
 [ 6.9  40.8   1.89]
 [ 3.5  30.2   1.55]
 [11.8   4.3   1.18]
 [ 9.5  10.7   1.68]]

您评论说
pandas
版本很简单。如果使用正确的起点,那么
numpy
也是如此

In [110]: vec=pd.Series([15.1,7.9,4.5,12.8,10.5]) 
     ...: SeG=pd.DataFrame({'const':[1,1,1,1,1], 'growth':[35.5, 40.8, 30.2, 4.3, 10.7], 'dim':[1.23, 1.89, 1
     ...: .55, 1.18, 1.68]})                                                                                 
In [111]: vec                                                                                                
Out[111]: 
0    15.1
1     7.9
2     4.5
3    12.8
4    10.5
dtype: float64
In [112]: SeG                                                                                                
Out[112]: 
   const  growth   dim
0      1    35.5  1.23
1      1    40.8  1.89
2      1    30.2  1.55
3      1     4.3  1.18
4      1    10.7  1.68
vec
是一个系列,它的
是一个1d数组。同一列的
SeG

In [113]: vec.values                                                                                         
Out[113]: array([15.1,  7.9,  4.5, 12.8, 10.5])
In [114]: SeG['const']                                                                                       
Out[114]: 
0    1
1    1
2    1
3    1
4    1
Name: const, dtype: int64
In [115]: SeG['const'].values                                                                                
Out[115]: array([1, 1, 1, 1, 1])
因此
a['const']=vec-a['const']
相当于从一个1d数组中减去另一个1d数组,并将结果放回正确的位置。这正是公认答案的作用:

mt[..., 0] = vec.T - mt[..., 0]
让我们从两个数组开始,一个是1d,另一个是2d(但不是
np.matrix
子类):

===

使用原始阵列:

In [103]: vec                                                                                                
Out[103]: array([[15.1,  7.9,  4.5, 12.8, 10.5]])  # (1,5) shape
In [104]: mt                                                                                                 
Out[104]: 
matrix([[ 1.  , 35.5 ,  1.23],
        [ 1.  , 40.8 ,  1.89],
        [ 1.  , 30.2 ,  1.55],
        [ 1.  ,  4.3 ,  1.18],
        [ 1.  , 10.7 ,  1.68]])     # (5,3) shape
In [105]: vec.T                                                                                              
Out[105]: 
array([[15.1],
       [ 7.9],
       [ 4.5],
       [12.8],
       [10.5]])           # (5,1) shape
In [106]: mt[:,0]                                                                                            
Out[106]: 
matrix([[1.],
        [1.],
        [1.],
        [1.],
        [1.]])            # (5,1) shape
如果
mt
作为
ndarray
而不是
matrix
mt[:,0]
将具有(5,)形状。这种区别很重要

In [107]: mt[:,0] = vec.T-mt[:,0]          # operation on (5,1) arrays
你的
减法(vec,mt)
应该给你一个错误,而不仅仅是一个不想要的结果
vec
is(1,5)形状,
mt
is(5,3)。这些不兼容:

In [122]: np.subtract(_103, _104)                                                                            
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-122-35bafa7d9625> in <module>
----> 1 np.subtract(_103, _104)

ValueError: operands could not be broadcast together with shapes (1,5) (5,3) 
[122]中的
:名词性减法(_103,_104)
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在里面
---->1 np.减法(_103,_104)
ValueError:操作数无法与形状(1,5)(5,3)一起广播

vec_mt=np.substract(vec,mt[0])
?感谢大家的回复。我补充说,对熊猫来说,这是很容易做到的。vec=pd.Series([15.1,7.9,4.5,12.8,10.5])SeG=pd.DataFrame({'const':[1,1,1,1,1,1,1],'growth':[35.5,40.8,30.2,4.3,10.7],'dim':[1.23,1.89,1.55,1.18,1.68])a['const']=vec-a['const']你确定
np subtract(vec,mt
ran吗?这给我带来了一个错误。谢谢你有用的回答。是的,我注意到ndarray而不是矩阵对形状很重要,因为当我尝试一些命令时,它引发了您发布的错误。
In [107]: mt[:,0] = vec.T-mt[:,0]          # operation on (5,1) arrays
In [122]: np.subtract(_103, _104)                                                                            
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-122-35bafa7d9625> in <module>
----> 1 np.subtract(_103, _104)

ValueError: operands could not be broadcast together with shapes (1,5) (5,3)