import numpy as np
1.逻辑运算
score = np.random.randint(40, 100, (10, 5))
score
array([[65, 92, 42, 52, 91],
[41, 92, 82, 55, 95],
[77, 66, 88, 94, 43],
[75, 92, 53, 89, 86],
[94, 59, 50, 60, 73],
[57, 57, 67, 99, 72],
[84, 77, 83, 84, 49],
[88, 81, 72, 85, 85],
[72, 90, 61, 69, 72],
[54, 63, 70, 85, 99]])
test_score = score[6:, 0: 5]
test_score
array([[84, 77, 83, 84, 49],
[88, 81, 72, 85, 85],
[72, 90, 61, 69, 72],
[54, 63, 70, 85, 99]])
test_score > 60
array([[ True, True, True, True, False],
[ True, True, True, True, True],
[ True, True, True, True, True],
[False, True, True, True, True]])
test_score[test_score > 60] = 1
test_score
array([[ 1, 1, 1, 1, 49],
[ 1, 1, 1, 1, 1],
[ 1, 1, 1, 1, 1],
[54, 1, 1, 1, 1]])
2.通用判断函数
np.all(score[0:2, :] > 60)
False
score
array([[65, 92, 42, 52, 91],
[41, 92, 82, 55, 95],
[77, 66, 88, 94, 43],
[75, 92, 53, 89, 86],
[94, 59, 50, 60, 73],
[57, 57, 67, 99, 72],
[ 1, 1, 1, 1, 49],
[ 1, 1, 1, 1, 1],
[ 1, 1, 1, 1, 1],
[54, 1, 1, 1, 1]])
np.any(score[0:2, :] > 90)
True
3.np.where(三元运算符)
temp = score[:4, :4]
temp
array([[65, 92, 42, 52],
[41, 92, 82, 55],
[77, 66, 88, 94],
[75, 92, 53, 89]])
np.where(temp > 60, 1, 0)
array([[1, 1, 0, 0],
[0, 1, 1, 0],
[1, 1, 1, 1],
[1, 1, 0, 1]])
np.where(np.logical_and(temp > 60, temp < 90), 1, 0)
array([[1, 0, 0, 0],
[0, 0, 1, 0],
[1, 1, 1, 0],
[1, 0, 0, 1]])
np.where(np.logical_or(temp > 90, temp < 60), 1, 0)
array([[0, 1, 1, 1],
[1, 1, 0, 1],
[0, 0, 0, 1],
[0, 1, 1, 0]])
4.统计运算
temp = score[:4, :]
temp
array([[65, 92, 42, 52, 91],
[41, 92, 82, 55, 95],
[77, 66, 88, 94, 43],
[75, 92, 53, 89, 86]])
np.max(temp)
95
np.mean(temp)
73.5
np.max(temp, axis=0)
array([77, 92, 88, 94, 95])
np.argmax(temp)
9
np.argmax(temp, axis = 1)
array([1, 4, 3, 1], dtype=int64)
5.数组间运算
a = np.array([[1,2,3],[3,4,5]])
a
array([[1, 2, 3],
[3, 4, 5]])
a + 3
array([[4, 5, 6],
[6, 7, 8]])
a / 2
array([[0.5, 1. , 1.5],
[1.5, 2. , 2.5]])
a = [1,2,3]
a*3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
arr1 = np.random.randint(1,10,(2,6))
arr2 = np.random.randint(5,8,(2,4))
print(arr1
,arr2)
[[9 3 9 7 7 4]
[4 1 8 5 7 9]] [[6 6 5 6]
[5 6 7 6]]
arr2=np.array([[1],[3]])
print(arr1.shape
,arr2.shape)
(2, 6) (2, 1)
arr1+arr2
array([[10, 4, 10, 8, 8, 5],
[ 7, 4, 11, 8, 10, 12]])
6.矩阵运算
a = np.random.randint(80,90,(6,2))
a
array([[87, 80],
[81, 81],
[84, 88],
[89, 87],
[84, 86],
[84, 86]])
b = np.array([[0.7],[0.3]])
np.dot(a,b)
array([[84.9],
[81. ],
[85.2],
[88.4],
[84.6],
[84.6]])
np.matmul(a, b)
array([[84.9],
[81. ],
[85.2],
[88.4],
[84.6],
[84.6]])
源码
import numpy as np
score = np.random.randint(40, 100, (10, 5))
score
test_score = score[6:, 0: 5]
test_score
test_score > 60
test_score[test_score > 60] = 1
test_score
np.all(score[0:2, :] > 60)
score
np.any(score[0:2, :] > 90)
temp = score[:4, :4]
temp
np.where(temp > 60, 1, 0)
np.where(np.logical_and(temp > 60, temp < 90), 1, 0)
np.where(np.logical_or(temp > 90, temp < 60), 1, 0)
temp = score[:4, :]
temp
np.max(temp)
np.mean(temp)
np.max(temp, axis=0)
np.argmax(temp)
np.argmax(temp, axis = 1)
a = np.array([[1,2,3],[3,4,5]])
a
a + 3
a / 2
a = [1,2,3]
a*3
arr1 = np.random.randint(1,10,(2,6))
arr2 = np.random.randint(5,8,(2,4))
print(arr1
,arr2)
arr2=np.array([[1],[3]])
print(arr1.shape
,arr2.shape)
arr1+arr2
a = np.random.randint(80,90,(6,2))
a
b = np.array([[0.7],[0.3]])
np.dot(a,b)
np.matmul(a, b)