# 【3】数据分析-1-数据的处理--numpy--4--逻辑函数(all, any,where,isin)

## 一、真值检验

### np.all

>>> np.all([[True,False],[True,True]])
False
>>> np.all([[True,False],[True,True]], axis=0)
array([ True, False], dtype=bool)
>>> np.all([-1, 4, 5])
True
>>> np.all([1.0, np.nan])
True

>>> o=np.array([False])
>>> z=np.all([-1, 4, 5], out=o)
>>> id(z), id(o), z
(28293632, 28293632, array([ True], dtype=bool))

Not a Number (NaN), positive infinity and negative infinity evaluate to True because these are not equal to zero.


### np.any

>>> np.any([[True, False], [True, True]])
True
>>>
>>> np.any([[True, False], [False, False]], axis=0)
array([ True, False])
>>>
>>> np.any([-1, 0, 5])
True
>>>
>>> np.any(np.nan)
True
>>>
>>> o=np.array([False])
>>> z=np.any([-1, 4, 5], out=o)
>>> z, o
(array([ True]), array([ True]))
>>> # Check now that z is a reference to o
>>> z is o
True
>>> id(z), id(o) # identity of z and o
(191614240, 191614240)


## 二、条件选择

### 2.1 numpy.where

numpy.where() 有两种用法：

#### 1.np.where(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', 'chosen'],
['chosen', 'chosen']], dtype='<U10')


#### 2.np.where(condition)

>>> a = np.array([2,4,6,8,10])
>>> np.where(a > 5)             # 返回索引
(array([2, 3, 4]),)
>>> a[np.where(a > 5)]              # 等价于 a[a>5]
array([ 6,  8, 10])

>>> np.where([[0, 1], [1, 0]])
(array([0, 1]), array([1, 0]))


>>> a = np.arange(27).reshape(3,3,3)
>>> a
array([[[ 0,  1,  2],
[ 3,  4,  5],
[ 6,  7,  8]],

[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],

[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]])

>>> np.where(a > 5)
(array([0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2]),
array([2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2]),
array([0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]))


#符合条件的元素为

   [ 6,  7,  8]],

[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],

[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]]


## 三、语义判断

### 3.1 numpy.isin

>>> element = 2*np.arange(4).reshape((2, 2))
>>> element
array([[0, 2],
[4, 6]])
>>> test_elements = [1, 2, 4, 8]
array([[ False,  True],
[ True,  False]])
array([2, 4])


>>> np.nonzero(mask)
(array([0, 1]), array([1, 0]))


>>> mask = np.isin(element, test_elements, invert=True)
array([[ True, False],
[ False, True]])
array([0, 6])


>>> test_set = {1, 2, 4, 8}
>>> np.isin(element, test_set)
array([[ False, False],
[ False, False]])

>>> np.isin(element, list(test_set))
array([[ False,  True],
[ True,  False]])