简体   繁体   English

下面的矩阵有什么区别?

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

I have a piece of code like the following.我有一段如下所示的代码。 I have to implement image2vector() that takes an input of shape (length, height, 3) and returns a vector of shape (length*height*3).我必须实现 image2vector(),它接受形状(长度、高度、3)的输入并返回形状(长度*高度*3)的向量。 It doesn't give me a result of what I expect.它没有给我我期望的结果。 Actually, I don't understand the difference between the result which I got and the expected one.实际上,我不明白我得到的结果和预期的结果之间的区别。

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

I got te following result:我得到了以下结果:

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

But I want to get the following one:但我想得到以下一个:

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

What is the difference between them?它们之间有什么区别? How I get the second matrix from the first one?我如何从第一个矩阵获得第二个矩阵?

Your image does not have the shape (length, height, 3)您的图像没有形状(长度、高度、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)

and you can't do the reshape you try:你不能做你尝试的重塑:

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)

It has only 18 elements;它只有 18 个元素; you can't increase the number of elements with 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)

The apparently desired shape is:显然需要的形状是:

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)

The difference if you want just a vector array, or you want a row or column vector.如果您只想要一个向量数组,或者您想要一个行或列向量,则不同。 usually column vector "vertical vector" has the shape(n,1) and row vector "horizontal" has the shape (1,n)通常列向量“垂直向量”具有形状(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.

相关问题 @和*与python矩阵乘法有什么区别? - What's the difference between @ and * with python matrix multiplication? numpy 中的两个以下重塑 function 有什么区别? - what is the difference between two following reshape function in numpy? 矩阵乘法和循环遍历之间的根本区别是什么? - What is the underlying difference between multiplying a matrix and looping through? 使用 Numpy 对矩阵进行元素操作的运算符与在矩阵上执行的操作有什么区别? - What is the difference between the operator acting elementwise vs on the matrix using Numpy? 使用矩阵乘法与np.matrix数组和dot()/ tensor()与np.arrays有什么区别? - What is the difference between using matrix multiplication with np.matrix arrays, and dot()/tensor() with np.arrays? 用于矩阵-矩阵乘法的函数 numpy.dot()、@ 和方法 .dot() 有什么区别? - What is difference between the function numpy.dot(), @, and method .dot() for matrix-matrix multiplication? 列向量和矩阵之间的区别 - difference between a column vector and a matrix 通过 numpy 矩阵进行索引时,mat[:3,1:2] 和 mat[:3,1] 有什么区别? - What is the difference between mat[:3,1:2] and mat[:3,1] when indexing through a numpy matrix? a[:,:-1] 和 a[:,-1] 有什么区别? - What is the difference between a[:,:-1] and a[:,-1]? [:][:] 和 [:,:] 之间的 NumPy 有什么区别? - What is the difference in NumPy between [:][:] and [:,:]?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM