当前位置:网站首页>Numpy learning notes (6) -- sum() function

Numpy learning notes (6) -- sum() function

2022-06-22 21:00:00 AC doesn't know the depth

One 、 Preface

Element in ( Array ) When you add up , You can use this function to accumulate , Very convenient

Two 、 Function explanation

1、sum() function

Let's look at all the parameters first

sum(a, axis, dtype, out, keepdims, initial, where)

2、 Parameters on

1)a

The first parameter is passed in / Input array elements

2)aixs( Optional )

Use along axis ( Optional , The default is to flatten the array into one-dimensional form , namely 0,1,2,3,4… etc. )

  1. If it's along 0 Axis , Then return each Column Index of maximum value
  2. If it's along 1 Axis , Then return each That's ok Index of maximum value
  3. If axis It's a negative number , Add from the last axis to the first axis
  4. If the default is None, Then add up all the input elements

aixs by 0 And 1 The direction of time is shown in the figure

 Insert picture description here

Last code example

import numpy as np


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

b = np.sum(a,axis=0)
#  Add by column 
## [4 6]

c = np.sum(a,axis=1)
#  Add by lines 
## [3 7]

d = np.sum(a)
#  All four elements are added by default 
## 10

3)dtype( Optional )

The default is :numpy.float64

We can also modify

For example, we often use dtype = numpy.int32

The specific integer type or precision should be selected according to the situation

For example, we can consider using all the age data int

Relevant shopping consumption data can be considered to use float

4)out( Optional )

An array type

This parameter is difficult to understand

Readers with good English can read the original development manual first , The hard-working students jump directly

 Insert picture description here
--------------------------------------- Split line ------------------------------------------------------------

I understand it :

It's called N Time sum function , Will create N Frequency group ( Size and input a identical ) Output the results

The disadvantages of this approach are obvious : Consume a lot of memory .

Then the developer defines out Parameters , Write all your calculations in out in

Second call sum function , The results will cover the previous round out value

I'm afraid the reader doesn't understand the code

import numpy as np

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

# out The shape of a The shape should be big 
ao = np.zeros_like([2,2])
print(ao)
## a0=[0 0]

b = np.sum(a,axis=0,out=ao)
print(ao)
## ao=[4 6]

c = np.sum(a,axis=1,out=ao)
print(ao)
## ao=[3 7]

5)keepdims( Optional )

Boolean type

In short

If set to True value , Then the size of the reduced shaft remains 1, Save in dimension ( The result is )

If set to False value , Will not keep this 1

Code directly for better understanding

import numpy as np
x = np.ones([2,8])

y = np.sum(x,axis=0,keepdims=True).shape
##  Keep the original two-dimensional shape (1, 8)

z = np.sum(x,axis=0,keepdims=False).shape
##  The original two-dimensional shape is not maintained (8,)

6)initial( Optional )

It's a scalar

This scalar will also be added to the cumulative result , It's equivalent to adding bias( bias )



z = np.sum(3,axis=0,initial=4)
## 7(3+4)

z = np.sum([1,2],initial=4)
## 7

z = np.sum([[1,2],[3,4]],axis=1,initial=4)
## [(1+2+4),(3+4+4)]
## [7,11]


z = np.sum([[1,2],[3,4]],axis=0,initial=4)
## [(1+3+4),(2+4+4)]
## [8,10]


7)where( Optional )

Boolean type

If True value , Will proceed normally sum operation

If the False value , It won't go on sum operation , Thus, the value is assigned to 0

print(np.sum([[0, 1], [np.nan, 5]], where=True, axis=1))
## [ 1. nan]

print(np.sum([[0, 1], [np.nan, 5]], where=True, axis=0))
## [nan 6.]

print(np.sum([[0, 1], [np.nan, 5]], where=False, axis=1))
## [0. 0.]

print(np.sum([[0, 1], [np.nan, 5]], where=False, axis=0))
## [0. 0.]

print(np.sum([[0, 1], [1, 5]], where=False, axis=1))
## [0 0]

print(np.sum([[0, 1], [1, 5]], where=False, axis=0))
## [0 0]

print(np.sum([[0, 1], [1, 5]], where=[True,False], axis=0))
## [1 0]

print(np.sum([[0, 1], [1, 5]], where=[False,True], axis=0))
## [0 6]

print(np.sum([[0, 1], [1, 5]], where=[True,False], axis=1))
## [0 1]

print(np.sum([[0, 1], [1, 5]], where=[False,True], axis=1))
## [1 5]

3、 Return value

If you don't set axis Value , Will return a scalar

If axis=0/1, Returns the same as a An array of the same shape ( It depends on which axis to add )

Be careful

If the function is called more than once , I suggest adding out Parameters , This saves a lot of memory !!!!

原网站

版权声明
本文为[AC doesn't know the depth]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221930313406.html