I have the following array:
import numpy as np
a = np.array([[2, 3, 5],
[4, 6, 7],
[1, 5, 7]])
I want to expand it to this array:
b = [[2 2 2 3 3 3 5 5 5]
[2 2 2 3 3 3 5 5 5]
[2 2 2 3 3 3 5 5 5]
[4 4 4 6 6 6 7 7 7]
[4 4 4 6 6 6 7 7 7]
[4 4 4 6 6 6 7 7 7]
[1 1 1 5 5 5 7 7 7]
[1 1 1 5 5 5 7 7 7]
[1 1 1 5 5 5 7 7 7]]
So I'm using the following command:
import scipy.ndimage
b = scipy.ndimage.interpolation.zoom(a, 3, order=0)
based on this question and answer here Resampling a numpy array representing an image.
However, what I'm getting is this:
b = [[2 2 3 3 3 3 5 5 5]
[2 2 3 3 3 3 5 5 5]
[4 4 6 6 6 6 7 7 7]
[4 4 6 6 6 6 7 7 7]
[4 4 6 6 6 6 7 7 7]
[4 4 6 6 6 6 7 7 7]
[1 1 5 5 5 5 7 7 7]
[1 1 5 5 5 5 7 7 7]
[1 1 5 5 5 5 7 7 7]]
I want the expansion to be exactly by 3, or whatever the zoom factor is, but currently it's different for each element of the array.
Is there a direct way to do this? Or shall I do it manually with some coding?
With the help of Numpy numpy. resize(), we can resize the size of an array. Array can be of any shape but to resize it we just need the size i.e (2, 2), (2, 3) and many more.
Changing size of numpy Array in PythonSize of a numpy array can be changed by using resize() function of the NumPy library. refcheck- It is a boolean that checks the reference count. It checks if the array buffer is referenced to any other object.
In general numpy arrays can have more than one dimension. One way to create such array is to start with a 1-dimensional array and use the numpy reshape() function that rearranges elements of that array into a new shape.
Maybe a little late, but for the sake of completness: Numpy Kron does the job perfectly
>>> import numpy as np
>>> a = np.array([[2,3,5], [4,6,7], [1,5,7]])
>>> np.kron(a, np.ones((3,3)))
array([[ 2., 2., 2., 3., 3., 3., 5., 5., 5.],
[ 2., 2., 2., 3., 3., 3., 5., 5., 5.],
[ 2., 2., 2., 3., 3., 3., 5., 5., 5.],
[ 4., 4., 4., 6., 6., 6., 7., 7., 7.],
[ 4., 4., 4., 6., 6., 6., 7., 7., 7.],
[ 4., 4., 4., 6., 6., 6., 7., 7., 7.],
[ 1., 1., 1., 5., 5., 5., 7., 7., 7.],
[ 1., 1., 1., 5., 5., 5., 7., 7., 7.],
[ 1., 1., 1., 5., 5., 5., 7., 7., 7.]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With