繁体   English   中英

下面的矩阵有什么区别?

[英]What is the difference between the following matrix?

我有一段如下所示的代码。 我必须实现 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]]

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

您的图像没有形状(长度、高度、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 个元素; 你不能通过重塑来增加元素的数量。

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)

如果您只想要一个向量数组,或者您想要一个行或列向量,则不同。 通常列向量“垂直向量”具有形状(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)

暂无
暂无

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

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