Reshape an Array in Numpy

How to reshape an array with numpy like this:

You can just split and concatenate:

a = np.array([[0, 0, 1, 1, 2, 2, 3, 3],
[0, 0, 1, 1, 2, 2, 3, 3]])

cols = a.shape[1] // 2
np.concatenate((a[:,:cols], a[:,cols:]))

#[[0 0 1 1]
# [0 0 1 1]
# [2 2 3 3]
# [2 2 3 3]]

How to reshape numpy array from image on different monitor's resolution

Reshape just reconfigures the existing information so if you have 67500 data points (150*150*3) you can have that as one long array or you can nest it as (150,150,3) or (22500,3) and so on. However if you want to go to bigger resolutions you'd need more information than you've got available and so you need to make something up.

Like you could double the size of a pixel to cover 4 pixels instead of 1 or you can copy the array multiple times or you can pad it with 0s or other values. There are functions like numpy.resize but you should check beforehand what you actually want it to be like.

Also usually the other way around is easier, that is you start with a high definition image and then resize it to become smaller. In that case you lose information in rescaling and so you don't have to make something up.

Numpy array reshape customization python

numpy.reshape

Gives a new shape to an array without changing its data.

so this is not right to convert array with shape of (250,250,3) into array with shape of (250,250) as 1st does have 187500 cells and 2nd does have 62500 cells.

You probably should use slicing, consider following example

import numpy as np
arr = np.array([[[0,1],[2,3]],[[4,5],[6,7]]]) # has shape (2,2,2)
arr2 = arr[:,:,0] # get certain cross-section, check what will happend if you use 1 inplace of 0 and 2 inplace of 0
print("arr2")
print(arr2)
print("arr2.shape",arr2.shape)

output

arr2
[[0 2]
[4 6]]
arr2.shape (2, 2)

How do I reshape a NumPy multi dimensional array to another array with the same dimensions, but different shape?

The function you are looking for is np.moveaxis() which lets you move a source axis to its destination.

>>> arr = np.random.random((1,76,76,255))
>>>
>>> arr.shape
(1, 76, 76, 255)
>>> arr2 = np.moveaxis(arr, 3, 1)
>>> arr2.shape
(1, 255, 76, 76)
>>>

Please note that these axes are 0-indexed

How to reshape numpy arrays to turn rows into columns

Do the obvious reshape:

In [304]: xy.reshape(9,2)
Out[304]:
array([[0, 0],
[1, 0],
[2, 0],
[0, 1],
[1, 1],
[2, 1],
[0, 2],
[1, 2],
[2, 2]])

now just transpose:

In [305]: xy.reshape(9,2).transpose()
Out[305]:
array([[0, 1, 2, 0, 1, 2, 0, 1, 2],
[0, 0, 0, 1, 1, 1, 2, 2, 2]])

There are other ways of doing this, but this is most obvious and easy to understand.

Reshaping/Grouping a 3D numpy array in a performant way

Use reshape() to transform your dimensions in numpy.

arr = np.repeat([1,2,3],430080).reshape(72,4480,4)
print(arr.shape)
print(arr.reshape(72,-1).shape)
(72, 4480, 4)
(72, 17920)

Numpy array reshape element-wise

You task is not to reshape the array. You have to swap the last axis (the third dimension of your array) with the second.

import numpy as np

#input
arr = np.array([
[[1, 2, 3],[4, 5, 6]],
[[7, 8, 9],[10,11,12]],
[[13,14,15],[16,17,19]]
])

#output
np.moveaxis(arr, 2, 1)

#an alternative is
np.swapaxes(arr, 1, 2)


Related Topics



Leave a reply



Submit