-
一、简介
-
二、安装
-
三、数组的创建
-
-
3.1 array创建
-
3.2 arange创建
-
3.3 随机数创建数组
-
-
3.3.1 创建随机小数
-
3.3.2 创建随机整数
-
3.3.3 创建标准正态分布数组
-
3.3.4 创建指定期望与方差的正态分布数组
-
-
四、ndarray对象的属性
-
五、其他形式创建数组
-
-
5.1 zeros创建数组
-
5.2 ones创建数组
-
5.3 empty创建数组
-
5.4 linspace创建等差数组
-
5.5 logspace创建等比数组
-
-
六、数组的切片和索引
-
-
6.1 一维数组的切片和索引
-
6.2 二维数组的切片和索引
-
-
七、数组的复制
-
八、改变数组的维度
-
-
8.1 一维数组修改为多维数组
-
-
8.1.1 reshap方法
-
8.1.2 np.reshape()方法
-
-
8.2 多维数组修改为一维数组
-
-
8.2.1 reshape()方法
-
8.2.2 reshape(-1)方法
-
8.2.3 ravel()函数方法
-
8.2.4 flatten()函数方法
-
-
-
九、数组的拼接
-
-
9.1 水平数组拼接
-
9.2 垂直数组拼接
-
9.3 concatenate数组拼接
-
-
十、数组的分割
-
-
10.1 split()方法分割
-
-
10.1.1 一维数组分割
-
10.1.2 二维数组分割
-
-
10.2 hsplite()方法水平分割
-
10.3 vsplite()方法垂直分割
-
-
十一、数组的转置
-
十二、函数
-
-
12.1算数函数
-
-
12.1.1 加法运算
-
12.1.2 减法运算
-
12.1.3 乘法运算
-
12.1.4 除法运算
-
12.1.5 out参数的使用
-
12.1.6 sin()函数使用
-
12.1.7 四舍五入
-
12.1.8 向上取值函数
-
12.1.9 向下取值函数
-
-
12.2聚合函数
-
-
12.2.1 部分聚合函数
-
12.2.2 部分聚合函数示例
-
-
12.2.2.1 power()函数示例
-
12.2.2.2 median()函数示例
-
12.2.2.3 mean()函数示例
-
-
-
-
一、简介
Numpy是科学计算基础库,提高大量科学计算的功能,比如数据统计,随机数生成等,其提供最核心类型为多维数组类型(ndarray),支持大量的维度数组与矩阵运算,Numpy支持向量处理ndarry对象,提高程序的运算速度。
二、安装
-
pip install numpy
三、数组的创建
3.1 array创建
array函数参数为:array(p_object, dtype=None, copy=True, order=‘K’, subok=False, ndmin=0) ,下面只介绍常用参数类型。 1、p_object:数组对象 2、dtype:指定转换的数据中的数据类型 3、ndmin:指定维度数
-
一维数组创建 一维数组的创建只需传入一个列表即可,输出的类型为numpy.ndarray类型 示例代码:
import numpy as np
# 使用array函数创建一维数组
x = np.array([1, 2, 3, 4])
print(x) # [1 2 3 4]
print(type(x)) # <class 'numpy.ndarray'>
-
二维数组创建 同一维数组一样,同样传入列表即可。 注意:传入的列表最外层还有一层中括号,如果不加则会报错。 示例代码:
import numpy as np
# 使用array函数创建二维数组
y = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # 创建二维数组
print(y)
输出结果:
[[1 2 3]
[4 5 6]
[7 8 9]]
print(type(x)) # <class 'numpy.ndarray'>
-
三维数组创建 三位数组同二维一样,只不过在最外层又加上了一层中括号。 示例代码:
import numpy as np
# 使用array函数创建三维数组
z = np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]) # 创建三维数组
print(z)
输出结果:
[[[1 2 3]
[4 5 6]
[7 8 9]]]
-
数组中dtype()的使用。 dtype可以将创建的数组转换为其他类型。 注意:转换的只是列表中的数据类型,并不是numpy.ndarray类型。 示例代码:
import numpy as np
# dtype的使用,转换类型
w = np.array([1, 2, 3], dtype=float) # 将列表中的数据转换为浮点型,还可以转换为字符串类型
print(w) #[1. 2. 3.],列表中的数据转换为浮点型
print(type(w)) #<class 'numpy.ndarray'>,返回的类型仍然是ndarray类型
-
ndmin的使用,指定维度数 ndmin可以指定数组的维度数,单个列表也可以创建出三位数组。但指定的高维数组转换不了低维数组。 示例代码:一维数组通过ndmin转换为三维数组
import numpy as np
# ndmin的使用,指定维度数
a = np.array([1, 2, 3], dtype=float, ndmin=3)
print(a) # [[[1. 2. 3.]]]转换为三维数据
print(type(a)) # <class 'numpy.ndarray'>
示例代码:三位数组使用ndmin转换不了低维数组
# ndmin的使用,指定维度数
a = np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]]], dtype=float, ndmin=1) # 指定的数组为三维,ndmin为1,输出结果还是三维
print(a)
输出结果:
[[[1. 2. 3.]
[4. 5. 6.]
[7. 8. 9.]]]
3.2 arange创建
arange函数的参数:arange([start,] stop[, step,], dtype=None) 1、start:起始值 2、stop:终止值 3、step:步长,默认为1 4、dtype:转换类型,如果不指定,则会使用数据类型 示例代码:
import numpy as np
# 创建1-10的数组
a = np.arange(0, 10, dtype=int)
print(a)
输出结果:
[0 1 2 3 4 5 6 7 8 9]
设置步长代码:
import numpy as np
# 创建1-10的数组
a = np.arange(0, 10, 2, dtype=int) # 设置步长为2的数组
print(a)
输出结果:
[0 2 4 6 8]
3.3 随机数创建数组
3.3.1 创建随机小数
随机数创建数组采用的是numpy库里面的random()函数。 语法:np.random.random(size=None) 返回的是0.0-1.0之间的随机数,但不包括1.0,size指定随机数的个数。 创建一维数组示例代码:
import numpy as np # 生成0-1之间的随机数组,共10个数据 a = np.random.random(10) print(a) 输出结果: [0.10646094 0.55262119 0.1376072 0.09820666 0.25679125 0.75362808 0.92731829 0.7927795 0.69050268 0.1210358 ]
创建二维数组时传入的size参数,指定的是数组的行和列 创建二维数组示例代码:
import numpy as np # 创建二维随机数组 b = np.random.random(size=(2, 3)) # 创建2行3列的随机数组 print(b) 输出结果: [[0.37721388 0.37544322 0.21520047] [0.53715603 0.30601605 0.06256915]]
创建三维数组时传入的size参数为三个。 创建三位数组示例代码:
import numpy as np # 创建三维随机数组 c = np.random.random(size=(2, 3, 4)) # 创建2个三行四列的数组 print(c) 输出结果: [[[0.36208984 0.81947471 0.14298442 0.79046203] [0.54460578 0.24994409 0.46474527 0.32052213] [0.17897466 0.82689056 0.90160973 0.45336997]] [[0.45532895 0.8677309 0.09609607 0.51677404] [0.57373632 0.27907846 0.70424555 0.84253691] [0.57790128 0.20907129 0.49335608 0.87549777]]]
3.3.2 创建随机整数
创建随机整数使用的是randint()函数,参数为:randint(low, high=None, size=None, dtype=‘l’) low:最小值 high:最大值 size:指定维度数,一维、二维、三维。 dtype:指定数组类型 创建一维随机整数示例代码:
import numpy as np # 生成一维0-5之间的10个随机整数 a = np.random.randint(0, 5, size=10) # size为10 ,表示创建一维10个随机整数。 print(a) 输出结果: [4 1 0 1 2 0 3 1 0 2]
创建二维数组时size的参数为一个元组,指定行和列 创建二维随机整数示例代码:
import numpy as np # 创建二维2-8之间的随机数组 b = np.random.randint(2, 8, size=(2, 3)) print(b) 输出结果: [[3 6 3] [3 4 4]]
创建三维随机整数示例代码:
import numpy as np # 创建三维2-8之间的随机数组 c = np.random.randint(2, 8, size=(2, 3, 4)) # 创建三维数组 print(c) 输出结果: [[[6 5 6 4] [2 3 4 5] [5 5 7 5]] [[5 2 2 5] [3 4 5 5] [3 3 6 3]]]
3.3.3 创建标准正态分布数组
创建标准正态分布使用的时randn()方法。 randn(d0, d1, …, dn),返回一个或一组样本,具有标准正态分布(期望为0,均值为1) dn表示维度数 创建一维标准正态分布示例代码:
import numpy as np # 创建一维标准正态分布 a = np.random.randn(6) print(a) 输出结果: [ 0.58850012 -0.88685655 -1.00077417 2.50602404 -0.69627562 1.19382079]
创建二维标准正态分布:
import numpy as np # 创建一维标准正态分布 b = np.random.randn(2, 3) # 指定行和列 print(b) 输出结果: [[-1.59507936 2.00741908 -0.19886889] [-0.78522123 -0.94702049 0.10118063]]
创建三维标准正态分布:
import numpy as np # 创建一维标准正态分布 c = np.random.randn(2, 3, 4) print(c) 输出结果: [[[ 2.25392453 -1.03092967 0.62695321 -1.59550922] [ 0.21379353 -1.14740262 1.39019012 0.01449549] [ 1.29115361 0.01583029 -1.53528833 -1.65218213]] [[ 1.36693468 1.27192511 -0.36759254 -0.67529018] [ 0.12840871 -0.40780793 0.3604168 0.88594743] [ 0.57778304 -2.42864619 -0.5829699 -0.29083045]]]
3.3.4 创建指定期望与方差的正态分布数组
创建指定期望与方差时的数组,采用的是normal()函数。 参数为:normal(loc=0.0, scale=1.0, size=None) loc:指定期望 scale:指定方差 size:指定维度 创建默认期望与方差的一维数组示例代码:
import numpy as np # 创建默认期望与均值的一维数组 a = np.random.normal(size=5) # 默认期望loc=0.0,方差scale=1.0 print(a) 输出结果: [ 0.68913158 -0.24866231 0.93683785 -0.33245719 1.56009623]
创建指定期望与方差的二维数组示例代码:
import numpy as np # 创建默认期望与均值的一维数组 a = np.random.normal(loc=2, scale=3, size=(2, 3)) # 指定期望为2,方差为3的二维数组 print(a) 输出结果: [[ 6.13038568 4.63502362 1.5378486 ] [ 0.47091329 2.1003756 -0.93129833]]
四、ndarray对象的属性
Numpy最重要的一个特点是其N维数组对象ndarray,它是一系列同类型数据的集合,以0为下标开始进行集合中元素的索引。 ndarray对象是用于存放同类型元素的多维数组,ndarray中的每个元素在内存中都有相同存储的大小的区域。 ndarray内部由以下内容组成:
-
一个指向数据(内存或内存映射文件中的一块数据)的指针。
-
数据类型或dtype,描述在数组中固定大小值的格子。
-
一个表示数组形状(shape)的元组,表示各维度大小的元组。
五、其他形式创建数组
5.1 zeros创建数组
zeros创建语法:zeros(shape, dtype=float, order=‘C’) Return a new array of given shape and type, filled with zeros.返回一个数组,给定形状和类型,以0填充。 示例代码: zreos创建一维数组
import numpy as np # zeros函数创建一维列表 a = np.zeros(5) # 默认数据为浮点型,可指定数据类型 print(a) 输出结果: [0. 0. 0. 0. 0.]
zreos创建二维数组
import numpy as np # zeros函数创建二维列表 b = np.zeros((2, 3), dtype=int) # 默认数据为浮点型,可指定数据类型 print(b) 输出结果: [[0 0 0] [0 0 0]]
5.2 ones创建数组
ones参数:ones(shape, dtype=None, order=‘C’) Return a new array of given shape and type, filled with ones.返回一个数组,给定形状和类型,以1填充。 ones绘制一维数组示例代码:
import numpy as np c = np.ones(5, dtype=int) # 默认数据为浮点型,可指定数据类型 print(c) 输出结果: [1 1 1 1 1]
ones绘制二维数组示例代码:
import numpy as np # ones绘制二维数组 d = np.ones((3, 4), dtype=int) # 默认数据为浮点型,可指定数据类型 print(d) 输出结果: [[1 1 1 1] [1 1 1 1] [1 1 1 1]]
5.3 empty创建数组
empty()方法用来创建一个指定形状(shape)、数据类型(dtype)且为初始化的数组,里面的元素的值是之前内存的值。 empty语法:empty(shape, dtype=float, order=‘C’) order有C和F两个选项,分别代表行优先和列优先,在计算机内存中的存储的顺序。 empty创建一维数组实例代码:
import numpy as np # empty创建一维数组 e = np.empty(5) print(e) 输出结果: [8.73990362e+245 3.20929408e-220 1.63354242e-301 1.39249620e-309 4.55718212e-303]
empty创建二维数组实例代码:
import numpy as np # empty创建二维数组 f = np.empty((2, 3), dtype=float) print(f) 输出结果: [[6.23042070e-307 3.56043053e-307 7.56595733e-307] [8.45590539e-307 6.89807188e-307 9.34604358e-307]]
5.4 linspace创建等差数组
linspace函数用于创建一个一维数组,数组是一个等差数列构成的。 linspace语法:linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None): start:序列的起始值 stop:序列的终止值,如果endpoint=True则包含终止值 num:要生成的等步长样本的数量,默认为50 endpoint:默认为True,为True时生成的数组包含终止值 restep:如果为True,生成的数组中会显示间距,反之不显示 dtype:ndarray的数据类型 linspace创建一维数组示例代码:
import numpy as np # linspsce创建一维数组 g = np.linspace(1, 10, 12) # 起始值为1,终止值为10,生成12个数据的等差数列数组 print(g) 输出结果: [ 1. 1.81818182 2.63636364 3.45454545 4.27272727 5.09090909 5.90909091 6.72727273 7.54545455 8.36363636 9.18181818 10. ]
5.5 logspace创建等比数组
logspace语法:logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None): base参数为对数log的底数,其他参数同上。 logspace创建一维数组示例代码:
import numpy as np # logspac创建等比一维数组 h = np.logspace(1, 10, 10, base=2) print(h) 输出结果: [ 2. 4. 8. 16. 32. 64. 128. 256. 512. 1024.]
六、数组的切片和索引
ndarray对象的内容可以通过索引或切片来访问和修改,与python中list的切片操作一样。 ndarray数组可以基于0-n的下标进行索引,并设置start,stop,step参数进行,从原数组中切割出一个新的数组。
6.1 一维数组的切片和索引
-
索引访问
import numpy as np a = np.arange(10) # 生成一维数组 # 正序索引访问 print(a[0], a[2]) 输出结果0 2 # 负序索引访问 print(a[-1], a[-3]) 输出结果9 7
-
切片操作
import numpy as np a = np.arange(10) # 生成一维数组 # 切片操作 print(a[1:-1:2]) #切取第二个元素到最后一个元素且步长为2 输出结果: [1 3 5 7]
6.2 二维数组的切片和索引
-
索引访问,访问行数据 版本一:
import numpy as np b = np.array([[1, 2, 3], [4, 5, 6]]) # 创建一个二维数组 print(b[0]) # 访问数组第0行 输出结果: [1 2 3]
版本二:
import numpy as np b = np.arange(1, 13) # 创建一个一维数组 c = b.reshape((4, 3)) # 将一维数组转换为二维数组 print(c[0]) # 索引第一行 输出结果: [1 2 3]
索引具体值:
import numpy as np b = np.arange(1, 13) # 创建一个一维数组 c = b.reshape((4, 3)) # 将一维数组转换为二维数组 print(c[0][2]) # 索引第一行 输出结果: 3
-
切片操作 切片的使用:[行进行切片,列进行切片] : [start:stop:step,start:stop:step]
切取所有行所有列示例代码:**
import numpy as np b = np.arange(1, 13) # 创建一个一维数组 c = b.reshape((4, 3)) # 将一维数组转换为二维数组 print(c[:, :]) # 切取所有行所有列 输出结果: [[ 1 2 3] [ 4 5 6] [ 7 8 9] [10 11 12]]
切取所有行部分列示例代码:
import numpy as np b = np.arange(1, 13) # 创建一个一维数组 c = b.reshape((4, 3)) # 将一维数组转换为二维数组 print(c[:, 1]) # 切取所有行第二列 输出结果: [ 2 5 8 11]
切取部分行所有列示例代码:
b = np.arange(1, 13) # 创建一个一维数组 c = b.reshape((4, 3)) # 将一维数组转换为二维数组 print(c[0:2, :]) # 切取前两行所有列 输出结果: [[1 2 3] [4 5 6]]
切取部分行部分列示例代码:
import numpy as np b = np.arange(1, 13) # 创建一个一维数组 c = b.reshape((4, 3)) # 将一维数组转换为二维数组 print(c[0:2, 0:2]) # 切取两行两列 输出结果: [[1 2] [4 5]]
坐标获取具体值:[行,列] 坐标获取单一值
import numpy as np b = np.arange(1, 13) # 创建一个一维数组 c = b.reshape((4, 3)) # 将一维数组转换为二维数组 # 坐标获取 print(c[1, 2]) 输出结果: 6
坐标同时获取不同行不同列
import numpy as np b = np.arange(1, 13) # 创建一个一维数组 c = b.reshape((4, 3)) # 将一维数组转换为二维数组 # 使用坐标获取多值 print(c[(1, 2), (2, 0)]) # 获取第二行第三列,第三行第一列 输出结果: [6 7]
负索引的使用
import numpy as np b = np.arange(1, 13) # 创建一个一维数组 c = b.reshape((4, 3)) # 将一维数组转换为二维数组 # 负索引的使用 print(c[-1]) 输出结果: [10 11 12]
行倒序
import numpy as np b = np.arange(1, 13) # 创建一个一维数组 c = b.reshape((4, 3)) # 将一维数组转换为二维数组 # 行倒序 print(c[::-1]) 输出结果: [[10 11 12] [ 7 8 9] [ 4 5 6] [ 1 2 3]]
行列倒序
import numpy as np b = np.arange(1, 13) # 创建一个一维数组 c = b.reshape((4, 3)) # 将一维数组转换为二维数组 # 行列倒序 print(c[::-1,::-1]) 输出结果: [[12 11 10] [ 9 8 7] [ 6 5 4] [ 3 2 1]]
七、数组的复制
通过切片可以获取到新数组,即使赋值给新的变量,但还是原来数组的视图,如果对切片数组中的元素进行修改,则原数组也会改变。如果要复制数组则使用numpy中的copy方法即可 未使用copy方法的示例代码:
import numpy as np a = np.arange(1, 13).reshape((3, 4)) # 对a数组进行切片处理,获取第一二行,第一二列 sub_a = a[:2, :2] print(sub_a) 输出结果: [[1 2] [5 6]] # 对sub_a中的第一行第一列的值进行修改 sub_a[0][0] = 100 # 修改第一行第一列元素的值 print(sub_a) 输出结果: [[100 2] [ 5 6]] print(a) # 改变切片数组的值,则原数组的数据也发生改变 输出结果: [[100 2 3 4] [ 5 6 7 8] [ 9 10 11 12]]
使用copy方法的示例代码:
import numpy as np a = np.arange(1, 13).reshape((3, 4)) #创建一个二维数组 sub_b = np.copy(a[:2, :2]) #切取第一二行,第一二列,同时复制给sub_b sub_b[0][0] = 200 # 修改第一行第一列元素的值 print(sub_b) 输出结果: [[200 2] [ 5 6]] print(a) # 修改切片数组的值,不改变原数组的值 输出结果: [[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]]
八、改变数组的维度
处理数组的一项重要的工作就是改变数组的维度,包含提高数组的维度和降低数组的维度,还包括数组的转置,Numpy提高的大量API可以很轻松地完成这些数组的操作,下面介绍改变数组维度的方法。
注意:修改数组时,不管修改到几维,数据的个数应始终相等,否则报错 示例:
import numpy as np # arange方法创建一个一维数组 a = np.arange(24) # reshape()方法改变数组的维度,装换为二维,传入元组也可 b = a.reshape(4, 6) #修改为(2,12),(12,2),(3,8),(8,3)也可,当个数必须相等。
上述代码中将一维数组修改维二维数组,修改的数组的数据数4*6等于24,与一维数组中的个数相等。三维数组同理。
8.1 一维数组修改为多维数组
8.1.1 reshap方法
一维数组转换为二维数组 示例代码:
import numpy as np # # reshape()方法改变数组的维度,装换为二维,传入元组也可 a = np.arange(24) # reshape()方法改变数组的维度 b = a.reshape(4, 6) print(b) 输出结果: [[ 0 1 2 3 4 5] [ 6 7 8 9 10 11] [12 13 14 15 16 17] [18 19 20 21 22 23]]
一维数组转换为三维数组 示例代码:
import numpy as np # arange方法创建一个一维数组 a = np.arange(24) # # 将一维转换为三维数组 c = a.reshape(2, 3, 4) print(c) 输出结果: [[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [[12 13 14 15] [16 17 18 19] [20 21 22 23]]]
8.1.2 np.reshape()方法
语法:reshape(a, newshape, order=‘C’): a:表示要修改的数组 newshape:x修改后的数组形状,传入元组类型 一维数组转换为二维数组示例代码:
import numpy as np # arange方法创建一个一维数组 a = np.arange(24) # np.reshape()方法 d = np.reshape(a, (4, 6)) print(d) 输出结果: [[ 0 1 2 3 4 5] [ 6 7 8 9 10 11] [12 13 14 15 16 17] [18 19 20 21 22 23]]
一维数组转换为三维数组示例代码:
import numpy as np # arange方法创建一个一维数组 a = np.arange(24) # np.reshape()方法转换为三维数组 e = np.reshape(a, (2, 3, 4)) print(e) 输出结果: [[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [[12 13 14 15] [16 17 18 19] [20 21 22 23]]]
8.2 多维数组修改为一维数组
8.2.1 reshape()方法
reshape方法不仅可以将一维数组转换为多维数组,还可以将多维数组转换为一维数组。 reshape二维数组修改为一维数组示例代码:
import numpy as np # arange方法创建一个一维数组 a = np.arange(24) # reshape()方法改变数组的维度,装换为二维,传入元组也可 b = a.reshape(4, 6) # reshape()方法将二维数组修改为一维数组 f = b.reshape(24) print(f) 输出结果: [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
reshape三维数组修改为一维数组示例代码:
import numpy as np # arange方法创建一个一维数组 a = np.arange(24) # np.reshape()方法转换为三维数组 e = np.reshape(a, (2, 3, 4)) # reshape()方法将三维数组修改为一维数组 g = e.reshape(24) print(g) 输出结果: [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
8.2.2 reshape(-1)方法
调用该方法后,不管几维数组,都转化为一维数组。 示例代码:
import numpy as np # arange方法创建一个一维数组 a = np.arange(24) # np.reshape()方法转换为三维数组 e = np.reshape(a, (2, 3, 4)) # reshape()方法将三维数组修改为一维数组 g = e.reshape(-1) print(g) 输出结果: [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
8.2.3 ravel()函数方法
使用该函数时,直接调用即可,这里举例三维数组修改为一维数组,二维同理。 示例代码:
import numpy as np # arange方法创建一个一维数组 a = np.arange(24) # 将一维转换为三维数组 c = a.reshape(2, 3, 4) # ravel函数修改 h = c.ravel() print(h) 输出结果: [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
8.2.4 flatten()函数方法
使用该函数时,直接调用即可,这里举例三维数组修改为一维数组,二维同理。 示例代码:
import numpy as np # arange方法创建一个一维数组 a = np.arange(24) # 将一维转换为三维数组 c = a.reshape(2, 3, 4) # flatten()函数修改 i = c.flatten() print(i) 输出结果: [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
九、数组的拼接
9.1 水平数组拼接
通过hstack函数可以将两个或多个数组水平组合起来形成一个新的数组。 语法:hstack(tup): hstack函数接收的参数为元组或者列表形式 示例代码:
import numpy as np # 创建两个二维数组 a = np.array([[1, 2, 3], [4, 5, 6]]) b = np.array([[10, 20, 30], [40, 50, 60]]) # 调用hstack函数,参数为元组类型或着列表类型都可 c = np.hstack((a, b)) print(c) 输出结果: [[ 1 2 3 10 20 30] [ 4 5 6 40 50 60]]
9.2 垂直数组拼接
通过vstack函数可以将两个或多个数组垂直组合起来形成一个新的数组。 语法: vstack(tup): vstack函数接收的参数为元组或是列表形式 示例代码:
import numpy as np # 创建两个二维数组 a = np.array([[1, 2, 3], [4, 5, 6]]) b = np.array([[10, 20, 30], [40, 50, 60]]) # 调用vstack函数,实现垂直方向拼接,参数为元组或是列表 d = np.vstack([a, b]) print(d) 输出结果: [[ 1 2 3] [ 4 5 6] [10 20 30] [40 50 60]]
9.3 concatenate数组拼接
语法:concatenate(arrays, axis=None, out=None) arrays:表示要拼接的数组 axis:拼接轴,默认为0。二维数组时有两个轴,0代表x轴,1代表y轴。三维数组有三个轴2代表z轴。 out:ndarray,可选,如果提供,则指定放置结果的目的地。形状必须是正确,如果不匹配,则匹配concatenate返回的值指定了out参数。 axis采用默认值也就是0时的示例代码:
import numpy as np # 创建两个二维数组 a = np.array([[1, 2, 3], [4, 5, 6]]) b = np.array([[10, 20, 30], [40, 50, 60]]) # concatenate的使用,axis默认为0时,相当于vstack e = np.concatenate((a, b), axis=0) print(e) 输出结果: [[ 1 2 3] [ 4 5 6] [10 20 30] [40 50 60]] # 当axis=0时,shape为x轴的数据的叠加,2+2=4 (4, 3)
axis=1时的示例代码:
import numpy as np # 创建两个二维数组 a = np.array([[1, 2, 3], [4, 5, 6]]) b = np.array([[10, 20, 30], [40, 50, 60]]) # concatenate的使用,axis默认为1时,相当于hstack e = np.concatenate((a, b), axis=1) print(e, e.shape) 输出结果: [[ 1 2 3 10 20 30] [ 4 5 6 40 50 60]] # 当axis=1时,shape为y轴的数据的叠加,3+3=6 (2, 6)
三维数组axis=2的拼接
import numpy as np # 创建两个三维数组 f = np.arange(1, 13).reshape(1, 2, 6) g = np.arange(13, 25).reshape(1, 2, 6) # concatenate的使用,axis=2时 h = np.concatenate((f, g), axis=2) print(h, h.shape) 输出结果: [[[ 1 2 3 4 5 6 13 14 15 16 17 18] [ 7 8 9 10 11 12 19 20 21 22 23 24]]] # 当axis=2时,shape为z轴的数据的叠加,6+6=12 (1, 2, 12)
十、数组的分割
10.1 split()方法分割
语法:split(ary, indices_or_sections, axis=0) ary:被分割的数组 indices_or_sections:如果是一个整数,就用该数平均切分,如果是一个数组,为沿轴切分的位置。 axis:沿着哪个维度进行切向,默认为0,横向切分。为1时,纵向切分。
10.1.1 一维数组分割
平均分割示例代码:
import numpy as np # split分割一维数组 x = np.arange(1, 9) # 传递整数,采用平均分割 a = np.split(x, 4) print(a) 输出结果: [array([1, 2]), array([3, 4]), array([5, 6]), array([7, 8])]
传递数组,按位置分割:
import numpy as np # split分割一维数组 x = np.arange(1, 9) # 传递数组,按位置分割 b = np.split(x, [3, 5]) #(1,2,3)为一组,(3,4)为一组,(6,7,8)为一组 print(b) 输出结果: [array([1, 2, 3]), array([4, 5]), array([6, 7, 8])]
10.1.2 二维数组分割
注意:使用平均分割时,数组必须能够平均分割,如果不能平均分割,则会报错。 平均分割垂直方向示例代码:
import numpy as np # 创建一个二维数组 x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) # 将x按垂直方向平均分割为两份,且分别接收 a, b = np.split(x, 2, axis=0) print(a) print(b) 输出结果: [[1 2 3] [4 5 6]] [[ 7 8 9] [10 11 12]]
平均分割水平方向示例代码:
import numpy as np x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) # 将x按水平方向平均分割为三份,且分别接收 a, b, c = np.split(x, 3, axis=1) print(a, b, c) 输出结果: [[ 1] [ 4] [ 7] [10]] [[ 2] [ 5] [ 8] [11]] [[ 3] [ 6] [ 9] [12]]
传递数组,垂直方向,按位置分割:
import numpy as np x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) # 将x按垂直方向按位置分割为三份,且分别接收 c, d, f = np.split(x, [1, 2], axis=0) print(c) print(d) print(f) 输出结果: [[1 2 3]] [[4 5 6]] [[ 7 8 9] [10 11 12]]
传递数组,水平方向,按位置分割:
import numpy as np x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) # 将x按水平方向按位置分割为三份,且分别接收 g, h, i = np.split(x, [1, 2], axis=1) print(g) print(h) print(i) 输出结果: [[ 1] [ 4] [ 7] [10]] [[ 2] [ 5] [ 8] [11]] [[ 3] [ 6] [ 9] [12]]
10.2 hsplite()方法水平分割
使用hsplit函数可以水平分割数组,该函数有两个参数,第一个参数为待分割的数组,第二个参数表示要将数组水平分割成几个小组。 注意:使用平均分割时,数组必须能够平均分割,如果不能平均分割,则会报错。 hsplit()水平方向平均分割示例代码
import numpy as np x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) # 使用hsplit()水平方向分割 a, b, c = np.hsplit(x, 3) print(a) print(b) print(c) 输出结果: [[ 1] [ 4] [ 7] [10]] [[ 2] [ 5] [ 8] [11]] [[ 3] [ 6] [ 9] [12]]
hsplit()水平方向按位置分割示例代码
import numpy as np x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) # 使用hsplit()水平方向位置分割 a, b, c = np.hsplit(x, [1, 2]) print(a) print(b) print(c) 输出结果: [[ 1] [ 4] [ 7] [10]] [[ 2] [ 5] [ 8] [11]] [[ 3] [ 6] [ 9] [12]]
10.3 vsplite()方法垂直分割
注意:使用平均分割时,数组必须能够平均分割,如果不能平均分割,则会报错。 vsplit()垂直方向平均分割示例代码
import numpy as np x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) # 使用vsplit()垂直方向平均分割 a, b = np.vsplit(x, 2) print(a) print(b) 输出结果: [[1 2 3] [4 5 6]] [[ 7 8 9] [10 11 12]]
vsplit()垂直方向按位置分割示例代码
import numpy as np x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) # 使用vsplit()垂直方向按位置分割 a, b, c = np.vsplit(x, [2, 3]) print(a) print(b) print(c) 输出结果: [[1 2 3] [4 5 6]] [[7 8 9]] [[10 11 12]]
十一、数组的转置
调用transpose()方法可以转置我们的数组。 语法:transpose(a, axes=None) a:要转置的数组 axes:对于多维数组转置时需要传递这个参数,参数为想要成的维度元组
二维数组的转置具体代码如下:
import numpy as np # 创建一个二维数组 x = np.arange(1, 13).reshape((2, 6)) print(x) 输出结果: [[ 1 2 3 4 5 6] [ 7 8 9 10 11 12]] a = np.transpose(x) print(a) 输出结果: [[ 1 7] [ 2 8] [ 3 9] [ 4 10] [ 5 11] [ 6 12]]
二维数组.T方法转置数组 具体代码如下:
import numpy as np # 创建一个二维数组 x = np.arange(1, 13).reshape((2, 6)) print(x) 输出结果: [[ 1 2 3 4 5 6] [ 7 8 9 10 11 12]] # .T方法转置数组 b = x.T print(b) 输出结果: [[ 1 7] [ 2 8] [ 3 9] [ 4 10] [ 5 11] [ 6 12]]
三维数组的转置 对于三维数组x[ i ],[ j ] ,[ k ] 进行转置,默认的将 i 和 k 进行交换 示例代码:
import numpy as np # 创建一个三维数组 x = np.arange(1, 25).reshape((2, 3, 4)) print(x) 输出结果: [[[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]] [[13 14 15 16] [17 18 19 20] [21 22 23 24]]] # 三维数组的转置 c = np.transpose(x) print(c) 输出结果: [[[ 1 13] [ 5 17] [ 9 21]] [[ 2 14] [ 6 18] [10 22]] [[ 3 15] [ 7 19] [11 23]] [[ 4 16] [ 8 20] [12 24]]]
指定转置后维度
import numpy as np # 创建一个三维数组 x = np.arange(1, 25).reshape((2, 3, 4)) print(x) 输出结果: [[[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]] [[13 14 15 16] [17 18 19 20] [21 22 23 24]]] # 三维数组的转置 c = np.transpose(x, (1, 0, 2)) # (1,0,2)表示想要转置数组的维度,本例为三个两行四列的数组 print(c) 输出结果: [[[ 1 2 3 4] [13 14 15 16]] [[ 5 6 7 8] [17 18 19 20]] [[ 9 10 11 12] [21 22 23 24]]]
十二、函数
如果参与运算的两个对象,都是ndarray,并且形状相同,那么会对位彼此之间进行加减乘除运算。Numpy算数函数包含简单的加减乘除:add(),subtract(),multiply()和divide()。
12.1算数函数
12.1.1 加法运算
一维数组运算时,会发生广播从而完成运算 示例代码:
import numpy as np # 创建两个数组 a = np.arange(9, dtype=float).reshape(3, 3) b = np.array([10, 10, 10]) # 加法运算,以下两种方法均可,输出的结果相同 print(np.add(a, b)) print(a + b) 输出结果: [[10. 11. 12.] [13. 14. 15.] [16. 17. 18.]]
12.1.2 减法运算
import numpy as np a = np.arange(9, dtype=float).reshape(3, 3) b = np.array([10, 10, 10]) # 减法运算,以下两种方法均可,输出的结果相同 print(np.subtract(a, b)) print(a-b) 输出结果: [[-10. -9. -8.] [ -7. -6. -5.] [ -4. -3. -2.]]
12.1.3 乘法运算
import numpy as np a = np.arange(9, dtype=float).reshape(3, 3) b = np.array([10, 10, 10]) # 乘法运算,以下两种方法均可,输出的结果相同 print(np.multiply(a, b)) print(a*b) 输出结果: [[ 0. 10. 20.] [30. 40. 50.] [60. 70. 80.]]
12.1.4 除法运算
import numpy as np a = np.arange(9, dtype=float).reshape(3, 3) b = np.array([10, 10, 10]) # 除法运算,以下两种方法均可,输出的结果相同 print(np.divide(a, b)) print(a/b) 输出结果: [[0. 0.1 0.2] [0.3 0.4 0.5] [0.6 0.7 0.8]]
12.1.5 out参数的使用
本示例用乘法示例,其他运算同理。
import numpy as np a = np.arange(9, dtype=float).reshape(3, 3) # out参数的使用 c = np.empty((3, 3)) # 创建一个3行3列的空数组 np.multiply(a, 10, out=c) # 将a*10得到的数组传输给c空数组 print(c) 输出结果: [[ 0. 10. 20.] [30. 40. 50.] [60. 70. 80.]]
12.1.6 sin()函数使用
本例以sin函数示例,其他三角函数同理
import numpy as np # sin函数的使用 a = np.array([0, 30, 45, 60, 90]) # 转换为弧度 print(np.sin(a)) 输出结果: [ 0. -0.98803162 0.85090352 -0.30481062 0.89399666]
12.1.7 四舍五入
around()函数提供了四舍五入的方法 语法:numpy.around(a, decimals) a:数组 decimals:舍入的小位数。默认为0,如果为负值,整数将四舍五入到小数点左侧的位置。 示例代码:
import numpy as np # 创建一个数组 a = np.array([10.2, 1.233, 3.55, 6.7845]) # 四舍五入方法 b = np.around(a) print(b) 输出结果: [10. 1. 4. 7.]
12.1.8 向上取值函数
ceil()函数提供了向上取值的方法。用法同四舍五入方法相同。 示例代码:
import numpy as np a = np.array([10.2, 1.233, 3.55, 6.7845]) # 向上取值函数 c = np.ceil(a) print(c) 输出结果: [11. 2. 4. 7.]
12.1.9 向下取值函数
floor()函数提供了向下取值的方法。用法同四舍五入方法相同。 示例代码:
import numpy as np a = np.array([10.2, 1.233, 3.55, 6.7845]) # 向下取值函数 d = np.floor(a) print(d) 输出结果: [10. 1. 3. 6.]
12.2聚合函数
12.2.1 部分聚合函数
Numpy提高了很多的聚合函数,以下为部分聚合函数。
12.2.2 部分聚合函数示例
12.2.2.1 power()函数示例
numpy.power()函数将第一个输入数组中的元素作为底数,计算它与第二个输入数组中相应元素的幂。 power()函数示例:
import numpy as np # 创建一个二维数组 a = np.arange(12).reshape((3, 4)) print(a) 输出结果: [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] # 将数组中的每一个元素运行幂运算 e = np.power(a, 2) print(e) 输出结果: [[ 0 1 4 9] [ 16 25 36 49] [ 64 81 100 121]]
12.2.2.2 median()函数示例
median()函数的使用 一维数组的中位数
import numpy as np # 创建一个一维数组 f = np.array([2, 4, 3, 1, 2]) # 对数组排序,数组中的元素为偶数,中位数值:中间两个数的平均值。如果为奇数,中间的数 # 取中位数 g = np.median(f) print(g) 输出结果: 2.0
二维数组取中位数示例代码 二维数组的中位数,要通过axis指定轴
import numpy as np # 创建一个二维数组。二维数组的中位数,要通过axis指定轴 h = np.arange(1, 13).reshape((3, 4)) print(h) 输出结果: [[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]] i = np.median(h, axis=0) # axis=0表示垂直方向取中位数 j = np.median(h, axis=1) # axis=1表示水平方向取中位数 print(i) 输出结果: [5. 6. 7. 8.] print(j) 输出结果: [ 2.5 6.5 10.5]
12.2.2.3 mean()函数示例
一维数组求平均数
import numpy as np # 创建一个一维数组 f = np.array([2, 4, 3, 1, 2]) # 求一维数组平均数 k = np.mean(f) print(k) 输出结果: 2.4
二维数组求平均数
import numpy as np # 创建一个二维数组。二维数组的平均数,要通过axis指定轴 h = np.arange(1, 13).reshape((3, 4)) print(h) 输出结果: [[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]] # 二维数组求平均数,axis指定轴求平均 l = np.mean(h, axis=0) # 垂直方向求平均数 m = np.mean(h, axis=1) # 水平方向求平均数 print(l) 输出结果: [5. 6. 7. 8.] print(m) [ 2.5 6.5 10.5]