numpy使用

NumPy 是一个运行速度非常快的数学库,主要用于数组计算,包含:

  • 一个强大的N维数组对象 ndarray
  • 广播功能函数
  • 整合 C/C++/Fortran 代码的工具
  • 线性代数、傅里叶变换、随机数生成等功能

安装

通过pip install 安装 numpy

pip install numpy

导入

import numpy as np

查看版本

image-20220630161950742

ndarray

常用创建方法

1
2
3
# 创建全是1的ndarray, 默认是float64类型
# np.ones(shape, dtype=None, order='C')
np.ones(shape=(4,5))

image-20220704104818993

1
2
3
# 全是0的ndarray
# np.zeros(shape, dtype=float, order='C')
np.zeros(shape=(4,4))

image-20220704105615223

1
2
3
# 使用指定的元素来填充,
# np.full(shape, fill_value, dtype=None, order='C')
np.full(shape=(3,3), fill_value=8, dtype=np.float64)

image-20220704111543026

1
2
3
4
5
# 等分一个范围
# np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
np.linspace(0, 100, num=50, endpoint=False, retstep=True)
# endpoint:True则包含stop;False则不包含stop
# retstep : 返回步长,并以元祖输出

image-20220704132615078

1
2
3
4
# 和python的 range一样
# 当step是非整数的时候,尽量使用np.linspace
# np.arange([start, ]stop, [step, ]dtype=None)
np.arange(0, 100,2)

image-20220704132758892

1
2
3
# 全是整数的ndarray 左闭右开区间.
# np.random.randint(low, high=None, size=None, dtype='l')
np.random.randint(0, 150, size=(2,3,4), )

image-20220704133905777

1
2
3
4
5
# 标准正太分布
# 平均值为0, 方差 为1 的正态分布叫做标准正态分布
# np.random.randn(d0, d1, ..., dn)
np.random.randn(2,3, 2,3)
# 分为2个大矩阵,然后每个矩阵里又有3个2行3列的小矩阵,

image-20220704142321831

1
2
3
4
# 正态分布
# np.random.normal(loc=0.0, scale=1.0, size=None)
np.random.normal(loc=10, scale=3,size=(3,3))
# 均值,标准差,输出维度

image-20220704144755072

1
2
3
# 生成0到1的随机数,左闭右开
# np.random.random(size=None)
np.random.random(size=(3,2,2))

image-20220704152400609

1
2
# 和np.random.random一样
np.random.rand(2,3,3)

image-20220704152806987

ndarray的属性

1
2
3
4
5
4个必记参数:
ndim:维度
shape:形状(各维度的长度)
size:总长度
dtype:元素类型

image-20220704154531580

ndarray的基本操作

索引

一维与列表完全一致 多维时同理

二维

1
2
3
4
5
6
n = np.random.randint(0,150, size=(4,5))
n
array([[ 77, 14, 134, 14, 43],
[ 85, 123, 124, 91, 108],
[ 64, 31, 94, 39, 24],
[ 44, 77, 26, 67, 80]])

image-20220704161835043

三维

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
n = np.random.randint(0,100, size=(4,5,3))
n
array([[[ 9, 39, 23],
[13, 11, 6],
[60, 62, 39],
[76, 86, 68],
[24, 66, 39]],

[[42, 42, 95],
[70, 4, 87],
[45, 23, 60],
[46, 58, 43],
[85, 92, 27]],

[[36, 51, 87],
[64, 83, 24],
[ 0, 30, 14],
[22, 34, 8],
[80, 49, 78]],

[[34, 44, 19],
[17, 20, 45],
[29, 71, 4],
[36, 85, 70],
[20, 9, 35]]])

image-20220704162631249

切片

一维与列表完全一致 多维时同理

1
2
3
4
5
6
n = np.random.randint(0,100, size=(4,6))
n
array([[49, 27, 77, 39, 6, 33],
[74, 48, 88, 45, 49, 96],
[41, 71, 81, 50, 28, 9],
[69, 97, 81, 85, 70, 54]])

image-20220704165159077

变形

1
2
3
4
5
n
array([[49, 27, 77, 39, 6, 33],
[74, 48, 88, 45, 49, 96],
[41, 71, 81, 50, 28, 9],
[69, 97, 81, 85, 70, 54]])

image-20220704165505336

级联

np.concatenate() 级联需要注意的点:

  • 级联的参数是列表:一定要加中括号或小括号
  • 维度必须相同
  • 形状相符
  • 【重点】级联的方向默认是shape这个tuple的第一个值所代表的维度方向
  • 可通过axis参数改变级联的方向

垂直级联

image-20220704170008231

水平级联

image-20220704170030568

np.hstacknp.vstack : 水平级联与垂直级联,处理自己,进行维度的变更

image-20220704170308261

级联的基本要求: 垂直级联的时候 ,列数一定要相同, 水平级联行数一定要相同.

切分

与级联类似,三个函数完成切分工作:

  • np.split
  • np.vsplit
  • np.hsplit
1
2
3
4
5
6
7
8
9

n = np.random.randint(0,100, size=(6,6))
n
array([[83, 81, 77, 2, 20, 69],
[49, 94, 5, 91, 71, 65],
[12, 10, 10, 25, 53, 90],
[81, 45, 89, 64, 85, 45],
[63, 35, 52, 90, 49, 82],
[16, 67, 24, 86, 91, 42]])

image-20220704172931919

image-20220704173034060

ndarray的聚合操作

求和np.sum

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import numpy as np
n = np.eye(3)
n
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])

n.sum() # axis=None 表示所有的维度都聚合成0维
3.0

# axis=0 表示对行进行聚合操作,行没了,剩下列.
n.sum(axis=0)
array([1., 1., 1.])

# axis=1表示对列进行聚合,列没了,行还在.
n.sum(axis=1)
array([1., 1., 1.])

最大最小值:np.max/ np.min

1
2
3
4
5
6
7
# 同理

n.max(axis=0)
array([1., 1., 1.])

n.min(axis=1)
array([0., 0., 0.])