numpy where()函数

原文链接:https://blog.csdn.net/qq_32005671/article/details/74011993

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】
 

猜你喜欢

转载自blog.csdn.net/y_f_raquelle/article/details/83477278