Axis option specification summary of Python "numpy.sum (...)"

Introduction

A summary of the ** axis options ** that can be specified with numpy.ndarray.sum (...) or numpy.ndarray.mean (...). I will explain using figures for 2D and 3D numpy arrays.

For a two-dimensional numpy array

First, target a two-dimensional (ndim = 2) numpy array. As an example, consider the following ** 3 rows 4 columns ** ** 2D numpy array ** </ font> x.

m1.png

This array x can be generated with the following code.

shape=(3,4)Generate a numpy array of


import numpy as np
x = np.arange(1,12+1).reshape(3,4)
print(x)
#print(x.ndim)  # -> 2
#print(x.shape) # -> (3, 4)

Execution result


[ [ 1  2  3  4]
  [ 5  6  7  8]
  [ 9 10 11 12] ]

Specify ʻaxis = None`

Since ʻaxis = Noneis the default argument, bothx.sum ()andx.sum (axis = None)` have the same behavior. Reference for numpy.sum (...) / numpy.ndarray.sum (...)

sum(axis=None)


s = x.sum(axis=None)
# print(type(s)) # -> <class 'numpy.int64'>
# print(s.ndim)  # -> 0
# print(s.shape) # -> ()
print(s)

If ʻaxis = None` is specified, the sum of ** all the elements that make up the array ** will be calculated. Specifically, $ 1 + 2 + 3 + \ cdots + 11 + 12 = $ $ \ bf {78} $ </ font> is calculated.

Execution result


78

Specify ʻaxis = 0`

sum(axis=0)


s = x.sum(axis=0)
# print(type(s)) # -> <class 'numpy.ndarray'>
# print(s.ndim)  # -> 1
# print(s.shape) # -> (4,)
print(s)

Execution result


[15 18 21 24]

If you specify ʻaxis = 0, the elements are ** summed in the row direction **. Specifically, $ 1 + 5 + 9 = $ <font color ='red'> $ \ bf {15} $ </ font>, $ 2 + 6 + 10 = $ <font color ='red'> $ \ bf {18} $ </ font>, $ 3 + 7 + 11 = $ <font color ='red'> $ \ bf {21} $ </ font>, $ 4 + 8 + 12 = $ <font color ='red' > $ \ Bf {24} $ </ font> is calculated to be [15 18 21 24]`.

ma0.png ma0s.png

** [Caution]: The elements are summed in the row direction (the direction in which the row becomes 0, 1, 2, 3, ...), not the sum of the elements in each row. Please note that ** </ font> (If you misunderstand here, you will fall into ??? at once (experience story)).

Specify ʻaxis = 1`

sum(axis=1)


s = x.sum(axis=1)
# print(type(s)) # -> <class 'numpy.ndarray'>
# print(s.ndim)  # -> 1
# print(s.shape) # -> (3,)
print(s)

Execution result


[10 26 42]

If ʻaxis = 1is specified, the elements are summed in the ** column direction (the direction in which the column becomes larger as 0, 1, 2, 3, ...) **. Specifically, $ 1 + 2 + 3 + 4 = $ <font color ='red'> $ \ bf {10} $ </ font>, $ 5 + 6 + 7 + 8 = $ <font color ='red' > $ \ bf {26} $ </ font>, $ 9 + 10 + 11 + 12 = $ <font color ='red'> $ \ bf {42} $ </ font> is calculated[10 26 42 ] `.

ma1.png ma1s.png

For 3D numpy arrays

Next, we will target a 3D numpy array. As an example, we'll work with the numpy array x with shape (3,4,2).

shape=(3,4,2)Generate a numpy array of


import numpy as np
x = np.arange(1,24+1).reshape(3,4,2)
print(x)
# print(x.ndim)  # -> 3
# print(x.shape) # -> (3, 4, 2)

Execution result (line break position etc. are formatted for readability)


[  [  [ 1  2]  [ 3  4] [ 5  6]  [ 7  8]  ]
   [  [ 9 10]  [11 12]  [13 14]  [15 16]  ] 
   [  [17 18]  [19 20]  [21 22]  [23 24]  ]  ]

If you draw separately with ʻaxis = 2`, it will be as follows.

m2.png

Specify ʻaxis = None`

sum(axis=None)


s = x.sum(axis=None)
# print(type(s)) # -> <class 'numpy.int64'>
# print(s.ndim)  # -> 0
# print(s.shape) # -> ()
print(s)

** Total for all elements **. Specifically, $ 1 + 2 + 3 + 4 + \ cdots + 22 + 23 + 24 = 300 $ is calculated.

Execution result


300

Specify ʻaxis = 0`

sum(axis=0)


s = x.sum(axis=0)
# print(type(s)) # -> <class 'numpy.ndarray'>
# print(s.ndim)  # -> 2
# print(s.shape) # -> (4, 2)
print(s)

Execution result


[ [27 30]
  [33 36]
  [39 42]
  [45 48] ]

By specifying ʻaxis = 0`, the elements are summed in the ** row direction **.

The first element [27 30] of the above execution result is $ 1 + 9 + 17 = $ $ \ bf {27} $ </ font>, $ 2 + 10 + 18 = $ It is the result calculated as $ \ bf {30} $ </ font>.

m2a0.png m2as.png

x, which was shape = (3,4,2), becomes shape = (4,2) withx.sum (axis = 0).

Specify ʻaxis = 1`

sum(axis=1)


s = x.sum(axis=1)
# print(type(s)) # -> <class 'numpy.ndarray'>
# print(s.ndim)  # -> 2
# print(s.shape) # -> (3, 2)
print(s)

Execution result


[ [16 20]
  [48 52]
  [80 84] ]

By specifying ʻaxis = 1, the elements are summed in the ** column direction **. The first element [16 20]` of the above execution result is $ 1 + 3 + 5 + 7 = $ $ \ bf {16} $ </ font>, $ 2 + 4 + 6 + It is the result calculated as 8 = $ $ \ bf {20} $ </ font>.

m2a1.png m2a1s.png

x, which was shape = (3,4,2), becomes shape = (3,2) withx.sum (axis = 1).

Specify ʻaxis = 2`

sum(axis=2)


s = x.sum(axis=2)
# print(type(s)) # -> <class 'numpy.ndarray'>
# print(s.ndim)  # -> 2
# print(s.shape) # -> (3, 4)
print(s)

Execution result


[ [ 3  7 11 15]
  [19 23 27 31]
  [35 39 43 47] ]

By specifying ʻaxis = 2, the elements are summed in the ** channel direction ** if it is an image. The first upper left element 3 of the above execution result is $ 1 + 2 = $ <font color ='red'> $ \ bf {3} $ </ font>, and the lower right element 47` is $ 23 + 24 = It is the result calculated as $ $ \ bf {47} $ </ font>.

m2a2.png m2a2s.png

x, which was shape = (3,4,2), becomes shape = (3,4) withx.sum (axis = 2).

Specify ʻaxis = (0,1) `

Starting with NumPy 1.7, you can specify the axis as a tuple.

By specifying ʻaxis = (0,1), ** totals for rows and columns ** are calculated. The result is the same even if ʻaxis = (1,0) (the order does not matter).

sum(axis=(0,1))


s = x.sum(axis=(0,1))
#print(type(s)) # -> <class 'numpy.ndarray'>
#print(s.ndim)  # -> 1
#print(s.shape) # -> (2,)
print(s)

Execution result


[144 156]

The first element 144 of the execution result is the result of calculating the sum of all the elements ofx [:,:, 0], that is, $ 1 + 3 + 5 + 7 + \ cdots + 19 + 21 + 23 $. Become.

m2a01.png m2a01s.png

x, which was shape = (3,4,2), becomes shape = (2,) withx.sum (axis = (0,1)).

Specify ʻaxis = (1,2) `

The same is true for ʻaxis = (2,1)`.

sum(axis=(1,2))


s = x.sum(axis=(1,2))
# print(type(s)) # -> <class 'numpy.ndarray'>
# print(s.ndim)  # -> 1
# print(s.shape) # -> (3,)
print(s)

Execution result


[ 36 100 164]

The first element 36 of the execution result is the sum of the elements in the blue frame in the figure below **, that is, $ (1 + 3 + 5 + 7) + (2 + 4 + 6 + 8) $ is calculated. The result is.

m2a12.png m2a12s.png

x, which was shape = (3,4,2), becomes shape = (3,) withx.sum (axis = (1,2)).

Specify ʻaxis = (0,2) `

The same is true for ʻaxis = (2,0)`.

sum(axis=(0,2))


s = x.sum(axis=(0,2))
# print(type(s)) # -> <class 'numpy.ndarray'>
# print(s.ndim)  # -> 1
# print(s.shape) # -> (4,)
print(s)

Execution result


[57 69 81 93]

The first element 57 of the execution result is the result of calculating the ** total of the elements in the blue frame ** in the figure below, that is, $ (1 + 9 + 17) + (2 + 10 + 18) $. ..

m2a02.png m2a02s.png

x, which was shape = (3,4,2), becomes shape = (4,) withx.sum (axis = (0,2)).

Specify ʻaxis = (0,1,2) `

sum (axis = (0,1,2)) is the same assum (axis = None)orsum ()and the sum of all elements is calculated.

sum(axis=(0,1,2))


s = x.sum(axis=(0,1,2))
#print(type(s)) # -> <class 'numpy.int64'>
#print(s.ndim)  # -> 0
#print(s.shape) # -> ()
print(s)

Execution result


300

x, which was shape = (3,4,2), becomes shape = (0) withx.sum (axis = (0,1,2)).

Recommended Posts