当前位置:网站首页>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 !!!!
边栏推荐
- Résolu: peut - on avoir plus d'une colonne auto - incrémentale dans un tableau
- [in depth understanding of tcapulusdb technology] form creation and approval of document acceptance
- PlainSelect.getGroupBy()Lnet/sf/jsqlparser/statement/select/GroupByElement;
- Three ways of extending ribbon to support Nacos weight
- Xunrui CMS custom data interface PHP executable code
- 89-oracle SQL写法与优化器缺陷一例
- R language airpassengers dataset visualization
- 让知识付费系统视频支持M3U8格式播放的方法
- 智能计算之神经网络(Hopfield网络-DHNN,CHNN )介绍
- [resolved] -go_ out: protoc-gen-go: Plugin failed with status code 1.
猜你喜欢

Alibaba cloud video on demand playback error, console access code:4400

Moke 5. Service discovery -nacos

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

深度学习常用损失函数总览:基本形式、原理、特点
![[proteus simulation] 8x8LED dot matrix digital cyclic display](/img/a9/0107eb02c7a081fb466b1c7c12baef.png)
[proteus simulation] 8x8LED dot matrix digital cyclic display

Multi transactions in redis

Overview of common loss functions for in-depth learning: basic forms, principles and characteristics

R 语言USArrests 数据集可视化

R 语言nutrient数据集的可视化
![[resolved] -go_ out: protoc-gen-go: Plugin failed with status code 1.](/img/da/9ced1c0a9c386bc8da75dddaa443e5.png)
[resolved] -go_ out: protoc-gen-go: Plugin failed with status code 1.
随机推荐
如何计算 R 中的基尼系数(附示例)
87-with as写法的5种用途
Dynamicdatabasesource, which supports the master-slave database on the application side
R language universalbank CSV "data analysis
R 语言USArrests 数据集可视化
Golang学习笔记—结构体
Moke 5. Service discovery -nacos
How to calculate the Gini coefficient in R (with examples)
【奇葩需求之记录对象不同的日志】
Easydss problem and solution summary
【观察】软件行业创新进入“新周期”,如何在变局中开新局?
MySQL基础——约束
71-对2010年阿里一道Oracle DBA面试题目的分析
PlainSelect.getGroupBy()Lnet/sf/jsqlparser/statement/select/GroupByElement;
迅睿CMS 自定义数据接口-php执行文件代码
采用QTest进行数据集测试-性能测试-GUI测试
跨域 CORS/OPTIONS
怎样实现网页端im即时通讯中的@人功能
AAAI 2022 | 传统GAN修改后可解释,并保证卷积核可解释性和生成图像真实性
如何使用Feign构造多参数的请求