当前位置:网站首页>numpy库中的函数 bincount() where() diag() all()
numpy库中的函数 bincount() where() diag() all()
2022-08-09 10:28:00 【白十月】
毕设过程中遇到的,汇总在一起~
NumPy一元函数对ndarray中的数据执行元素级运算的函数
np.abs(x)、np.fabs(x) :
计算数组各元素的绝对值
np.sqrt(x) :
计算数组各元素的平方根
np.square(x) :
计算数组各元素的平方
np.log( )
以10为底 np.log10(x)
以e为底 np.log(x)
copy( )
numpy.bincount( )用法
输入数组x需要是非负整数,且是一维数组。
给出了它的索引值在x中出现的次数(在默认权重下)
本例中最大值是 6 ,因此显示的就是[0,6]这些数的次数
若不指定minlength,输出的个数应该为6+1=7
import numpy as np
x=np.array([1,2,3,3,0,1,4,0,6])
a = np.bincount(x)
print(a)
结果为:
[2 2 1 2 1 0 1]
增加参数 minlength
当minlength长度小于7时,输出不受影响;
但是,当minlength长度大于7时,其余位置会自动补0
import numpy as np
x=np.array([1,2,3,3,0,1,4,0,6])
a = np.bincount(x, minlength=20)
print(a)
结果为:
[2 2 1 2 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0]
np.where( )用法
用法一 np.where(condition)
只有条件 (condition),没有x和y,则输出满足条件 (即非0) 元素的坐标 (等价于numpy.nonzero)。这里的坐标以tuple的形式给出,
import numpy as np
x=np.array([1,2,3,3,0,1,4,0,6])
a = np.where(x > 2)
print(a)
print(x[a])
结果为:
(array([2, 3, 6, 8], dtype=int64),)
[3 3 4 6]
用法二 np.where(condition, x, y)
满足条件(condition),输出x,不满足输出y
import numpy as np
x=np.array([1,2,3,3,0,1,4,0,6])
a = np.where(x > 2,1,0)
print(a)
结果为:
[0 0 1 1 0 0 1 0 1]
np.diag(array)
array是一个1维数组时,结果形成一个以一维数组为对角线元素的矩阵
array是一个二维矩阵时,结果输出矩阵的对角线元素
np.maximum(X, Y, out=None)
X和Y逐位进行比较,选择最大值.
numpy.all( )
numpy.all(a,axis = None,out = None,keepdims = <无值>)
测试是否沿给定轴的所有数组元素求值为True。
文档:
https://numpy.org/doc/1.17/reference/generated/numpy.all.html?highlight=numpy%20all#numpy.all
边栏推荐
- 使用.NET简单实现一个Redis的高性能克隆版(四、五)
- 踩坑scrollIntoView
- [贴装专题] 视觉贴装平台与贴装流程介绍
- Loop nesting and basic operations on lists
- The GNU Privacy Guard
- 力扣(LeetCode)220. 存在重复元素 III(2022.08.08)
- antd的Table列选择、列拓展
- 1004 成绩排名 (20 分)
- 单元测试1之单元测试的引用
- Dialogue with the DPO of a multinational consumer brand: How to start with data security compliance?See you on 8.11 Live!
猜你喜欢
【Linux】宝塔面板设置MySQL慢查询日志,未走索引日志
Dialogue with the DPO of a multinational consumer brand: How to start with data security compliance?See you on 8.11 Live!
机器学习--朴素贝叶斯(Naive Bayes)
MySQL执行过程及执行顺序
壁纸
[相机配置] 海康相机丢包配置环境
Attentional Feature Fusion
Shell script combat (2nd edition) / People's Posts and Telecommunications Press Script 2 Validate input: letters and numbers only
机器学习--线性回归(Linear Regression)
Solve the ali cloud oss - the original 】 【 exe double-click response can't open, to provide a solution
随机推荐
Umi Hooks
按键精灵之输出文本
antd表单
相伴成长,彼此成就 用友U9 cloud做好制造业数智化升级的同路人
Unix Environment Programming Chapter 15 15.3 Functions popen and pclose
Redis cache update strategy actively
MySQL索引的B+树到底有多高?
Oracle数据库:for update 和for update nowait的区别
VBA实战(11) - 工作表(Sheet) 操作汇总
Unix Environment Programming Chapter 14 14.4 I/O Multiplexing
xmms的歌词显示及音量控制OK
unix系统编程 第十五章 15.2管道
shell脚本实战(第2版)/人民邮电出版社 脚本2 验证输入:仅限字母和数字
How to quickly get through the mirror release process?
京东物流与五菱将开发联名版定制产品
Battery modeling, analysis and optimization (Matlab code implementation)
Win7 远程桌面限制IP
tensor.eq() tensor.item() tensor.argmax()
3D打印了这个杜邦线理线神器,从此桌面再也不乱了
stimulus.js 初体验