import numpy as np
1.逻辑运算
score = np.random.randint(40, 100, [10, 5])
score
array([[98, 47, 44, 77, 43],
[46, 78, 52, 54, 87],
[64, 78, 67, 75, 59],
[59, 72, 78, 59, 50],
[92, 55, 56, 81, 84],
[54, 94, 41, 72, 48],
[96, 40, 67, 57, 71],
[69, 42, 88, 52, 95],
[74, 85, 98, 64, 71],
[68, 62, 47, 96, 75]])
test_score=score[6:,:]
test_score
array([[69, 60, 85, 58, 95],
[44, 45, 52, 52, 81],
[56, 43, 81, 99, 96],
[97, 51, 64, 99, 95]])
test_score>60
array([[False, False, True, True, True],
[ True, False, True, True, False],
[ True, True, True, True, True],
[False, False, True, True, False]])
test_score[test_score > 60] = 1
test_score
array([[ 1, 60, 1, 58, 1],
[44, 45, 52, 52, 1],
[56, 43, 1, 1, 1],
[ 1, 51, 1, 1, 1]])
2.通用函数判断
np.all(score[0:2, :] > 60)
False
np.any(score[0:2, :] > 90)
True
3.np.where(三元运算符)
tem = score[0:4, 0:4]
np.where(tem > 60, 1, 0)
array([[1, 0, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 0],
[1, 1, 1, 0]])
np.where(np.logical_and(tem > 60, tem < 90), 1, 0)
array([[0, 0, 0, 1],
[0, 1, 0, 0],
[1, 1, 1, 1],
[0, 1, 1, 0]])
np.where(np.logical_or(tem < 60, tem > 90), 1, 0)
array([[1, 1, 1, 0],
[1, 0, 1, 1],
[0, 0, 0, 0],
[1, 0, 0, 1]])
4.统计运算
temp = score[:4, :]
print("前四名学生,各科成绩的最高分:{}".format(np.max(temp, axis=0)))
print("前四名学生,各科成绩的最低分:{}".format(np.min(temp, axis=0)))
print("前四名学生,各科成绩波动情况:{}".format(np.std(temp, axis=0)))
print("前四名学生,各科成绩的平均分:{}".format(np.mean(temp, axis=0)))
temp
前四名学生,各科成绩的最高分:[98 78 78 77 87]
前四名学生,各科成绩的最低分:[46 47 44 54 43]
前四名学生,各科成绩波动情况:[19.20123694 12.79404158 13.16007219 9.93415824 16.7238602 ]
前四名学生,各科成绩的平均分:[66.75 68.75 60.25 66.25 59.75]
array([[98, 47, 44, 77, 43],
[46, 78, 52, 54, 87],
[64, 78, 67, 75, 59],
[59, 72, 78, 59, 50]])
print("前四名学生,各科成绩最高分对应的下标:{}".format(np.argmax(temp, axis=0)))
前四名学生,各科成绩最高分对应的下标:[0 1 3 0 1]