I have the following problem, I have an array that contains 60 arrays, inside them there are 21 arrays, and inside these last ones there are 3 integers, when I do the shape, it says (60,) How can I make the reshape to be (60,21,3)?
CodePudding user response:
I am not entirely sure what you are asking, you didn't leave a whole lot of details. However, here are some basic numpy
functions that seem like they would answer your question.
arr = np.random.rand(2,2,1)
print(arr)
#obviously you can use your dimensions of (60,21,3)
#however I am going to print the array
output:
array([[[0.76494962],
[0.20654063]],
[[0.22742839],
[0.15418584]]])
If you want to print the exact shape, in this case of (2,2,1) print(arr.shape)
gives the output (2, 2, 1)
. This is a tuple object stored within the numpy array, therefore will not change unless you re-define the array with a new numpy array. If numpy does not output (60,21,3)
in your case, there is a very good chance you set your code up incorrectly.
If I want to resize my array it is important that you adhere to the size of your array, in my case if I use the function np.size(arr)
I get 4
. This is because 2*2*1 = 4
so I must reshape it to a size of something that adheres to the same size. for example these are valid calls:
arr = arr.reshape(4)
arr = arr.reshape(1,1,4)
arr = arr.reshape(1,1,2,2)
And so on. As long as if you multiply the numbers they come out to np.size(arr)
. In your case you have the shape (60,21,3)
therefore you must reshape it to have the size 60*21*3 = 3780
.
I also left an example of the resize
function below. Again, you did not leave much in terms of details on what you wanted, so I am assuming it is one of the functions or explanations I have outlined in this post.
>>> import numpy as np
>>> arr = np.random.rand(60,21,3)
>>> arr.shape
(60, 21, 3)
>>> arr = arr.reshape(3780)
>>> arr.shape
(3780,)
>>> arr = np.random.rand(60)
>>> arr.shape
(60,)
>>> arr = np.resize(arr, (60,21,3))
>>> arr.shape
(60, 21, 3)
CodePudding user response:
You probably created a array of lists (objects), for example:
b = np.array([0], dtype=np.dtype(object))
b[0] = [4,5,6]
b.shape
Gives b
as:
array([list([4, 5, 6])], dtype=object)
and the shape will be (1,), not 'seeing' the 2nd dimension represented by the list
Check of this is the case by running b.dtype