逻辑运算

逻辑运算

import numpy as np

# 生成10名同学,5门功课的数据
>>> score = np.random.randint(40, 100, (10, 5))

>>> score

array([[77, 71, 93, 56, 55],
       [97, 71, 68, 92, 52],
       [78, 67, 59, 58, 58],
       [70, 60, 42, 49, 88],
       [94, 87, 46, 55, 69],
       [96, 80, 86, 71, 51],
       [51, 60, 69, 48, 59],
       [80, 60, 62, 49, 46],
       [79, 84, 66, 97, 88],
       [95, 73, 94, 96, 59]])

>>> test_score = score[6:,0:5]

>>> test_score

array([[51, 60, 69, 48, 59],
       [80, 60, 62, 49, 46],
       [79, 84, 66, 97, 88],
       [95, 73, 94, 96, 59]])

# 逻辑判断,如果成绩大于60就标记为True 否则为False
>>> test_score > 60

array([[False, False,  True, False, False],
       [ True, False,  True, False, False],
       [ True,  True,  True,  True,  True],
       [ True,  True,  True,  True, False]])

# BOOL赋值,将满足条件的设置为指定的值-布尔索引
>>> test_score[test_score > 60] = 1

>>> test_score

array([[51, 60,  1, 48, 59],
       [ 1, 60,  1, 49, 46],
       [ 1,  1,  1,  1,  1],
       [ 1,  1,  1,  1, 59]])