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). 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)
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; 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)
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.