【问题标题】:What is the difference between the following matrix?以下矩阵有什么区别?
【发布时间】:2020-07-11 15:40:36
【问题描述】:

我有一段如下代码。我必须实现 image2vector() 输入形状(长度,高度,3)并返回形状向量(长度*高度*3)。它没有给我我期望的结果。实际上,我不明白我得到的结果和预期的结果之间的区别。

def image2vector(image):
    v = None
    v = image.reshape(1, 9, image.shape[0] * image.shape[1] * image.shape[2])
    return v

image = np.array([[[ 0.67826139,  0.29380381],
        [ 0.90714982,  0.52835647],
        [ 0.4215251 ,  0.45017551]],

       [[ 0.92814219,  0.96677647],
        [ 0.85304703,  0.52351845],
        [ 0.19981397,  0.27417313]],

       [[ 0.60659855,  0.00533165],
        [ 0.10820313,  0.49978937],
        [ 0.34144279,  0.94630077]]])

print ("image2vector(image) = " + str(image2vector(image)))

我得到了以下结果:

image2vector(image) = [[ 0.67826139  0.29380381  0.90714982  0.52835647  0.4215251   0.45017551
   0.92814219  0.96677647  0.85304703  0.52351845  0.19981397  0.27417313
   0.60659855  0.00533165  0.10820313  0.49978937  0.34144279  0.94630077]]

但我想得到以下一个:

[[ 0.67826139] [ 0.29380381] [ 0.90714982] [ 0.52835647] [ 0.4215251 ] [ 0.45017551] [ 0.92814219] [ 0.96677647] [ 0.85304703] [ 0.52351845] [ 0.19981397] [ 0.27417313] [ 0.60659855] [ 0.00533165] [ 0.10820313] [ 0.49978937] [ 0.34144279] [ 0.94630077]]

它们之间有什么区别?我如何从第一个矩阵中得到第二个矩阵?

【问题讨论】:

    标签: numpy math matrix


    【解决方案1】:

    如果你只想要一个向量数组,或者你想要一个行向量或列向量,这两者的区别。 通常列向量“垂直向量”具有形状(n,1),行向量“水平”具有形状(1,n)

    import numpy as np
    image = np.array([[[ 0.67826139,  0.29380381],
            [ 0.90714982,  0.52835647],
            [ 0.4215251 ,  0.45017551]],
    
           [[ 0.92814219,  0.96677647],
            [ 0.85304703,  0.52351845],
            [ 0.19981397,  0.27417313]],
    
           [[ 0.60659855,  0.00533165],
            [ 0.10820313,  0.49978937],
            [ 0.34144279,  0.94630077]]])
    reshapedImage = image.reshape(18,1)
    reshapedImage
    array([[0.67826139],
           [0.29380381],
           [0.90714982],
           [0.52835647],
           [0.4215251],
           [0.45017551],
           [0.92814219],
           [0.96677647],
           [0.85304703],
           [0.52351845],
           [0.19981397],
           [0.27417313],
           [0.60659855],
           [0.00533165],
           [0.10820313],
           [0.49978937],
           [0.34144279],
           [0.94630077]], dtype=object)
    

    【讨论】:

      【解决方案2】:

      您的图像没有形状(长度、高度、3)

      In [1]: image = np.array([[[ 0.67826139,  0.29380381], 
         ...:         [ 0.90714982,  0.52835647], 
         ...:         [ 0.4215251 ,  0.45017551]], 
         ...:  
         ...:        [[ 0.92814219,  0.96677647], 
         ...:         [ 0.85304703,  0.52351845], 
         ...:         [ 0.19981397,  0.27417313]], 
         ...:  
         ...:        [[ 0.60659855,  0.00533165], 
         ...:         [ 0.10820313,  0.49978937], 
         ...:         [ 0.34144279,  0.94630077]]])                                                  
      In [2]: image.shape                                                                            
      Out[2]: (3, 3, 2)
      

      你不能做你尝试的重塑:

      In [3]: image.reshape(1, 9, image.shape[0] * image.shape[1] * image.shape[2])                  
      ---------------------------------------------------------------------------
      ValueError                                Traceback (most recent call last)
      <ipython-input-3-aac5649a99ea> in <module>
      ----> 1 image.reshape(1, 9, image.shape[0] * image.shape[1] * image.shape[2])
      
      ValueError: cannot reshape array of size 18 into shape (1,9,18)
      

      它只有 18 个元素;你不能通过 reshape 来增加元素的数量。

      In [4]: image.reshape(1, image.shape[0] * image.shape[1] * image.shape[2])                     
      Out[4]: 
      array([[0.67826139, 0.29380381, 0.90714982, 0.52835647, 0.4215251 ,
              0.45017551, 0.92814219, 0.96677647, 0.85304703, 0.52351845,
              0.19981397, 0.27417313, 0.60659855, 0.00533165, 0.10820313,
              0.49978937, 0.34144279, 0.94630077]])
      In [5]: _.shape                                                                                
      Out[5]: (1, 18)
      

      显然想要的形状是:

      In [6]: image.reshape(image.shape[0] * image.shape[1] * image.shape[2],1)                      
      Out[6]: 
      array([[0.67826139],
             [0.29380381],
             [0.90714982],
             [0.52835647],
             ...
             [0.94630077]])
      
      In [7]: _.shape                                                                                
      Out[7]: (18, 1)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-04-20
        • 1970-01-01
        • 2013-04-11
        • 1970-01-01
        • 2016-08-15
        • 2011-11-28
        • 1970-01-01
        相关资源
        最近更新 更多