当前位置:网站首页>Numpy learning notes (6) -- sum() function
Numpy learning notes (6) -- sum() function
2022-06-22 21:00:00 【AC doesn't know the depth】
Catalog
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. )
- If it's along 0 Axis , Then return each Column Index of maximum value
- If it's along 1 Axis , Then return each That's ok Index of maximum value
- If axis It's a negative number , Add from the last axis to the first axis
- 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

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

--------------------------------------- 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 !!!!
边栏推荐
- [resolved] -go_ out: protoc-gen-go: Plugin failed with status code 1.
- R language universalbank CSV "data analysis
- Is the brokerage account of qiniu delivery safe? Is the brokerage account provided by qiniu true?
- 智能計算之神經網絡(BP)介紹
- 71-对2010年阿里一道Oracle DBA面试题目的分析
- 密码学系列之:PKI的证书格式表示X.509
- 怎样实现网页端im即时通讯中的@人功能
- [proteus simulation] 74LS138 decoder water lamp
- 让知识付费系统视频支持M3U8格式播放的方法
- 性能测试(一)
猜你喜欢
随机推荐
智能计算之神经网络(BP)介绍
直播预报|中国信息协会网信大讲堂第六期将于6月24日开播啦
Golang学习笔记—结构体
The road to systematic construction of geek planet business monitoring and alarm system
Oracle system/用户被锁定的解决方法
uniapp小程序商城开发thinkphp6积分商城、团购、秒杀 封装APP
华为云发布拉美互联网战略
让知识付费系统视频支持M3U8格式播放的方法
Overview of common loss functions for in-depth learning: basic forms, principles and characteristics
Is the brokerage account of qiniu delivery safe? Is the brokerage account provided by qiniu true?
R 语言 UniversalBank.csv“ 数据分析
How to use feign to construct multi parameter requests
真正的缓存之王Caffine Cache
已解决:一個錶中可以有多個自增列嗎
Code to Image Converter | 代码生成漂亮图片工具
采用网络远程访问树莓派。
73-找到业务高峰时段的sql示例(报表开发类)
Easyclick fixed status log window
Using qtest for data set test performance test GUI test
[proteus simulation] NE555 delay circuit








