Numpy
Numpy数组的属性
arr = np.array([[1, 2, 3],
[4, 5, 6]])
print(arr)
print('dim:', arr.ndim)
print('shape:', arr.shape)
print('size:', arr.size)
print('type:', arr.dtype)
print('data:', arr.data)
print('itemsize:', arr.itemsize)
---------
[[1 2 3]
[4 5 6]]
dim: 2
shape: (2, 3)
size: 6
type: int32
data: <memory at 0x000002A9DD4B0E48>
itemsize: 4
Numpy数组的几个特殊的生成方法
arr1 = np.zeros((3, 4))
arr2 = np.ones((3, 4), dtype=int)
arr3 = np.eye(3)
print(arr1)
print('-'*9)
print(arr2)
print('-'*9)
print(arr3)
---------
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
---------
[[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]
---------
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
Numpy数组的数据生成
arr1 = np.arange(12)
arr2 = np.arange(12).reshape(3, 4)
arr3 = np.arange(10, 21, 2).reshape(2, 3)
arr4 = np.linspace(1, 10, 5)
arr5 = np.linspace(50, 100, 6).reshape(2, 3)
print(arr1)
print('-'*9)
print(arr2)
print('-'*9)
print(arr3)
print('-'*9)
print(arr4)
print('-'*9)
print(arr5)
---------
[ 0 1 2 3 4 5 6 7 8 9 10 11]
---------
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
---------
[[10 12 14]
[16 18 20]]
---------
[ 1. 3.25 5.5 7.75 10. ]
---------
[[ 50. 60. 70.]
[ 80. 90. 100.]]
一维数组的基础运算
a = np.array([10, 20, 30, 40])
b = np.arange(4)
arr1 = a + b
arr2 = a - b
arr3 = a * b
arr4 = a / b
arr5 = np.dot(a, b)
arr6 = np.dot(b, a)
arr7 = b ** 2
print(arr1)
print(arr2)
print(arr3)
print(arr4)
print(arr5)
print(arr6)
print(arr7)
print(b < 3)
---------
[10 21 32 43]
[10 19 28 37]
[ 0 20 60 120]
[ inf 20. 15. 13.33333333]
200
200
[0 1 4 9]
[ True True True False]
二维数组的基础运算
a = np.array([[10, 20, 30],
[40, 50, 60]])
b = np.array([[1, 2, 3],
[4, 5, 6]])
c = np.array([[1, 2],
[3, 4],
[5, 6]])
arr1 = a + b
arr2 = a - b
arr3 = a * b
arr4 = a / b
arr5 = b ** 2
arr6 = np.dot(a, c)
arr7 = np.dot(c, a)
print(arr1)
print('-'*9)
print(arr2)
print('-'*9)
print(arr3)
print('-'*9)
print(arr4)
print('-'*9)
print(arr5)
print('-'*9)
print(arr6)
print('-'*9)
print(arr7)
print('-'*9)
print(b < 3)
---------
[[11 22 33]
[44 55 66]]
---------
[[ 9 18 27]
[36 45 54]]
---------
[[ 10 40 90]
[160 250 360]]
---------
[[10. 10. 10.]
[10. 10. 10.]]
---------
[[ 1 4 9]
[16 25 36]]
---------
[[220 280]
[490 640]]
---------
[[ 90 120 150]
[190 260 330]
[290 400 510]]
---------
[[ True True False]
[False False False]]
数组的求和、最小值、最大值、平均值、中位数…
arr = []
for i in range(12):
arr.append(np.random.randint(1, 10))
arr = np.array(arr).reshape(3, 4)
aw = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.10, 0.11, 0.12])
print(arr)
print('sum:')
print(np.sum(arr))
print(np.sum(arr, axis=0))
print(np.sum(arr, axis=1))
print('min:')
print(np.min(arr))
print(np.min(arr, axis=0))
print(np.min(arr, axis=1))
print('max:')
print(np.max(arr))
print(np.max(arr, axis=0))
print(np.max(arr, axis=1))
print('mean:')
print(np.mean(arr))
print(np.mean(arr, axis=0))
print(np.mean(arr, axis=1))
print('average:')
print(np.average(arr))
print(np.average(arr, weights=aw.reshape(3, 4)))
print('median:')
print(np.median(arr))
print(np.median(arr, axis=0))
print(np.median(arr, axis=1))
print('arg:')
print(np.argmin(arr))
print(np.argmax(arr))
print('cumsum:')
print(np.cumsum(arr))
print('diff:')
print(np.diff(arr))
print('sort:')
print(np.sort(arr))
print('transpose:')
print(np.transpose(arr))
print('clip:')
print(np.clip(arr, 3, 7))
---------
[[2 2 4 8]
[5 2 9 6]
[7 1 7 4]]
sum:
57
[14 5 20 18]
[16 22 19]
min:
1
[2 1 4 4]
[2 2 1]
max:
9
[7 2 9 8]
[8 9 7]
mean:
4.75
[4.66666667 1.66666667 6.66666667 6. ]
[4. 5.5 4.75]
average:
4.75
5.683229813664597
median:
4.5
[5. 2. 7. 6.]
[3. 5.5 5.5]
arg:
9
6
cumsum:
[ 2 4 8 16 21 23 32 38 45 46 53 57]
diff:
[[ 0 2 4]
[-3 7 -3]
[-6 6 -3]]
sort:
[[2 2 4 8]
[2 5 6 9]
[1 4 7 7]]
transpose:
[[2 5 7]
[2 2 1]
[4 9 7]
[8 6 4]]
clip:
[[3 3 4 7]
[5 3 7 6]
[7 3 7 4]]
Numpy数组的索引
arr = np.arange(3, 15)
print(arr)
print('对一维数组索引取值:')
print(arr[2])
arr = arr.reshape(3, 4)
print(arr)
print('对二维数组索引取值:')
print(arr[2])
print('对二维数组取某个具体值:')
print(arr[2][1])
print('对二维数组取某一行或某一列的所有或部分元素:')
print(arr[1, :])
print(arr[1, 1:3])
print(arr[:, 1])
print(arr[:2, 1])
print('对二维数组迭代:')
for row in arr:
print(row)
for column in arr.T:
print(column)
for element in arr.flat:
print(element)
print('把多维数组展平成一维数组:')
print(arr.flatten())
---------
[ 3 4 5 6 7 8 9 10 11 12 13 14]
对一维数组索引取值:
5
[[ 3 4 5 6]
[ 7 8 9 10]
[11 12 13 14]]
对二维数组索引取值:
[11 12 13 14]
对二维数组取某个具体值:
12
对二维数组取某一行或某一列的所有或部分元素:
[ 7 8 9 10]
[8 9]
[ 4 8 12]
[4 8]
对二维数组迭代:
[3 4 5 6]
[ 7 8 9 10]
[11 12 13 14]
[ 3 7 11]
[ 4 8 12]
[ 5 9 13]
[ 6 10 14]
3
4
5
6
7
8
9
10
11
12
13
14
把多维数组展平成一维数组:
[ 3 4 5 6 7 8 9 10 11 12 13 14]
Numpy数组的合并
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.array([[10, 20, 30],
[40, 50, 60]])
d = np.array([[100, 200, 300],
[400, 500, 600]])
print('对一维数组:')
print(np.vstack((a, b)))
print(np.hstack((a, b)))
print(np.concatenate((a, b)))
print('对二维数组:')
print(np.vstack((c, d)))
print(np.hstack((c, d)))
print(np.concatenate((c, d), axis=0))
print(np.concatenate((c, d), axis=1))
---------
对一维数组:
[[1 2 3]
[4 5 6]]
[1 2 3 4 5 6]
[1 2 3 4 5 6]
对二维数组:
[[ 10 20 30]
[ 40 50 60]
[100 200 300]
[400 500 600]]
[[ 10 20 30 100 200 300]
[ 40 50 60 400 500 600]]
[[ 10 20 30]
[ 40 50 60]
[100 200 300]
[400 500 600]]
[[ 10 20 30 100 200 300]
[ 40 50 60 400 500 600]]
Numpy数组的分割
arr = np.arange(12).reshape(3, 4)
print(arr)
print('split:')
print(np.split(arr, 3, axis=0))
print(np.split(arr, 2, axis=1))
print('array_split:')
print(np.array_split(arr, 2, axis=0))
print(np.array_split(arr, 3, axis=1))
print('vsplit & hsplit:')
print(np.vsplit(arr, 3))
print(np.hsplit(arr, 2))
---------
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
split:
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
[array([[0, 1],
[4, 5],
[8, 9]]), array([[ 2, 3],
[ 6, 7],
[10, 11]])]
array_split:
[array([[0, 1, 2, 3],
[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
[array([[0, 1],
[4, 5],
[8, 9]]), array([[ 2],
[ 6],
[10]]), array([[ 3],
[ 7],
[11]])]
vsplit & hsplit:
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
[array([[0, 1],
[4, 5],
[8, 9]]), array([[ 2, 3],
[ 6, 7],
[10, 11]])]
Numpy数组的赋值与copy
a = np.array([1, 2, 3, 4])
b = a
c = b
print('赋值:')
print(a)
print(b)
print(c)
a[2] = 9
print(a)
print(b)
print(c)
b[3] = 16
print(a)
print(b)
print(c)
print(a is b is c)
d = a.copy()
print('copy:')
print(a)
print(d)
a[0] = 10
print(a)
print(d)
d[1] = 4
print(a)
print(d)
print(a is d)
---------
赋值:
[1 2 3 4]
[1 2 3 4]
[1 2 3 4]
[1 2 9 4]
[1 2 9 4]
[1 2 9 4]
[ 1 2 9 16]
[ 1 2 9 16]
[ 1 2 9 16]
True
copy:
[ 1 2 9 16]
[ 1 2 9 16]
[10 2 9 16]
[ 1 2 9 16]
[10 2 9 16]
[ 1 4 9 16]
False