当前位置:网站首页>Numpy数组中d[True]=1的含义
Numpy数组中d[True]=1的含义
2022-08-02 22:26:00 【Tim_Van】
一、解释
Numpy数组中d[True]的含义,是d中所有值都赋值为1。
d[True] = 1,是所有值都满足条件的意思,并且其值均改为1,同理如果为 d[False] = 1 并不起实际作用。
二、示例
import numpy as np
if __name__ == "__main__":
# 当数值为int时
d = np.array(-3)
d[True] = 9
print(d) # 输出9
# 当数值为列表时
d = np.array([-3, 0, 3])
d[True] = 9
print(d) # 输出[9 9 9]如上述代码示例所示,当数值为列表时,推测 d[True] 自动扩展成 d[[True, True, True]]
三、问题来源
def derivative(x): # ReLU 函数的导数
d = np.array(x, copy=True) # 用于保存梯度的张量
d[x < 0] = 0 # 元素为负的导数为 0
d[x >= 0] = 1 # 元素为正的导数为 1
return d在学习上述ReLU函数求导的代码中,遇见了一个没理解的地方。例如当函数输入x=-3,返回d=0。但是d[x < 0] = 0和d[x >= 0] = 1这两行代码没能理解其意义。目前的理解是,如x = -3时,d[x < 0] = 0 这行代码等价于 d[True] = 0,类似的d[x >= 0] = 1则等价于d[False] = 1。
四、参考资料
1. 《NumPy 数组筛选》
2. 感谢 Dr. RuoBing
边栏推荐
- 买母婴产品先来京东“券民空间站”抢券!大牌好物低至5折
- WAF WebShell Trojan free to kill
- Tanabata is here - the romance of programmers
- 【UE5 骨骼动画】全形体IK导致Two Bone IK只能斜着移动,不能平移
- redis的学习笔记
- 反弹shell原理与实现
- AcWing 2983. 玩具
- You and I will meet the needs of: how to export the data in a MySQL simple ~!Practical!
- 在软件测试行业近20年的我,再来和大家谈谈今日的软件测试
- RuoYi-App启动教程
猜你喜欢
随机推荐
严格反馈非线性系统基于事件触发的自抗扰预设有限时间跟踪控制
ROS2初级知识(9):bag记录过程数据和重放
If the watermark according to how to realize the function
Directing a non-relational database introduction and deployment
总数据量超万亿行,玉溪卷烟厂通过正确选择时序数据库轻松应对
The only way to go from a monthly salary of 10k to 30k: automated testing
创建型模式 - 单例模式Singleton
Kubernetes 进阶训练营 网络
虚拟内存 virualmemory
ML之PDP:基于titanic泰坦尼克是否获救二分类预测数据集利用PDP部分依赖图对RF随机森林和LightGBM模型实现可解释性案例
采用QT进行OpenGL开发(三)着色器编程
Towards a General Purpose CNN for Long Range Dependencies in ND
centos7安装mysql5.7
WAF WebShell Trojan free to kill
Week 7 - Distributional Representations(分布表示)
测试人生 | 阿里实习 90 天:从实习生的视角谈谈个人成长
vscode 自定义快捷键——设置eslint
记一次mysql查询慢的优化历程
mysql根据多字段分组——group by带两个或多个参数
学习基因富集工具DAVID(2)









