当前位置:网站首页>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 !!!!
边栏推荐
- 启牛送的券商账户是安全的吗?启牛提供的券商账户是真的?
- 从感知机到Transformer,一文概述深度学习简史
- 87-with as写法的5种用途
- 慕课5、服务发现-Nacos
- R language CO2 dataset visualization
- 79-不要看到有order by xxx desc就创建desc降序索引-文末有赠书福利
- LORA技术---LoRa信号从数据流变为LoRa扩频信号,再从射频信号通过解调变为数据
- [observation] innovation in the software industry has entered a "new cycle". How can we make a new start in the changing situation?
- 85-这些SQL调优小'技巧',你学废了吗?
- Precautions for Apollo use
猜你喜欢

软件测试——测试用例设计&测试分类详解

【513. 找树左下角的值】

智能计算之神经网络(BP)介绍

他98年的,我玩不过他...
![[proteus simulation] 74LS138 decoder water lamp](/img/30/7dbdead9c18788cd946b5541e76443.png)
[proteus simulation] 74LS138 decoder water lamp
![[proteus simulation] 8x8LED dot matrix digital cyclic display](/img/a9/0107eb02c7a081fb466b1c7c12baef.png)
[proteus simulation] 8x8LED dot matrix digital cyclic display

Software testing - Test Case Design & detailed explanation of test classification

慕课5、服务发现-Nacos

【文末送书】火遍全网的AI给老照片上色,这里有一份详细教程!
Oracle system/用户被锁定的解决方法
随机推荐
Multi transactions in redis
Résolu: peut - on avoir plus d'une colonne auto - incrémentale dans un tableau
Stochastic Adaptive Dynamics of a Simple Market as a Non-Stationary Multi-Armed Bandit Problem
Easyclick update Gallery
如何使用Feign构造多参数的请求
laravel+宝塔计划任务
山东大学科技文献期末复习(个人速成向)
让知识付费系统视频支持M3U8格式播放的方法
从感知机到Transformer,一文概述深度学习简史
访问成功但抛出异常:Could not find acceptable representation
讲真,Kotlin 协程的挂起没那么神秘(原理篇)
R language airpassengers dataset visualization
智能计算之神经网络(Hopfield网络-DHNN,CHNN )介绍
Golang學習筆記—結構體
智能計算之神經網絡(BP)介紹
[observation] innovation in the software industry has entered a "new cycle". How can we make a new start in the changing situation?
MySql给某列字段添加(追加)前缀后缀
一张图解码 OpenCloudOS 社区开放日
他98年的,我玩不过他...
慕课6、实现负载均衡-Ribbon