数据科学:numpy.where() 的用法
原文出处: numpy.where() 用法讲解 原创作者: massquantity numpy.where() 有两种用法: 1. np.where(condition, x, y) 满足条件(condition),输出x,不满足输出y 情景(一) >>> aa = np.arange(10) >>> np.where(aa,1,-1) array([-1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) # 0为False,所以第一个输出-1 >>> np.where(aa > 5,1,-1) array([-1, -1, -1, -1, -1, -1, 1, 1, 1, 1]) >>> np.where([[True,False], [True,True]], # 官网上的例子 [[1,2], [3,4]], [[9,8], [7,6]]) array([[1, 8], [3, 4]]) 情景(二) >>> a = 10 >>> np.where([[a > 5,a < 5], [a == 10,a == 7]], [["chosen","not chosen"], ["chosen","not chosen"]], [["not chosen","chosen"], ["not chosen","chosen"]]) array([['chosen',