python np.where()

来源:互联网 发布:js缺少对象 编辑:程序博客网 时间:2024/06/11 23:28

numpy.where()函数是三元表达式x if condition else y的矢量化版本。假设我们有一个布尔数组和两个值数组:

x = np.array([1.1, 1.2, 1.3, 1.4, 1.5])y = np.array([2.1, 2.2, 2.3, 2.4, 2.5])condition = np.array([True, False, True, True, False])
假设我们想要根据condition中的值选取x和y的值:当condition中的值为True时,选取x的值,否则从y中选取。

result = np.where(condition, x, y)
打印的结果为:【1.1, 2.2, 1.3, 1.4, 2.5】


原创粉丝点击