ajayramesh
ajayramesh

Reputation: 3774

how to access entire sub array in numpy?

I am trying to store different images results in a multi-dimensional array. Consider below m has 3 images of size 3x4, now I want to access the second image, I am trying m[:, :, 1]. But it produced wrong results. Can you please correct me on how to access sub array.

m = np.random.random((3,4,3))
array([[[ 0.72474649,  0.88239477,  0.21834498],
        [ 0.3594392 ,  0.06462519,  0.43329582],
        [ 0.83452734,  0.43937307,  0.61267164],
        [ 0.75062416,  0.76516036,  0.35487906]],

       [[ 0.90644054,  0.22091564,  0.94508058],
        [ 0.20201594,  0.60295539,  0.77998197],
        [ 0.10496835,  0.77960017,  0.78249163],
        [ 0.53903028,  0.81245971,  0.38983454]],

       [[ 0.81369716,  0.89243267,  0.49874087],
        [ 0.95248644,  0.0847973 ,  0.59074351],
        [ 0.24507041,  0.03595347,  0.32902477],
        [ 0.48906304,  0.14659161,  0.77392082]]])


m[:, :, 1]
// Actual output
array([[ 0.88239477,  0.06462519,  0.43937307,  0.76516036],
       [ 0.22091564,  0.60295539,  0.77960017,  0.81245971],
       [ 0.89243267,  0.0847973 ,  0.03595347,  0.14659161]])

// Expected  output
[ 0.90644054,  0.22091564,  0.94508058],
[ 0.20201594,  0.60295539,  0.77998197],
[ 0.10496835,  0.77960017,  0.78249163],
[ 0.53903028,  0.81245971,  0.38983454]

Upvotes: 1

Views: 338

Answers (4)

NaN
NaN

Reputation: 2312

This is simply a note about assumptions on what is the correct 'slice' based on work in other fields. You need to know how your 'image' was constructed. In your case you have 3 images, but in some fields (GIS and Remote Sensing) 3 'images' are used to construct an array from 3 color bands. Sadly, there are preferred orders of the elements, but it is not standard.

For example, consider the consequences of how an array is constructed on what is the 'correct' slice to take.

The three bands all have a shape of 5, 4

red = np.arange(20).reshape(5,4)
green = np.arange(20, 40).reshape(5,4)
blue = np.arange(40, 60).reshape(5,4)

red
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [16, 17, 18, 19]])

green 
array([[20, 21, 22, 23],
       [24, 25, 26, 27],
       [28, 29, 30, 31],
       [32, 33, 34, 35],
       [36, 37, 38, 39]])

blue
array([[40, 41, 42, 43],
       [44, 45, 46, 47],
       [48, 49, 50, 51],
       [52, 53, 54, 55],
       [56, 57, 58, 59]])

Now these can be assembled in 3 ways, and this sometimes depends on the software that you are using.... so you have to know how the array is constructed.

rgb_0 = np.stack((red, green, blue), axis=0)
rgb_1 = np.stack((red, green, blue), axis=1)
rgb_2 = np.stack((red, green, blue), axis=2)

The final arrays have an obviously different shape, but... remember... we don't know how they were constructed.

rgb_0.shape, rgb_1.shape, rgb_2.shape
((3, 5, 4), (5, 3, 4), (5, 4, 3))

Let's pull out the 'red' information for the 3 variants of rgb

red_0 = rgb_0[0, :, :]
red_1 = rgb_1[:, 0, :]
red_2 = rgb_2[:, :, 0]

Yielding

np.allclose(red_0, red_1)  # True
np.allclose(red_0, red_2)  # True
np.allclose(red_0, red_2)  # True

red_0 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [16, 17, 18, 19]])

So making assumptions about what is the correct 'slice' depends on your knowledge of how the array was constructed.

Upvotes: 1

ajayramesh
ajayramesh

Reputation: 3774

I tried m[1, :, :] or m[1] and both work.

Upvotes: 0

Joe Iddon
Joe Iddon

Reputation: 20414

The neat things about arrays is that you can forget about what they are storing and just narrow it down to the dimension that matters - the first.

So you can reduce your problem to:

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

and you want to extract the second element so the 2. And now we have stated the problem this way, I am sure you can see that to get the 2, we just do:

a[1]

Simple as that!

Upvotes: 0

user8502296
user8502296

Reputation:

You can access the individual image with it's expected position i.e. m[0] gives you the first image.

Proof:

>>> a = numpy.random.random((3, 4, 3))
>>> a
array([[[ 0.2585421 ,  0.6096753 ,  0.70295892],
        [ 0.50408344,  0.37075371,  0.30463057],
        [ 0.76298221,  0.67466292,  0.53305787],
        [ 0.63844013,  0.45100157,  0.1346955 ]],

       [[ 0.54268873,  0.31534909,  0.40414511],
        [ 0.87335605,  0.81278098,  0.12953214],
        [ 0.64353518,  0.22347   ,  0.63712407],
        [ 0.02646421,  0.56478202,  0.57160074]],

       [[ 0.36965073,  0.796066  ,  0.7289024 ],
        [ 0.47232785,  0.43087964,  0.873769  ],
        [ 0.12393581,  0.63266617,  0.0935309 ],
        [ 0.62007608,  0.77474674,  0.28507152]]])
>>> a[0]
array([[ 0.2585421 ,  0.6096753 ,  0.70295892],
       [ 0.50408344,  0.37075371,  0.30463057],
       [ 0.76298221,  0.67466292,  0.53305787],
       [ 0.63844013,  0.45100157,  0.1346955 ]])

Upvotes: 1

Related Questions