One - One Code All

Blog Content

numpy逻辑函数np.logical_and/or/not (逻辑与/或/非)

Python 统计学-科学计算   2015-03-02 20:07:58


import numpy as np

a = np.array([1, 3, 5, 6, 9, 10, 14, 15, 56])


np.where(np.logical_and(a>=6, a<=10))

# returns (array([3, 4, 5]),)


a = np.array([1, 3, 5, 6, 9, 10, 14, 15, 56]) . np.argwhere((a>=6) & (a<=10))


# ----------------------


np.logical_and/or/not (逻辑与/或/非)


np.logical_and(逻辑与)


Syntax


np.logical_and(x1, x2, *args, **kwargs)

Test


>>> np.logical_and(True, False)

False

>>> np.logical_and([True, False], [False, False])

array([False, False], dtype=bool)

 

>>> x = np.arange(5)

>>> np.logical_and(x>1, x<4)

array([False, False,  True,  True, False], dtype=bool)

np.logical_or(逻辑或)


Syntax


np.logical_or(x1, x2, *args, **kwargs)

Test


>>> np.logical_or(True, False)

True

>>> np.logical_or([True, False], [False, False])

array([ True, False], dtype=bool)

 

>>> x = np.arange(5)

>>> np.logical_or(x < 1, x > 3)

array([ True, False, False, False,  True], dtype=bool)

np.logical_not(逻辑非)


Syntax


logical_not(x, *args, **kwargs)

Test

>>> np.logical_not(3)

False

>>> np.logical_not([True, False, 0, 1])

array([False,  True,  True, False], dtype=bool)

 

>>> x = np.arange(5)

>>> np.logical_not(x<3)



上一篇:pandas中dataframe和series索引定位和选择loc:Indexing and Selecting Data
下一篇:RuntimeWarning: invalid value encountered in true_divide

The minute you think of giving up, think of the reason why you held on so long.