点击上方
“
小白学视觉
”,选择加"
星标
"或“
置顶
”
重磅干货,第一时间送达
01数组上的迭代
NumPy 包含一个迭代器对象numpy.nditer。它是一个有效的多维迭代器对象,可以用于在数组上进行迭代。数组的每个元素可使用 Python 的标准Iterator接口来访问。
import numpy as np a = np.arange(0 , 60 , 5 ) a = a.reshape(3 , 4 ) print(a)for x in np.nditer(a): print(x)
[[ 0 5 10 15] [20 25 30 35] [40 45 50 55]] 0 5 10 15 20 25 30 35 40 45 50 55
如果两个数组是可广播的,nditer组合对象能够同时迭代它们。假设数 组a具有维度 3X4,并且存在维度为 1X4 的另一个数组b,则使用以下类型的迭代器(数组b被广播到a的大小)。
import numpy as np a = np.arange(0 , 60 , 5 ) a = a.reshape(3 , 4 ) print(a) b = np.array([1 , 2 , 3 , 4 ], dtype=int) print(b)for x, y in np.nditer([a, b]): print(x, y)
[[ 0 5 10 15] [20 25 30 35] [40 45 50 55]] [1 2 3 4] 0 1 5 2 10 3 15 4 20 1 25 2 30 3 35 4 40 1 45 2 50 3 55 4
02 数组形状修改函数
1.ndarray.reshape
函数在不改变数据的条件下修改形状,参数如下:
ndarray.reshape(arr, newshape, order)
import numpy as np a = np.arange(8 ) print(a) b = a.reshape(4 , 2 ) print(b)
[0 1 2 3 4 5 6 7] [[0 1] [2 3] [4 5] [6 7]]
2.ndarray.flat
函数返回数组上的一维迭代器,行为类似 Python 内建的迭代器。
import numpy as np a = np.arange(0 , 16 , 2 ).reshape(2 , 4 ) print(a)# 返回展开数组中的下标的对应元素 print(list(a.flat))
[[ 0 2 4 6] [ 8 10 12 14]] [0, 2, 4, 6, 8, 10, 12, 14]
3.ndarray.flatten
函数返回折叠为一维的数组副本,函数接受下列参数:
ndarray.flatten(order)
其中:
order
:‘C’ — 按行,‘F’ — 按列,‘A’ — 原顺序,‘k’ —元素在内存中的出现顺序。
import
numpy as np a = np.arange(8 ).reshape(2 , 4 ) print(a)# default is column-major print(a.flatten()) print(a.flatten(order='F' ))
[[0 1 2 3] [4 5 6 7]] [0 1 2 3 4 5 6 7] [0 4 1 5 2 6 3 7]
03 数组翻转操作函数
1.numpy.transpose
函数翻转给定数组的维度。如果可能的话它会返回一个视图。函数接受下列参数:
numpy.transpose(arr, axes)
其中:
a
x
es:整数的列表,对应维度,通常所有维度都会翻转。
import numpy as np a = np.arange(24 ).reshape(2 , 3 , 4 ) print(a) b = np.array(np.transpose(a)) print(b) print(b.shape)
[[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [[12 13 14 15] [16 17 18 19] [20 21 22 23]]] [[[ 0 12] [ 4 16] [ 8 20]] [[ 1 13] [ 5 17] [ 9 21]] [[ 2 14] [ 6 18] [10 22]] [[ 3 15] [ 7 19] [11 23]]] (4, 3, 2)
b = np.array(np.transpose(a, (1 , 0 , 2 ))) print(b) print(b.shape
[[[ 0 1 2 3] [12 13 14 15]] [[ 4 5 6 7] [16 17 18 19]] [[ 8 9 10 11] [20 21 22 23]]] (3, 2, 4)
2. numpy.ndarray.T
该函数属于ndarray类,行为类似于
numpy.transpose.
import numpy as np a = np.arange(12 ).reshape(3 , 4 ) print(a) print(a.T)
[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [[ 0 4 8] [ 1 5 9] [ 2 6 10] [ 3 7 11]]
3.numpy.swapaxes
函数交换数组的两个轴。这个函数接受下列参数:
numpy.swapaxes(arr, axis1, axis2)
其中:
import numpy as np a = np.arange(8 ).reshape(2 , 2 , 2 ) print(a) print(np.swapaxes(a, 2 , 0 ))
[[[0 1] [2 3]] [[4 5] [6 7]]] [[[0 4] [2 6]] [[1 5] [3 7]]]
4.numpy.rollaxis
numpy.rollaxis()
函数向后滚动特定的轴,直到一个特定位置。这个函数接受三个参数:
numpy.rollaxis(arr, axis, start)
其中:
axis
:要向后滚动的轴,其它轴的相对位置不会改变
start
:默认为零,表示完整的滚动。会滚动到特定位置。
import numpy as np a = np.arange(8 ).reshape(2 ,2 ,2 ) print(a) print(np.rollaxis(a,2 )) print(np.rollaxis(a,2 ,1 ))
[[[0 1] [2 3]] [[4 5] [6 7]]] [[[0 2] [4 6]] [[1 3] [5 7]]] [[[0 2] [1 3]] [[4 6] [5 7]]]
04 数组修改维度函数
1.numpy.broadcast_to
函数将数组广播到新形状。它在原始数组上返回只 读视图。它通常不连续。如果新形状不符合 NumPy 的广播规则,该函数可能会抛出ValueError。该函数接受以下参数:
numpy.broadcast_to(array, shape, subok)
import numpy as np a = np.arange(4 ).reshape(1 ,4 ) print(a) print(np.broadcast_to(a,(4 ,4 )))
[[0 1 2 3]] [[0 1 2 3] [0 1 2 3] [0 1 2 3] [0 1 2 3]]
2.numpy.expand_dims
函数通过在指定位置插入新的轴来扩展数组形状。该函数需要两个参数:
numpy.expand_dims(arr, axis)
其中:
import numpy as np x = np.array(([1 , 2 ], [3 , 4 ])) print(x) y = np.expand_dims(x, axis=0 ) print(y) print(x.shape, y.shape) y = np.expand_dims(x, axis=1 ) print(y) print(x.ndim, y.ndim) print(x.shape, y.shape)
[[1 2] [3 4]] [[[1 2] [3 4]]] (2, 2) (1, 2, 2) [[[1 2]] [[3 4]]] 2 3 (2, 2) (2, 1, 2)
3.numpy.squeeze
函数从给定数组的形状中删除一维条目。此函数需要两 个参数。
numpy.squeeze(arr, axis)
其中:
axis
:整数或整数元组,用于选择形状中单一维度条目的子集
import numpy as np x = np.arange(9 ).reshape(1 , 3 , 3 ) print(x) y = np.squeeze(x) print(y) print(x.shape, y.shape)
[[[0 1 2] [3 4 5] [6 7 8]]] [[0 1 2] [3 4 5] [6 7 8]] (1, 3, 3) (3, 3)
05 数组的连接操作
NumPy中数组的连接函数主要有如下四个:
1.numpy.stack
函数沿新轴连接数组序列,需要提供以下参数:
numpy.stack(arrays, axis)
其中:
import numpy as np a = np.array([[1 ,2 ],[3 ,4 ]]) print(a) b = np.array([[5 ,6 ],[7 ,8 ]]) print(b) print(np.stack((a,b),0 )) print(np.stack((a,b),1 ))
[[1 2] [3 4]] [[5 6] [7 8]] [[[1 2] [3 4]] [[5 6] [7 8]]] [[[1 2] [5 6]] [[3 4] [7 8]]]
2.numpy.hstack
是
numpy.stack()
函数的变体,通过堆叠来生成水平的单个数组。
import numpy as np a = np.array([[1 , 2 ], [3 , 4 ]]) print(a) b = np.array([[5 , 6 ], [7 , 8 ]]) print(b) print('水平堆叠:' ) c = np.hstack((a, b)) print(c)
[[1 2] [3 4]] [[5 6] [7 8]] 水平堆叠: [[1 2 5 6] [3 4 7 8]]
3.numpy.vstack
是
numpy.stack()
函数的变体,通过堆叠来生成竖直的单个数组。
import numpy as np a = np.array([[1 , 2 ], [3 , 4 ]]) print(a) b = np.array([[5 , 6 ], [7 , 8 ]]) print(b) print('竖直堆叠:' ) c = np.vstack((a, b)) print(c)
[[1 2] [3 4]] [[5 6] [7 8]] 竖直堆叠: [[1 2] [3 4] [5 6] [7 8]]
4.numpy.concatenate
函数用于沿指定轴连接相同形状的两个或多个数组。该函数接受以下参数。
numpy.concatenate((a1, a2, …), axis)
其中:
import numpy as np a = np.array([[1 ,2 ],[3 ,4 ]]) print(a) b = np.array([[5 ,6 ],[7 ,8 ]]) print(b) print(np.concatenate((a,b))) print(np.concatenate((a,b),axis = 1 ))
[[1 2] [3 4]] [[5 6] [7 8]] [[1 2] [3 4] [5 6] [7 8]] [[1 2 5 6] [3 4 7 8]]
06 数组的分割操作
NumPy中数组的数组分割函数主要如下:
hsplit
将一个数组水平分割为多个子数组(按列)
vsplit
将一个数组竖直分割为多个子数组(按行)
1.numpy.split
该函数沿特定的轴将数组分割为子数组。函数接受三个参数:
numpy.split(ary, indices_or_sections, axis)
其中:
indices_or_sections
:可以是整数,表明要从输入数组创建的,等大小的子数组的数量。如果此参数是一维数组,则其元素表明要创建新子数组的点。
import numpy as np a = np.arange(9 ) print(a) print('将数组分为三个大小相等的子数组:' ) b = np.split(a,3 ) print(b) print('将数组在一维数组中表明的位置分割:' ) b = np.split(a,[4 ,7 ]) print(b)
[0 1 2 3 4 5 6 7 8] 将数组分为三个大小相等的子数组: [array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8])] 将数组在一维数组中表明的位置分割: [array([0, 1, 2, 3]), array([4, 5, 6]), array([7, 8])]
2.numpy.hsplit
split()
函数的特例,其中轴为 1 表示水平分割。
import numpy as np a = np.arange(16 ).reshape(4 ,4 ) print(a) print('水平分割:' ) b = np.hsplit(a,2 ) print(b)
[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15]] 水平分割: [array([[ 0, 1], [ 4, 5], [ 8, 9], [12, 13]]), array([[ 2, 3], [ 6, 7], [10, 11], [14, 15]])]
3.numpy.vsplit
split()
函数的特例,其中轴为 0 表示竖直分割,无论输入数组的维度是什么。
import numpy as np a = np.arange(16 ).reshape(4 ,4 ) print(a) print('竖直分割:' ) b = np.vsplit(a,2 ) print(b)
[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15]] 竖直分割: [array([[0, 1, 2, 3], [4, 5, 6, 7]]), array([[ 8, 9, 10, 11], [12, 13, 14, 15]])]
07 数组元素操作
NumPy中数组操作函数主要如下:
1.numpy.resize
函数返回指定大小的新数组。如果新大小大于原始大小,则包含原始数组中的元素的重复副本。如果小于则去掉原始数组的部分数据。该函数接受以下参数:
numpy.resize(arr, shape)
其中:
import numpy as np a = np.array([[1 ,2 ,3 ],[4 ,5 ,6 ]]) print(a) print(a.shape) b = np.resize(a, (3 ,2 )) print(b) print(b.shape) print('修改第二个数组的大小:' ) b = np.resize(a,(3 ,3 )) print(b) print('修改第三个数组的大小:' ) b = np.resize(a,(2 ,2 )) print(b)
[[1 2 3] [4 5 6]] (2, 3) [[1 2] [3 4] [5 6]] (3, 2) 修改第二个数组的大小: [[1 2 3] [4 5 6] [1 2 3]] 修改第三个数组的大小: [[1 2] [3 4]]
2.numpy.append
函数在输入数组的末尾添加值。附加操作不是原地的,而是分配新的数组。此外,输入数组的维度必须匹配否则将生成ValueError。函数接受下列函数:
numpy.append(arr, values, axis)
其中:
values
:要向arr添加的值,比如和arr形状相同(除了要添加的轴)
axis
:沿着它完成操作的轴。如果没有提供,两个参数都会被展开。
import numpy as np a = np.array([[1 ,2 ,3 ],[4 ,5 ,6 ]]) print(a) print(np.append(a, [[7 ,8 ,9 ]],axis = 0 )) print(np.append(a, [[5 ,5 ,5 ],[7 ,8 ,9 ]],axis = 1 ))
[[1 2 3] [4 5 6]] [[1 2 3] [4 5 6] [7 8 9]] [[1 2 3 5 5 5] [4 5 6 7 8 9]]
3.numpy.insert
函数在给定索引之前,沿给定轴在输入数组中插入值。如果值的类型转换为要插入,则它与输入数组不同。插入没有原地的,函数会返回一个新数组。此外,如果未提供轴,则输入数组会被展开。
insert()函数接受以下参数:
numpy.insert(arr, obj, values, axis)
其中:
import numpy as np a = np.array([[1 ,2 ],[3 ,4 ],[5 ,6 ]]) print(a) print(np.insert(a,3 ,[11 ,12 ])) print(np.insert(a,1 ,[11 ],axis = 0 )) print(np.insert(a,1 ,[11 ],axis = 1 ))
[[1 2] [3 4] [5 6]] [ 1 2 3 11 12 4 5 6] [[ 1 2] [11 11] [ 3 4] [ 5 6]] [[ 1 11 2] [ 3 11 4] [ 5 11 6]]
4.numpy.delete
函数返回从输入数组中删除指定子数组的新数组。与insert()函数的情况一样,如果未提供轴参数,则输入数组将展开。该函 数接受以下参数:
Numpy.delete(arr, obj, axis)
其中:
obj
:可以被切片,整数或者整数数组,表明要从输入数组删除的子数组
import numpy as np a = np.array([[1 ,2 ],[3 ,4 ],[5 ,6 ]]) print(a) print(np.delete(a,5 )) print(np.delete(a,1 ,axis = 1 ))
[[1 2] [3 4] [5 6]] [1 2 3 4 5] [[1] [3] [5]]
5.numpy.unique
函数返回输入数组中的去重元素数组。该函数能够返回一个元组,包含去重数组和相关索引的数组。索引的性质取决于函数调用中返回参数的类型。
numpy.unique(arr, return_index, return_inverse, return_counts)
其中:
•
arr
:输入数组,如果不是一维数组则会展开
•
return_index
:如果为true,返回输入数组中的元素下标
•
return_inverse
:如果为true,返回去重数组的下标,它可以用于重构输入数组
•
return_counts
:如果为true,返回去重数组中的元素在原数组中的出现次数
import numpy as np a = np.array([5 ,2 ,6 ,2 ,7 ,5 ,6 ,8 ,2 ,9 ]) u = np.unique(a) print(u) u,indices = np.unique(a, return_index = True ) print(u, indices) u,indices = np.unique(a,return_inverse = True ) print(u, indices) u,indices = np.unique(a,return_counts = True ) print(u, indices)
[2 5 6 7 8 9][2 5 6 7 8 9] [1 0 2 4 7 9][2 5 6 7 8 9] [1 0 2 0 3 1 2 4 0 5][2 5 6 7 8 9] [3 2 2 1 1 1]
08 NumPy - 字符串函数
以下函数用于对dtype为numpy.string_或numpy.unicode_的数组执行向量 化字符串操作。它们基于 Python 内置库中的标准字符串函数。字符数组类(numpy.char)中定义
import numpy as np print(np.char.add(['hello' ],[' xyz' ])) print(np.char.add(['hello' , 'hi' ],[' abc' , ' xyz' ])) print(np.char.multiply('Hello ' ,3 )) print(np.char.center('hello' , 20 ,fillchar = '*' )) print(np.char.capitalize('hello world' )) print(np.char.title('hello how are you?' )) print(np.char.lower(['HELLO' ,'WORLD' ])) print(np.char.lower('HELLO' )) print(np.char.upper('hello' )) print(np.char.upper(['hello' ,'world' ])) print(np.char.split ('hello how are you?' )) print(np.char.split ('YiibaiPoint,Hyderabad,Telangana' , sep = ',' )) print(np.char.splitlines('hello\nhow are you?' )) print(np.char.splitlines('hello\rhow are you?' )) print(np.char.strip('ashok arora' ,'a' )) print(np.char.strip(['arora' ,'admin' ,'java' ],'a' )) print(np.char.join(':' ,'dmy' )) print(np.char.join([':' ,'-' ],['dmy' ,'ymd' ])) print(np.char.replace ('He is a good boy' , 'is' , 'was' )) a = np.char.encode('hello' , 'cp500' ) print(a) print(np.char.decode(a,'cp500' ))
['hello xyz'] ['hello abc' 'hi xyz'] Hello Hello Hello *******hello******** Hello world Hello How Are You? ['hello' 'world'] hello HELLO ['HELLO' 'WORLD'] ['hello', 'how', 'are', 'you?'] ['YiibaiPoint', 'Hyderabad', 'Telangana'] ['hello', 'how are you?'] ['hello', 'how are you?'] shok aror ['ror' 'dmin' 'jav'] d:m:y ['d:m:y' 'y-m-d'] He was a good boy b'\x88\x85\x93\x93\x96' hello
09 NumPy - 算数函数
NumPy 包含大量的各种数学运算功能。NumPy 提供标准的三角函数,算术运算的函数,复数处理函数等。
1. NumPy -三角函数
NumPy 拥有标准的三角函数,它为弧度制单位的给定角度返回三角函 数比值。arcsin,arccos,和arctan函数返回给定角度的sin,cos和tan的反三角函数。这些函数的结果可以通过
numpy.degrees()
函数通过将弧度制 转换为角度制来验证。
import