I want to do divide an 8*8 array in to 4 segments(each segment of 4*4 array) as shown below in step2. Then again divide each segment in to other small 4 subsegemnts(each subsegment of 2*2 array) and then find the mean of each subsegment and then find the stabbndard deviation of each segment using the 4 means of the 4 subsegments in it. So that finally I only have an array (2*2 array) ie with 1 standard deviation for 1 segment.
JavaScript
x
7
1
import numpy as np
2
from skimage.util.shape import view_as_blocks
3
4
arr=np.array([[1,2,3,4,5,6,7,8],[1,2,3,4,5,6,7,8],[1,2,3,4,5,6,7,8],[1,2,3,4,5,6,7,8],[1,2,3,4,5,6,7,8],[1,2,3,4,5,6,7,8],[1,2,3,4,5,6,7,8],[1,2,3,4,5,6,7,8]])
5
6
img= view_as_blocks(arr, block_shape=(4,4))
7
upto this I have tried but I was unable to go further in my requirement as I am completely new to python and numpy. Kindly, help me in achieve my requirement.
JavaScript
1
34
34
1
#step1-Array
2
array([[1, 2, 3, 4, 5, 6, 7, 8],
3
[1, 2, 3, 4, 5, 6, 7, 8],
4
[1, 2, 3, 4, 5, 6, 7, 8],
5
[1, 2, 3, 4, 5, 6, 7, 8],
6
[1, 2, 3, 4, 5, 6, 7, 8],
7
[1, 2, 3, 4, 5, 6, 7, 8],
8
[1, 2, 3, 4, 5, 6, 7, 8],
9
[1, 2, 3, 4, 5, 6, 7, 8]])
10
11
#step2-segments
12
array([[[[1, 2, 3, 4],
13
[1, 2, 3, 4],
14
[1, 2, 3, 4],
15
[1, 2, 3, 4]],
16
17
[[5, 6, 7, 8],
18
[5, 6, 7, 8],
19
[5, 6, 7, 8],
20
[5, 6, 7, 8]]],
21
22
23
[[[1, 2, 3, 4],
24
[1, 2, 3, 4],
25
[1, 2, 3, 4],
26
[1, 2, 3, 4]],
27
28
[[5, 6, 7, 8],
29
[5, 6, 7, 8],
30
[5, 6, 7, 8],
31
[5, 6, 7, 8]]]])
32
33
**more steps to go to get final output**
34
Expected Output
JavaScript
1
3
1
([[1.0, 1.0],
2
[1.0, 1.0]])
3
Advertisement
Answer
It can be done using a function view_as_blocks
of skimage.util.shape
.