import numpy as np
版本
np.__version__
'1.16.2'
整形数组
np.array([1, 2, 3, 4, 5], dtype='int8')
array([1, 2, 3, 4, 5], dtype=int8)
浮点型数组
np.array([1, 2, 3, 4, 5], dtype='float32')
array([1., 2., 3., 4., 5.], dtype=float32)
嵌套列表构成的多维数组,内层的列表被当作二维数组的行
np.array([range(i, i+3) for i in [2, 4, 6]])
array([[2, 3, 4],
[4, 5, 6],
[6, 7, 8]])
创建一个长度为10的数组,数组的值都是0
np.zeros(10, dtype='int8')
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=int8)
np.full(10, 0, dtype='int8')
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=int8)
创建一个长度为10的数组,数组的值都是1
np.ones(10, dtype='float32')
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], dtype=float32)
np.full(10, 1, dtype='float32')
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], dtype=float32)
创建一个3×5的浮点型数组,数组的值都是3.14
np.full((3, 5), 3.14, dtype='float32')
array([[3.14, 3.14, 3.14, 3.14, 3.14],
[3.14, 3.14, 3.14, 3.14, 3.14],
[3.14, 3.14, 3.14, 3.14, 3.14]], dtype=float32)
创建一个数组,数组的值是一个线性序列
np.arange(0, 20, 2)
array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])
创建一个5个元素的数组,这5个数均匀地分配到0~1
np.linspace(0, 1, 5)
array([0. , 0.25, 0.5 , 0.75, 1. ])
创建一个3×3的、在0~1均匀分布的随机数数组
np.random.random((3, 3))
array([[0.28365282, 0.48392397, 0.77984123],
[0.38999091, 0.57175623, 0.44926413],
[0.93162627, 0.79461497, 0.57614447]])
创建一个3×3的、均值为0、方差为1的正态分布的随机数数组
np.random.normal(0, 1, (3, 3))
array([[-1.01797743, -0.12390125, 0.76398104],
[-0.34083355, -0.62158486, 0.41385314],
[ 0.6994842 , -0.86558774, 0.34496025]])
创建一个3×3的、[0, 10)区间的随机整型数组
np.random.randint(0, 10, (3, 3))
array([[0, 0, 6],
[0, 9, 9],
[9, 8, 2]])
创建一个3×3的单位矩阵
np.eye(3)
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
创建一个由3个整型数组成的未初始化的数组,数组的值是内存空间中的任意值
np.empty(3)
array([1., 1., 1.])
数组属性
np.random.seed(0)
x1 = np.random.randint(0, 10, size=6)
x1
array([5, 0, 3, 3, 7, 9])
x2 = np.random.randint(0, 10, size=(3, 4))
x2
array([[3, 5, 2, 4],
[7, 6, 8, 8],
[1, 6, 7, 7]])
x3 = np.random.randint(0, 10, size=(3, 4, 5))
x3
array([[[8, 1, 5, 9, 8],
[9, 4, 3, 0, 3],
[5, 0, 2, 3, 8],
[1, 3, 3, 3, 7]],
[[0, 1, 9, 9, 0],
[4, 7, 3, 2, 7],
[2, 0, 0, 4, 5],
[5, 6, 8, 4, 1]],
[[4, 9, 8, 1, 1],
[7, 9, 9, 3, 6],
[7, 2, 0, 3, 5],
[9, 4, 4, 6, 4]]])
print("x3.ndim:\t", x3.ndim)
x3.ndim: 3
print("x3.shape:\t", x3.shape)
x3.shape: (3, 4, 5)
print("x3.size:\t", x3.size)
x3.size: 60
print("x3.dtype:\t", x3.dtype)
x3.dtype: int32
print("x3.itemsize", x3.itemsize, "bytes")
x3.itemsize 4 bytes
print("x3.nbytes", x3.nbytes, "bytes")
x3.nbytes 240 bytes
数组索引:获取单个元素
x1[0]
5
x1[3]
3
获取数组的末尾元素,可以用负值索引
x1[-1]
9
x1[-2]
7
数组切片:获取子数组 x[start:stop:step]
x = np.arange(10)
x
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
x[:5]
array([0, 1, 2, 3, 4])
x[5:]
array([5, 6, 7, 8, 9])
x[4:7]
array([4, 5, 6])
x[::2]
array([0, 2, 4, 6, 8])
x[1::2]
array([1, 3, 5, 7, 9])
x[::-1]
array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
x[5::-2]
array([5, 3, 1])
数组视图
x2_sub = x2[:2, :2]
x2_sub
array([[3, 5],
[7, 6]])
x2_sub[0, 0] = 99
print(x2_sub)
[[99 5]
[ 7 6]]
print(x2)
[[99 5 2 4]
[ 7 6 8 8]
[ 1 6 7 7]]
数组副本
x2_sub_copy = x2[:2, :2].copy()
x2_sub_copy
array([[99, 5],
[ 7, 6]])
x2_sub_copy[0, 0] = 404
x2_sub_copy
array([[404, 5],
[ 7, 6]])
print(x2)
[[99 5 2 4]
[ 7 6 8 8]
[ 1 6 7 7]]
数组变形
grid = np.arange(1, 10).reshape((3, 3))
grid
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
x = np.array([1, 2, 3])
x
array([1, 2, 3])
x.reshape((1, 3))
array([[1, 2, 3]])
x[np.newaxis, :]
array([[1, 2, 3]])
x.reshape((3, 1))
array([[1],
[2],
[3]])
x[:, np.newaxis]
array([[1],
[2],
[3]])
一维数组的拼接
x = np.array([1, 2, 3])
y = np.array([3, 2, 1])
z = [99, 99, 99]
np.concatenate([x, y, z])
array([ 1, 2, 3, 3, 2, 1, 99, 99, 99])
二维数组的拼接
grid = np.array([
[1, 2, 3],
[4, 5, 6]
])
grid
array([[1, 2, 3],
[4, 5, 6]])
np.concatenate([grid, grid], axis=0)
array([[1, 2, 3],
[4, 5, 6],
[1, 2, 3],
[4, 5, 6]])
np.concatenate([grid, grid], axis=1)
array([[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]])
np.vstack([x, grid])
array([[1, 2, 3],
[1, 2, 3],
[4, 5, 6]])
np.hstack([grid, x[:2].reshape(2, 1)])
array([[1, 2, 3, 1],
[4, 5, 6, 2]])
切分一维数组
x = [1, 2, 3, 99, 99, 3, 2, 1]
x1, x2, x3 = np.split(x, [3, 5])
print(x1, x2, x3)
[1 2 3] [99 99] [3 2 1]
grid = np.arange(16).reshape(4, 4)
grid
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
切分二维数组
upper, lower = np.vsplit(grid, [2])
print(upper)
print(lower)
[[0 1 2 3]
[4 5 6 7]]
[[ 8 9 10 11]
[12 13 14 15]]
left, right = np.hsplit(grid, [2])
print(left)
print(right)
[[ 0 1]
[ 4 5]
[ 8 9]
[12 13]]
[[ 2 3]
[ 6 7]
[10 11]
[14 15]]