繁体   English   中英

列向量和矩阵之间的区别

[英]difference between a column vector and a matrix

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

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]])    

如果我尝试减去向量和矩阵

 np.subtract(vec,mt)

结果在数学上是错误的:

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]])

我该如何解决? 谢谢大家 :)

切片和减去-

  • T属性是数组的转置。

防爆。

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是一个Series,其values是一个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数组,然后将结果放回正确的位置。 这就是公认的答案:

mt[..., 0] = vec.T - mt[..., 0]

让我们以2个数组开始,一个是1d,另一个是2d(但不是np.matrix子类):

In [116]: x=np.array([15.1,7.9,4.5,12.8,10.5])                                                               
In [117]: y = np.array([[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]])

In [118]: y[:,0] = x - y[:,0]                                                                                
In [119]: y                                                                                                  
Out[119]: 
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]])

===

使用原始数组:

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

您的subtract(vec, mt)应该给您一个错误,而不仅仅是一个不希望的结果。 vec是(1,5)形状, mt是(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) 

你可以这样做:

import numpy as np
import pandas as pd
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]])
R=mt.copy().transpose() #R is transpose mt to calculate the difference
R[0]=np.subtract(vec,R[0]) #Only apply first row
R=R.transpose()           #Back correct shape(5x3)
R

日期:

matrix([[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]])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM