I have the following array:
JavaScript
x
5
1
import numpy as np
2
a = np.array([[2, 3, 5],
3
[4, 6, 7],
4
[1, 5, 7]])
5
I want to expand it to this array:
JavaScript
1
10
10
1
b = [[2 2 2 3 3 3 5 5 5]
2
[2 2 2 3 3 3 5 5 5]
3
[2 2 2 3 3 3 5 5 5]
4
[4 4 4 6 6 6 7 7 7]
5
[4 4 4 6 6 6 7 7 7]
6
[4 4 4 6 6 6 7 7 7]
7
[1 1 1 5 5 5 7 7 7]
8
[1 1 1 5 5 5 7 7 7]
9
[1 1 1 5 5 5 7 7 7]]
10
So I’m using the following command:
JavaScript
1
3
1
import scipy.ndimage
2
b = scipy.ndimage.interpolation.zoom(a, 3, order=0)
3
based on this question and answer here Resampling a numpy array representing an image.
However, what I’m getting is this:
JavaScript
1
10
10
1
b = [[2 2 3 3 3 3 5 5 5]
2
[2 2 3 3 3 3 5 5 5]
3
[4 4 6 6 6 6 7 7 7]
4
[4 4 6 6 6 6 7 7 7]
5
[4 4 6 6 6 6 7 7 7]
6
[4 4 6 6 6 6 7 7 7]
7
[1 1 5 5 5 5 7 7 7]
8
[1 1 5 5 5 5 7 7 7]
9
[1 1 5 5 5 5 7 7 7]]
10
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?
Advertisement
Answer
Maybe a little late, but for the sake of completness: Numpy Kron does the job perfectly
JavaScript
1
13
13
1
>>> import numpy as np
2
>>> a = np.array([[2,3,5], [4,6,7], [1,5,7]])
3
>>> np.kron(a, np.ones((3,3)))
4
array([[ 2., 2., 2., 3., 3., 3., 5., 5., 5.],
5
[ 2., 2., 2., 3., 3., 3., 5., 5., 5.],
6
[ 2., 2., 2., 3., 3., 3., 5., 5., 5.],
7
[ 4., 4., 4., 6., 6., 6., 7., 7., 7.],
8
[ 4., 4., 4., 6., 6., 6., 7., 7., 7.],
9
[ 4., 4., 4., 6., 6., 6., 7., 7., 7.],
10
[ 1., 1., 1., 5., 5., 5., 7., 7., 7.],
11
[ 1., 1., 1., 5., 5., 5., 7., 7., 7.],
12
[ 1., 1., 1., 5., 5., 5., 7., 7., 7.]])
13