NumPy库

1.定义

NumPy是一个开源python科学计算基础库,是SciPy、Pandas等数据处理或科学计算库的基础。其中包括:

一个强大的N维数组对象:ndarray

广播功能函数

整合C/C++/Fortran代码工具

线性代数、傅里叶变换、随机数生成等功能

2.导入

import numpy as np

3.思考:python已有列表类型,为什么还需要一个数组类型(对象)?

数组类型(对象)可以去掉元素间运算的循环,使一维向量更像单个数据;数组类型(对象)经过优化,可以提升运算速度;由于科学计算中,一个维度的数据类型往往相同,所以数组类型(对象)中元素采用相同的类型有助于节省运算和存储空间。

注意:NumPy底层是由C实现,在进行科学计算时,底层C语言会提供高效与快速的运算功能。

# 例如计算A²+B²,其中A和B都是一维数组
# 使用列表计算
def list1(A, B):
    C = []
    for i in range(len(A)):
        C.append(A[i] ** 2 + B[i] ** 2)
    return C
print(list1([1, 2, 3, 4], [1, 2, 3, 4]))  # [2, 8, 18, 32]

# 使用numpy中数组对象(类型)计算
def fun1(A, B):
    A = np.array(A)
    B = np.array(B)
    C = A ** 2 + B ** 2
    return C
print(fun1([1, 2, 3, 4], [1, 2, 3, 4]))  # [ 2  8 18 32]

4.ndarray:N维数组对象

ndarray是一个多维数组对象,包含两个部分:①实际存储的数据②描述这些实际数据的元数据(数据维度、数据类型等)(需要开辟新的空间存储这些元数据)

ndarray数组一般要求所有元素类型相同(同质)(也有非同质),且下标从0开始(与列表相似) 。

使用方法:nd.array()生成一个ndarray数组,其结果输出成[ ]的形式,其中元素由空格分隔。

a = np.array([[1, 3, 4, 45],
              [2, 5, 6, 32]])
print(a)
# [[ 1  3  4 45]
#  [ 2  5  6 32]]

两个重要概念:①轴(axis):保存数据的维度           ②秩(rank):轴的数量,即数组维度的多少

5:ndarray数组类型(对象)基本属性

属性说明
.ndim秩,即轴的数量或维度的数量
.shapendarray对象的尺度,对于矩阵, n 行 m 列
.sizendarray对象元素的个数,相当于.shape 中n*m的值
.dtypendarray对象的元素类型
.itemsizendarray对象中每个元素的大小,以字节为单位

a = np.array([[1, 3, 4, 45],
              [2, 5, 6, 32]])
print(a.ndim)      # 2
print(a.shape)     # (2,4)
print(a.size)      # 8
print(a.dtype)     # int32
print(a.itemsize)  # 4

6:ndarray数组类型(对象)的元素类型

数据类型说明
bool布尔类型,True 或False
intc与 C语言中的int类型一致,一般是int32 或int64
intp用于索引的整数,与 C语言中ssize_t一致,int32 或int64
int88位长度的整数,取值: [ ‐128, 127]
int1616位长度的整数,取值: [ ‐32768, 32767]
int3232位长度的整数,取值: [ ‐ 231 , 231 ‐1]
int6464位长度的整数,取值: [ ‐ 263 , 263 ‐1]
uint88位无符号整数,取值:[0, 255]
uint1616位无符号整数,取值:[0, 65535]
uint3232位无符号整数,取值:[0, 232 ‐1]
uint6464位无符号整数,取值:[0, 264 ‐1]
float1616位半精度浮点数: 1位符号位, 5位指数,10位尾数((符号)尾数*10指数)
float3232位半精度浮点数: 1位符号位, 8位指数,23位尾数
float6464位半精度浮点数: 1位符号位,11位指数,52位尾数
complex64复数类型,实部和虚部都是32位浮点数(实部(.real) + j虚部(.imag))
complex128复数类型,实部和虚部都是64位浮点数

思考:Python语法仅支持整数、浮点数和复数 3种类型,但ndarray支持多种元素类型,为什么?

科学计算涉及的数据较多,对存储和性能有较高要求;对元素类型精细定义,有助于NumPy合理使用存储空间并优化性能,且有助于程序员对程序规模有合理评估。

非同质的ndarray对象:

a = np.array([[1, 3, 4, 45],
              [2, 5, 6]])
print(a.ndim)      # 1
print(a.shape)     # (2,)
print(a.size)      # 2
print(a.dtype)     # object
print(a.itemsize)  # 8

注意:非同质ndarray对象中元素为对象类型,无法发挥NumPy的优势,应尽量避免使用非同质ndarray对象。

7.创建ndarray数组的几种方法

①从列表、元组等类型创建

函数说明
np.array(list/turple,dtype="int32")dtype可不指定,NumPy依情况关联一个dtype类型
# 从列表类型创建
a = np.array([[1, 3, 4, 45],
              [2, 5, 6, 7]])
print(a)
# [[ 1  3  4 45]
#  [ 2  5  6  7]]

# 从元组类型创建
b = np.array((3, 5, 6))
print(b)
# [3 5 6]

# 从列表和元组混合类型创建
c = np.array([[1, 3, 4, 45],
              (3, 5, 6, 7)])
print(c)
# [[ 1  3  4 45]
#  [ 3  5  6  7]]

②使用NumPy中函数创创建

注意:只有np.ararnge(n)结果dtype默认是int类型,其他函数结果dtype默认是float类型。

函数说明
np.arange(n)类似range()函数,返回ndarray类型,元素从 0 到 n ‐ 1
np.ones(shape)根据shape生成一个全 1数组,shape是元组类型
np.zeros(shape)根据shape生成一个全 0数组,shape是元组类型
np.full(shape,val)根据shape生成一个数组,每个元素值都是val
np.eye(n)创建一个正方的n*n单位矩阵,对角线为 1,其余为 0
np.ones_like(a)根据数组 a的形状生成一个全 1数组
np.zeros_like(a)根据数组 a的形状生成一个全 0数组
np.full_like(a,val)根据数组 a的形状生成一个数组,每个元素值都是val
np.linspace()根据起止数据等间距地填充数据,形成数组
np.concatenate()将两个或多个数组合并成一个新的数组

从字节流(raw bytes)中创建ndarray数组

print(np.frombuffer(b'\x01\x02\x03\x04\x05\x06\x07\x08', dtype=np.uint8))          
# [1 2 3 4 5 6 7 8]
print(np.frombuffer(b'\x01\x02\x03\x04\x05\x06\x07\x08', dtype=np.uint8, count=3))
# [1 2 3]

④从文件中读取特定格式,创建ndarray数组

# 1.txt文件
# 1,2,3,4
# 2,3,4,5

data = np.loadtxt('1.txt', delimiter=',', dtype=int)  # delimiter=','表示以逗号分隔
print(data)

# [[1 2 3 4]
#  [2 3 4 5]]

8.ndarray数组的变换

①维度变换

方法说明
.reshape(shape)不改变数组元素个数(否则报错),返回一个shape形状的数组,原数组不变,其参数可以是元组,也可以不是
.resize(shape)可以改变数组元素,返回一个shape形状的数组,原数组改变,其参数可以是元组,也可以不是
.swapaxes(ax1,ax2)将数组 n个维度中两个维度进行调换
.flatten()对数组进行降维,返回折叠后的一维数组,原数组不变

# ① .reshape()方法
a = np.array([[1, 2, 3, 4],
             [2, 3, 5, 5]])

b = a.reshape((4, 2))   # 或者 b = a.reshape(4, 2)
print(a)
# [[1 2 3 4]
#  [2 3 5 5]]
print(b)
# [[1 2]
#  [3 4]
#  [2 3]
#  [5 5]]

# ② .resize()方法
a = np.array([[1, 2, 3, 4],
             [2, 3, 5, 5]])

b = a.resize((4, 2))   # 或者 b = a.resize(4, 2)
print(a)
# [[1 2]
#  [3 4]
#  [2 3]
#  [5 5]]
print(b)    # None
c = np.array([[1, 2, 3, 4],
             [0, 3, 5, 5]], order="C")    # order="C" 内存布局,行主
c.resize((2, 1))              # 缩减数组
print(c)
# [[1]
#  [2]]
d = np.array([[1, 2, 3, 4],
             [0, 3, 5, 5]], order="F")    # order="C" 内存布局,列主
d.resize(2, 1)               # # 缩减数组
print(d)
# [[1]
#  [0]]
e = np.array([[1, 2, 3, 4],
             [0, 3, 5, 5]])
e.resize((2, 6))               # 扩充数组,扩充用0代替
print(e)
# [[1 2 3 4 0 3]
#  [5 5 0 0 0 0]]

# ③ .swapaxes()方法
f = np.array([[1, 2, 3, 4],
             [2, 3, 5, 5]])
print(f.swapaxes(0, 1))
# [[1 2]
#  [2 3]
#  [3 5]
#  [4 5]]

g = np.array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]])
print(g.swapaxes(0, 2))
# [[[0 4]
#   [2 6]]
#  [[1 5]
#   [3 7]]]]

# ④ .flatten()方法
h = np.array([[1, 2], [3, 4]])
l = h.flatten()
print(h)
# [[1 2]
#  [3 4]]
print(l)
# [1 2 3 4]

②类型变换

方法说明
.astype(new_type )不改变原数组,创建新的数组(拷贝),即使两个类型一致
.tolist()不改变原数组,数组向列表转换
# ① .astype(new_type )方法
cc = np.array([[1, 2, 3, 4], [4, 5, 6, 7]])
dd = cc.astype(float)
print(cc)
# [[1 2 3 4]
#  [4 5 6 7]]
print(dd)
# [[1. 2. 3. 4.]
#  [4. 5. 6. 7.]]

# ② .tolist()方法
array = np.full((2, 3), 95, dtype=int)
list1 = array.tolist()
print(array)
# [95 95 95]
#  [95 95 95]]
print(list1)
# [95, 95, 95], [95, 95, 95]]

9.ndarray数组的操作

①索引

# 一维数组索引
a = np.array([3, 4, 31, 35, 56])
print(a[3])   # 35
# 多维数组索引
b = np.arange(5, 29, 2).reshape((2, 2, 3))
print(b)
# [[[ 5  7  9]
#   [11 13 15]]
#
#  [[17 19 21]
#   [23 25 27]]]
print(b[1, 1, 2])   # 27
print(b[-2, -2, -1])    # 9

②切片

# 一维数组切片
a = np.array([3, 4, 31, 35, 56])
print(a[0:3:2])   # [ 3 31]
# 多维数组切片
b = np.arange(5, 29, 2).reshape((2, 2, 3))
print(b)
# [[[ 5  7  9]
#   [11 13 15]]
#
#  [[17 19 21]
#   [23 25 27]]]
print(b[:, 1, 1:3])
# [[13 15]
#  [25 27]]
print(b[:, :, ::2])    
# [[[ 5  9]
#   [11 15]]
# 
#  [[17 21]
#   [23 27]]]

10.ndarray数组的运算

注意:数组运算是指数组每个元素的运算,且几乎所有运算是运算后形成新数组。

①ndarray与标量(一个数据)之间的运算

a = np.arange(4, 16).reshape(2, 2, 3)
print(a)
# [[[ 4  5  6]
#   [ 7  8  9]]
#
#  [[10 11 12]
#   [13 14 15]]]
b = a / a.mean()
print(b)
# [[[0.42105263 0.52631579 0.63157895]
#   [0.73684211 0.84210526 0.94736842]]
#
#  [[1.05263158 1.15789474 1.26315789]
#   [1.36842105 1.47368421 1.57894737]]]

②ndarray一元运算

函数说明
np.abs(x)  np.fabs(x)计算数组各元素的绝对值
np.sqrt(x)计算数组各元素的平方根
np.square(x)计算数组各元素的平方
np.log(x) np.log10(x) np.log2(x)计算数组各元素的自然对数、10底对数和 2底对数
np.ceil(x) np.floor(x)计算数组各元素的ceiling值 或 floor 值
np.rint(x)计算数组各元素的四舍五入值
np.modf(x)将数组各元素的小数和整数部分以两个独立数组形式返回
np.cos(x) np.cosh(x) np.sin(x) np.sinh(x) np.tan(x) np.tanh(x)计算数组各元素的普通型和双曲型三角函数
np.exp(x)计算数组各元素的指数值
np.sign(x)计算数组各元素的符号值,1(+), 0, ‐1( ‐ )

a = np.arange(4, 16).reshape(2, 2, 3)
print(a)
# [[[ 4  5  6]
#   [ 7  8  9]]
#
#  [[10 11 12]
#   [13 14 15]]]
b = np.sqrt(a)
print(b)
# [[[2.         2.23606798 2.44948974]
#   [2.64575131 2.82842712 3.        ]]
#
#  [[3.16227766 3.31662479 3.46410162]
#   [3.60555128 3.74165739 3.87298335]]]

c = np.modf(b)
print(c)
# (array([[[0.        , 0.23606798, 0.44948974],
#         [0.64575131, 0.82842712, 0.        ]],
# 
#        [[0.16227766, 0.31662479, 0.46410162],
#         [0.60555128, 0.74165739, 0.87298335]]]), 
#  array([[[2., 2., 2.],
#         [2., 2., 3.]],
# 
#        [[3., 3., 3.],
#         [3., 3., 3.]]]))

③ndarray二元运算

函数说明
+ ‐ * / **两个数组各元素进行对应运算
np.maximum(x,y) np.fmax() np.minimum(x,y) np.fmin()元素级的最大值 /最小值计算
np.mod(x,y)元素级的模运算
np.copysign(x,y)将数组 y中各元素值的符号赋值给数组 x对应元素
> < >= <= == !=算术比较,产生布尔型数组
a = np.arange(4, 16).reshape(2, 2, 3)
b = np.sqrt(a)
c = np.maximum(a, b)
print(c)
# [[[ 4.  5.  6.]
#   [ 7.  8.  9.]]
#
#  [[10. 11. 12.]
#   [13. 14. 15.]]]
print(a > b)
# [[[ True  True  True]
#   [ True  True  True]]
# 
#  [[ True  True  True]
#   [ True  True  True]]]

11.NumPy数据存取与函数(一维和二维数据)

注意:因为CSV只能有效存储一维和二维数组 ,所以np.savetxt()和np.loadtxt()只能有效存取一维和二维数组

①numpy保存为csv文件

函数参数

np.savetxt(frame,

                 array,

                 fmt='%.18e',

                 delimiter=None) 

frame : 文件、字符串或产生器名字,可以是.gz或.bz2的压缩文件
array : 存入文件的数组
fmt : 写入文件的格式,例如:%d   %.2f    %.18e
delimiter : 分割字符串,默认是任何空格(None)
a = np.arange(50).reshape(5, 10)
np.savetxt('a.csv', a, fmt="%d", delimiter=",")
# a.csv文件
# 0,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,26,27,28,29
# 30,31,32,33,34,35,36,37,38,39
# 40,41,42,43,44,45,46,47,48,49
np.savetxt('a.csv', a, fmt="%.1f", delimiter=",")
# a.csv文件
# 0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0
# 10.0,11.0,12.0,13.0,14.0,15.0,16.0,17.0,18.0,19.0
# 20.0,21.0,22.0,23.0,24.0,25.0,26.0,27.0,28.0,29.0
# 30.0,31.0,32.0,33.0,34.0,35.0,36.0,37.0,38.0,39.0
# 40.0,41.0,42.0,43.0,44.0,45.0,46.0,47.0,48.0,49.0

②numpy读取csv文件

函数参数

np.loadtxt(frame,                                 dtype=np.float,                     delimiter=None,                 unpack=False)

frame : 文件、字符串或产生器名字,可以是.gz或.bz2的压缩文件
dtype : 数据类型(可选)
delimiter : 分割字符串,默认是任何空格(None)
unpack : 如果True,读入属性将分别写入不同变量
# a.csv文件
# 0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0
# 10.0,11.0,12.0,13.0,14.0,15.0,16.0,17.0,18.0,19.0
# 20.0,21.0,22.0,23.0,24.0,25.0,26.0,27.0,28.0,29.0
# 30.0,31.0,32.0,33.0,34.0,35.0,36.0,37.0,38.0,39.0
# 40.0,41.0,42.0,43.0,44.0,45.0,46.0,47.0,48.0,49.0
b = np.loadtxt("a.csv", delimiter=",")
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. 24. 25. 26. 27. 28. 29.]
#  [30. 31. 32. 33. 34. 35. 36. 37. 38. 39.]
#  [40. 41. 42. 43. 44. 45. 46. 47. 48. 49.]]
c = np.loadtxt("a.csv", dtype=int, delimiter=",")
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 24 25 26 27 28 29]
#  [30 31 32 33 34 35 36 37 38 39]
#  [40 41 42 43 44 45 46 47 48 49]]

12.NumPy数据存取与函数(多维数据)

注意:需要存储空间进行数据缓存,使用np.load()和np.save()方法 ;需要存储的文件与其他程序做数据交互,考虑csv文件或使用a.tofile()和np.fromfile()方法生成其他格式文件。

①numpy写入文件

注意:写入文件时,维度信息丢失。

函数参数
a.tofile(frame, sep='', format='%s')frame:文件、字符串
sep : 数据分割字符串,如果是空串,写入文件为二进制
format : 写入数据的格式
a = np.arange(50).reshape((5, 10))
a.tofile("a.dat", sep=",", format="%d")
# a.dat文件
# 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,...
a.tofile("a.dat", format="%d")
# a.dat文件
# 无法用文本编辑器打开,因为是一个二进制文件

②numpy读取文件

注意:该方法需要读取时知道存入文件时数组的维度和元素类型;a.tofile() 和np.fromfile()需要配合使用; 可以通过元数据文件来存储额外信息。

函数参数
np.fromfile(frame, dtype=float, count=‐1, sep='')frame:文件、字符串
dtype : 读取的数据类型
count : 读入元素个数,‐1表示读入整个文件
sep : 数据分割字符串,如果是空串,写入文件为二进制
# a.dat文件
# 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,...
b = np.fromfile("a.dat", dtype=int, sep=",").reshape(5, 10)
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 24 25 26 27 28 29]
#  [30 31 32 33 34 35 36 37 38 39]
#  [40 41 42 43 44 45 46 47 48 49]]

③numpy便捷文件存取

注意:npy文件存储numpy数组的维度和数据类型信息。

函数参数
np.save(fname, array) 或 np.savez(fname, array)

fname : 文件名,以.npy为扩展名,压缩扩展名为.npz 

array : 数组变量

np.load(fname) fname : 文件名,以.npy为扩展名,压缩扩展名为.npz
a = np.arange(50).reshape((5, 10))
np.save("a.npy", a)
b = np.load("a.npy")
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 24 25 26 27 28 29]
#  [30 31 32 33 34 35 36 37 38 39]
#  [40 41 42 43 44 45 46 47 48 49]]

# a.npy文件
#�NUMPY v {'descr': '<i4', 'fortran_order': False, 'shape': (5, 10), }                                                         
#                            	   
#         
#                    

13.numpy随机数函数

函数说明
seed(s)随机数种子,s是给定的种子值

rand(d0,d1,..,dn)

根据d0‐dn创建随机数数组,浮点数,[0,1),均匀分布
randn(d0,d1,..,dn)根据d0‐dn创建随机数数组,标准正态分布(均值0,标准差1)
randint(low[,high,shape])

根据shape创建随机整数或整数数组,范围是[low, high)

shuffle(a)根据数组a的第0轴(最外层)进行随排列,改变数组a
permutation(a)根据数组a的第0轴(最外层)产生一个新的乱序数组,不改变数组a
choice(a[,size,replace,p])从一维数组a中以概率p抽取元素,形成size形状新数组 replace表示是否可以重用元素,默认为False
uniform(low,high,size)产生具有均匀分布的数组,low起始值,high结束值,size形状
normal(loc,scale,size)产生具有正态分布的数组,loc均值,scale标准差,size形状
poisson(lam,size)产生具有泊松分布的数组,lam随机事件发生率,size形状
# np.random.seed()函数,略
# 1.np.random.rand()函数
a = np.random.rand(3, 2)
print(a)
# [[0.10434593 0.43533938]
#  [0.17886641 0.73404229]
#  [0.05426554 0.33076928]]

# 2.np.random.randn()函数
b = np.random.randn(3, 2)
print(b)  # 服从标准正态分布(均值0,标准差1)
# [[-0.52396447 -0.69719522]
#  [-0.87873649 -1.27278869]
#  [ 0.70384487  0.69510891]]

c = 10 + 3 * np.random.randn(3, 2)
print(c)  # 服从正态分布(均值10,标准差3)
# [[ 7.40857588 11.19056049]
#  [ 5.74796626  7.69609659]
#  [10.43380629 12.99346777]]

# 3.np.random.randint()函数
d = np.random.randint(10, 20, (3, 2))
print(d)
# [[18 12]
#  [19 18]
#  [12 16]]

e = np.random.randint([1, 2, 4], [3, 4, 10])
print(e)  # 上下限可以是列表类型
# [1 2 6]

# 4.np.random.shuffle()函数
f = np.random.randint(10, 20, (3, 3))
print(f)
# [[12 10 19]
#  [10 18 12]
#  [13 16 12]]
np.random.shuffle(f)
print(f)
#  第一列打乱
# [[10 18 12]
#  [12 10 19]
#  [13 16 12]]

# 5.np.random.permutation()函数
g = np.random.randint(10, 20, (3, 3))
print(g)
# [[12 10 19]
#  [10 18 12]
#  [13 16 12]]
h = np.random.permutation(f)
print(g)
# [[12 10 19]
#  [10 18 12]
#  [13 16 12]]
print(h)
#  第一列打乱
# [[10 18 12]
#  [12 10 19]
#  [13 16 12]]

# 6.np.random.choice()函数
l = np.random.choice(["a", "b", "c", "d"], 5, p=[0.6, 0.1, 0.1, 0.2])
print(l)   # ['a' 'b' 'b' 'c' 'a']
m = np.random.choice(["a", "b", "c", "d"], 3, p=[0.6, 0.1, 0.1, 0.2], replace=False)
print(m)   # ['a' 'd' 'b']

# 7.np.random.uniform()函数,均匀分布
n = np.random.uniform(10, 18, (3, 2))
print(n)
# [[11.67218353 15.16991442]
#  [12.97996616 11.51811622]
#  [11.78351524 13.7186368 ]]

# 8.np.random.normal()函数,正态分布
o = np.random.normal(10, 3, (3, 2))
print(o)
# [[ 6.03644938  9.62291196]
#  [ 2.04715278  7.83930258]
#  [15.41690006 11.64679709]]

# 9.np.random.possion()函数,泊松分布
p = np.random.poisson(5, (3, 2))
print(p)
# [[4 6]
#  [4 4]
#  [3 4]]

14.numpy统计函数

函数说明
sum(a, axis=None)根据给定轴axis计算数组a相关元素之和,axis整数或元组
mean(a, axis=None)根据给定轴axis计算数组a相关元素的期望,axis整数或元组
average(a,axis=None,weights=None)根据给定轴axis计算数组a相关元素的加权平均值
std(a, axis=None)根据给定轴axis计算数组a相关元素的标准差
var(a, axis=None)根据给定轴axis计算数组a相关元素的方差
min(a) max(a)计算数组a中元素的最小值、最大值
argmin(a) argmax(a)计算数组a中元素最小值、最大值的降一维后下标
unravel_index(index, shape)根据shape将一维下标index转换成多维下标
ptp(a)计算数组a中元素最大值与最小值的差
median(a)计算数组a中元素的中位数(中值)
np.random.seed(10)
a = np.random.randint(10, 20, (3, 3))
print(a)
# [[19 14 10]
#  [11 19 10]
#  [11 18 19]]

# 1.np.sum()函数
print(np.sum(a))  # 131
print(np.sum(a, axis=0))   # [41 51 39]
print(np.sum(a, axis=1))
# [43
#  40
#  48]

# 2.np.mean()函数
print(np.mean(a))  # 14.555555555555555
print(np.mean(a, axis=0))  # [13.66666667 17.         13.        ]
print(np.mean(a, axis=1))
# [14.33333333
#  13.33333333
#  16.        ]

# 3.np.average()函数
print(np.average(a))  # 14.555555555555555
print(np.average(a, axis=0))  # [13.66666667 17.         13.        ]
print(np.average(a, axis=1))
# [14.33333333
#  13.33333333
#  16.        ]

# 4.np.std()函数
print(np.std(a))  # 3.9189315752329774
print(np.std(a, axis=0))  # [3.77123617 2.1602469  4.24264069]
print(np.std(a, axis=1))
# [3.68178701
#  4.02768199
#  3.55902608]

# 5.np.var()函数
print(np.var(a))  # 15.358024691358025
print(np.var(a, axis=0))  # [14.22222222  4.66666667 18.        ]
print(np.var(a, axis=1))
# [13.55555556
#  16.22222222
#  12.66666667]

# 6.np.min()和np.max()函数
print(np.min(a))  # 10
print(np.min(a, axis=0))  # [11 14 10]
print(np.min(a, axis=1))  # [10 10 11]
print(np.max(a))  # 19
print(np.max(a, axis=0))  # [19 19 19]
print(np.max(a, axis=1))  # [19 19 19]

# 7.np.argmin()和np.argmax()函数
print(np.argmin(a))  # 2
print(np.argmin(a, axis=0))  # [1 0 0]
print(np.argmin(a, axis=1))  # [2 2 0]
print(np.argmax(a))  # 0
print(np.argmax(a, axis=0))  # [0 1 2]
print(np.argmax(a, axis=1))  # [0 1 2]

# 8.unravel_index()函数,与np.argmin()和np.argmax()函数一起使用
print(np.unravel_index(np.argmin(a), a.shape))  # (0, 2)

# 9.np.ptp()函数
print(np.ptp(a))  # 9
print(np.ptp(a, axis=0))  # [8 5 9]
print(np.ptp(a, axis=1))  # [9 9 8]

# 10.np.median()函数
print(np.median(a))  # 14.0
print(np.median(a, axis=0))  # [11. 18. 10.]
print(np.median(a, axis=1))  # [14. 11. 18.]

15.numpy梯度函数

注意:在进行图像、声音等批量处理时,梯度有助于发现图像、声音的边缘。

梯度:连续值之间的变化率,即斜率 XY坐标轴连续三个X坐标对应的Y轴值:a, b, c,其中b的梯度是: (c‐a)/2

函数说明
np.gradient(f)计算数组f中元素的梯度,当f为多维时,返回每个维度梯度
np.random.seed(10)
a = np.random.randint(0, 20, 5)
print(a)  # [ 9  4 15  0 17]

b = np.gradient(a)
print(b)  # [-5.  3. -2.  1. 17.]       -5 = (4 - 9) / 1 = -5,  3 = (15 - 9) / 2 = 3 ,  17 = (17 - 0) / 1 = 17

c = np.random.randint(0, 20, (3, 5))
print(c)
# [[16 17  8  9  0]
#  [10  8  4 19 16]
#  [ 4 15 11 11  1]]
d = np.gradient(c)
print(d)
# [array([[ -6. ,  -9. ,  -4. ,  10. ,  16. ],
#        [ -6. ,  -1. ,   1.5,   1. ,   0.5],
#        [ -6. ,   7. ,   7. ,  -8. , -15. ]]),   最外层维度梯度
#  array([[  1. ,  -4. ,  -4. ,  -4. ,  -9. ],
#        [ -2. ,  -3. ,   5.5,   6. ,  -3. ],
#        [ 11. ,   3.5,  -2. ,  -5. , -10. ]])]   第二层维度梯度

16.numpy总结

.ndim        .shape        .size         .dtype             .itemsize

.reshape(shape)   .resize(shape)   .swapaxes(ax1,ax2)       .flatten()

np.arange(n)

np.ones(shape)

np.zeros(shape)

np.full(shape,val)

np.eye(n)

np.ones_like(a)

np.zeros_like(a)

np.full_like(a,val)

CSV文件

np.loadtxt()             np.savetxt()

多维数据存取

a.tofile()        np.fromfile()         

np.save()       np.savez()            np.load()

随机函数

np.random.rand()

np.random.randn()

np.random.randint()

np.random.seed()

np.random.shuffle()

np.random.permutation()

np.random.choice()

补充:numpy的广播机制

假设两个numpy数组a和b的形状相同,即a.shape == b.shape,则a*b的结果是数组中各个元素相乘。广播(Broadcast)是用于不同形状的numpy数组之间的数值运算。

举例:

①形状相同

a = np.array([[3, 6, 9], [2, 3, 6]])
b = np.array([[1, 2, 3], [4, 9, 6]])
print(a * b)
# [[ 3 12 27]
#  [ 8 27 36]]

②简单广播

a = np.array([3, 7, 8])
b = 10
print(a * b)
# [30 70 80]

③广播(Broadcast)

In order to broadcast, the size of the trailing axes(拖尾轴(末尾轴)) for both arrays in an operation must either be the same size or one of them must be one.

广播条件:将数组的形状右侧对齐,从右向左依次比较,要么相等,要么其中一个为1,才可以广播。

④使用广播

a = np.array([0, 10, 20, 30])
b = np.array([1, 2, 3])
print(a[:, np.newaxis] + b)
# [[ 1  2  3]
#  [11 12 13]
#  [21 22 23]
#  [31 32 33]]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值