user248237
user248237

Reputation:

reshaping ndarrays versus regular arrays in numpy?

I have an object of type 'numpy.ndarray', called "myarray", that when printed to the screen using python's "print", looks like hits

[[[ 84   0 213 232] [153   0 304 363]]
 [[ 33   0  56 104] [ 83   0  77 238]]
 [[ 0  0  9 61] [ 0  0  2 74]]]

"myarray" is made by another library. The value of myarray.shape equals (3, 2). I expected this to be a 3dimensional array, with three indices. When I try to make this structure myself, using:

second_array = array([[[84, 0, 213, 232], [153, 0, 304, 363]],
 [[33, 0, 56,  104], [83,  0, 77,  238]],
 [[0,  0, 9,   61],  [0,   0,  2, 74]]])

I get that second_array.shape is equal to (3, 2, 4), as expected. Why is there this difference? Also, given this, how can I reshape "myarray" so that the two columns are merged, i.e. so that the result is:

[[[ 84   0 213 232 153   0 304 363]]
 [[ 33   0  56 104  83   0  77 238]]
 [[ 0  0  9 61  0  0  2 74]]]

Edit: to clarify, I know that in the case of second_array, I can do second_array.reshape((3,8)). But how does this work for the ndarray which has the format of myarray but does not have a 3d index?

myarray.dtype is "object" but can be changed to be ndarray too.

Edit 2: Getting closer, but still cannot quite get the ravel/flatten followed by reshape. I have:

a = array([[1, 2, 3],
       [4, 5, 6]])
b = array([[ 7,  8,  9],
       [10, 11, 12]])
arr = array([a, b])

I try:

arr.ravel().reshape((2,6))

But this gives [[1, 2, 3, 4, 5, 6], ...] and I wanted [[1, 2, 3, 7, 8, 9], ...]. How can this be done?

thanks.

Upvotes: 2

Views: 1802

Answers (1)

unutbu
unutbu

Reputation: 879331

Indeed, ravel and hstack can be useful tools for reshaping arrays:

import numpy as np

myarray = np.empty((3,2),dtype = object)
myarray[:] = [[np.array([ 84,   0, 213, 232]), np.array([153, 0, 304, 363])],
 [np.array([ 33,   0,  56, 104]), np.array([ 83,   0,  77, 238])],
 [np.array([ 0, 0,  9, 61]), np.array([ 0,  0,  2, 74])]]

myarray = np.hstack(myarray.ravel()).reshape(3,2,4)
print(myarray)
# [[[ 84   0 213 232]
#   [153   0 304 363]]

#  [[ 33   0  56 104]
#   [ 83   0  77 238]]

#  [[  0   0   9  61]
#   [  0   0   2  74]]]

myarray = myarray.ravel().reshape(3,8)
print(myarray)
# [[ 84   0 213 232 153   0 304 363]
#  [ 33   0  56 104  83   0  77 238]
#  [  0   0   9  61   0   0   2  74]]

Regarding Edit 2:

import numpy as np

a = np.array([[1, 2, 3],
       [4, 5, 6]])
b = np.array([[ 7,  8,  9],
       [10, 11, 12]])
arr = np.array([a, b])
print(arr)
# [[[ 1  2  3]
#   [ 4  5  6]]

#  [[ 7  8  9]
#   [10 11 12]]]

Notice that

In [45]: arr[:,0,:]
Out[45]: 
array([[1, 2, 3],
       [7, 8, 9]])

Since you want the first row to be [1,2,3,7,8,9], the above shows that you want the second axis to be the first axis. This can be accomplished with the swapaxes method:

print(arr.swapaxes(0,1).reshape(2,6))
# [[ 1  2  3  7  8  9]
#  [ 4  5  6 10 11 12]]

Or, given a and b, or equivalently, arr[0] and arr[1], you could form arr directly with the hstack method:

arr = np.hstack([a, b])
# [[ 1  2  3  7  8  9]
#  [ 4  5  6 10 11 12]]

Upvotes: 3

Related Questions