1 逻辑运算
# 生成10名同学,5门功课的数据>>> score = np.random.randint(40, 100, (10, 5))# 取出最后4名同学的成绩,用于逻辑判断>>> test_score = score[6:, 0:5]# 逻辑判断, 如果成绩大于60就标记为True 否则为False>>> test_score > 60array([[ True, True, True, False, True],[ True, True, True, False, True],[ True, True, False, False, True],[False, True, True, True, True]])# BOOL赋值, 将满足条件的设置为指定的值-布尔索引>>> test_score[test_score > 60] = 1>>> test_scorearray([[ 1, 1, 1, 52, 1],[ 1, 1, 1, 59, 1],[ 1, 1, 44, 44, 1],[59, 1, 1, 1, 1]])
2 通用判断函数
np.all()
# 判断前两名同学的成绩[0:2, :]是否全及格>>> np.all(score[0:2, :] > 60)False
np.any()
# 判断前两名同学的成绩[0:2, :]是否有大于90分的>>> np.any(score[0:2, :] > 80)True
3 np.where(三元运算符)
通过使用np.where能够进行更加复杂的运算
np.where()
# 判断前四名学生,前四门课程中,成绩中大于60的置为1,否则为0temp = score[:4, :4]np.where(temp > 60, 1, 0)
复合逻辑需要结合np.logical_and和np.logical_or使用 ```python
判断前四名学生,前四门课程中,成绩中大于60且小于90的换为1,否则为0
np.where(np.logical_and(temp > 60, temp < 90), 1, 0)
判断前四名学生,前四门课程中,成绩中大于90或小于60的换为1,否则为0
np.where(np.logical_or(temp > 90, temp < 60), 1, 0) ```
4 统计运算
axis 轴的取值并不一定,Numpy中不同的API轴的值都不一样,在这里,axis 0代表列, axis 1代表行去进行统计
4.1 统计指标
- min(a, axis)
- 求最小值
- max(a, axis])
- 求最大值
- median(a, axis)
- 求中位数
- mean(a, axis, dtype)
- 求平均值
- std(a, axis, dtype)
- 求均方差
- var(a, axis, dtype)
- 求方差
