Py之numpy:numpy库的使用方法之基础函数(np.concatenate/np.meshgrid等)简介、使用方法之详细攻略
目录
numpy库中的一些函数简介、使用方法
1、np.concatenate()
1.1、函数案例
1.2、函数用法
2、np.meshgrid()
2.1、函数案例
2.2、函数用法
numpy库中的一些函数简介、使用方法 1、np.concatenate() 1.1、函数案例
import numpy as np
a=np.array([1,2,3])
b=np.array([11,22,33])
c=np.array([44,55,66])
d=np.concatenate((a,b,c),axis=0) # 默认情况下,axis=0可以不写
print(d) #输出array([ 1, 2, 3, 11, 22, 33, 44, 55, 66]),对于一维数组拼接,axis的值不影响最后的结果
1.2、函数用法
concatenate Found at: numpy.core.multiarray concatenate((a1, a2, ...), axis=0, out=None) Join a sequence of arrays along an existing axis. Parameters ---------- a1, a2, ... : sequence of array_like. The arrays must have the same shape, except in the dimension corresponding to `axis` (the first, by default). axis : int, optional. The axis along which the arrays will be joined. Default is 0. out : ndarray, optional. If provided, the destination to place the result. The shape must be correct, matching that of what concatenate would have returned if no out argument were specified. Returns ------- res : ndarray The concatenated array. 在:numpy.core.multiarray找到连接 连接((a1, a2,…),axis=0, out=None) 沿着现有的轴连接数组序列。 参数 ---------- a1, a2,…:数组类型的序列。数组必须具有相同的形状,除了与“axis”对应的维度(默认情况下为第一个维度)。axis: int,可选。数组连接的轴线。默认值为0。out : ndarray,可选。如果提供,放置结果的目的地。形状必须正确,如果没有指定out参数,则匹配concatenate将返回的形状。
返回 ------- res: ndarray 连接后的字符串数组。
See Also -------- ma.concatenate : Concatenate function that preserves input masks. array_split : Split an array into multiple sub-arrays of equal or near-equal size. split : Split array into a list of multiple sub-arrays of equal size. hsplit : Split array into multiple sub-arrays horizontally (column wise) vsplit : Split array into multiple sub-arrays vertically (row wise) dsplit : Split array into multiple sub-arrays along the 3rd axis (depth). stack : Stack a sequence of arrays along a new axis. hstack : Stack arrays in sequence horizontally (column wise) vstack : Stack arrays in sequence vertically (row wise) dstack : Stack arrays in sequence depth wise (along third dimension) Notes ----- When one or more of the arrays to be concatenated is a MaskedArray, this function will return a MaskedArray object instead of an ndarray, but the input masks are *not* preserved. In cases where a MaskedArray is expected as input, use the ma.concatenate function from the masked array module instead.另请参阅 -------- 马。保存输入掩码的连接函数。 array_split:将一个数组分割成多个相等或的子数组 与大小。 分割:将数组分割成相同大小的多个子数组。 hsplit:水平(按列)将数组分割为多个子数组 垂直(按行)将数组分割为多个子数组 dsplit:沿着第三轴(深度)将数组分割成多个子数组。 堆栈:将数组序列沿着一个新的轴进行堆栈。 hstack:水平排列(按列排列) 垂直(行向)按顺序堆叠数组。 dstack:按深度顺序排列的堆栈数组(沿三维方向) 笔记 ----- 当一个或多个要连接的数组是一个MaskedArray时,这个函数将返回一个MaskedArray对象而不是ndarray,但是输入掩码*不*保留。在需要MaskedArray作为输入的情况下,使用ma。连接函数从掩码数组模块代替。Examples -------- >>> a = np.array([[1, 2], [3, 4]]) >>> b = np.array([[5, 6]]) >>> np.concatenate((a, b), axis=0) array([[1, 2], [3, 4], [5, 6]]) >>> np.concatenate((a, b.T), axis=1) array([[1, 2, 5], [3, 4, 6]])
This function will not preserve masking of MaskedArray inputs. >>> a = np.ma.arange(3) >>> a[1] = np.ma.masked >>> b = np.arange(2, 5) >>> a masked_array(data = [0 -- 2], mask = [False True False], fill_value = 999999) >>> b array([2, 3, 4]) >>> np.concatenate([a, b]) masked_array(data = [0 1 2 2 3 4], mask = False, fill_value = 999999) >>> np.ma.concatenate([a, b]) masked_array(data = [0 -- 2 2 3 4], mask = [False True False False False False], fill_value = 999999)
2、np.meshgrid() 2.1、函数案例X, Y = np.meshgrid(X, Y)
2.2、函数用法
meshgrid Found at: numpy.lib.function_base Return coordinate matrices from coordinate vectors. Make N-D coordinate arrays for vectorized evaluations of N-D scalar/vector fields over N-D grids, given one-dimensional coordinate arrays x1, x2,..., xn. .. versionchanged:: 1.9 1-D and 0-D cases are allowed. Parameters ---------- x1, x2,..., xn : array_like 1-D arrays representing the coordinates of a grid. indexing : {'xy', 'ij'}, optional Cartesian ('xy', default) or matrix ('ij') indexing of output. See Notes for more details. .. versionadded:: 1.7.0 sparse : bool, optional If True a sparse grid is returned in order to conserve memory. Default is False. .. versionadded:: 1.7.0 copy : bool, optional. If False, a view into the original arrays are returned in order to conserve memory. Default is True. Please note that ``sparse=False, copy=False`` will likely return noncontiguous arrays. Furthermore, more than one element of a broadcast array may refer to a single memory location. If you need to write to the arrays, make copies first. .. versionadded:: 1.7.0 Returns ------- X1, X2,..., XN : ndarray For vectors `x1`, `x2`,..., 'xn' with lengths ``Ni=len(xi)`` , return ``(N1, N2, N3,...Nn)`` shaped arrays if indexing='ij' or ``(N2, N1, N3,...Nn)`` shaped arrays if indexing='xy' with the elements of `xi` repeated to fill the matrix along the first dimension for `x1`, the second for `x2` and so on. Notes ----- This function supports both indexing conventions through the indexing keyword argument. Giving the string 'ij' returns a meshgrid with matrix indexing, while 'xy' returns a meshgrid with Cartesian indexing. In the 2-D case with inputs of length M and N, the outputs are of shape (N, M) for 'xy' indexing and (M, N) for 'ij' indexing. In the 3-D case with inputs of length M, N and P, outputs are of shape (N, M, P) for 'xy' indexing and (M, N, P) for 'ij' indexing. The difference is illustrated by the following code snippet:: xv, yv = np.meshgrid(x, y, sparse=False, indexing='ij') for i in range(nx): for j in range(ny): # treat xv[i,j], yv[i,j] xv, yv = np.meshgrid(x, y, sparse=False, indexing='xy') for i in range(nx): for j in range(ny): # treat xv[j,i], yv[j,i] In the 1-D and 0-D case, the indexing and sparse keywords have no effect. See Also -------- index_tricks.mgrid : Construct a multi-dimensional "meshgrid" using indexing notation. index_tricks.ogrid : Construct an open multi-dimensional "meshgrid" using indexing notation. 从坐标向量返回坐标矩阵。
建立N-D坐标阵列,在N-D网格上对N-D标量/向量场进行向量化计算,给定一维坐标阵列x1, x2,…,xn。 . .versionchanged:: 1.9 允许1-D和0-D。参数 ---------- x1, x2,…, xn: array_like 表示网格坐标的一维数组。 索引:{'xy', 'ij'},可选 Cartesian ('xy',默认)或矩阵('ij')索引的输出。 参见注释了解更多细节。 . .versionadded: 1.7.0 稀疏:bool,可选 如果为真,则返回一个稀疏网格以保存内存。默认是假的。 . .versionadded: 1.7.0 复制:bool,可选。如果为假,则返回原始数组的视图 为了保存记忆。默认是正确的。请注意,' ' sparse=False, copy=False ' '将可能返回不相邻的数组。此外,广播数组中的多个元素可以引用单个内存位置。如果需要对数组进行写入,请首先进行复制。 . .versionadded: 1.7.0返回 ------- X1, X2,…XN: ndarray 对于向量“x1”,“x2”,…, 'xn'加上length ' ' ' Ni=len(xi) ' ', 返回' ' (N1, N2, N3,…Nn) ' '形数组如果索引='ij'或' ' (N2, N1, N3,…Nn) ' '形数组如果索引='xy'与元素' xi '重复填充矩阵沿第一个维度为' x1 ',第二个为' x2 ',以此类推。 笔记 ----- 这个函数通过索引关键字参数支持两种索引约定。给出字符串'ij'返回一个带矩阵索引的meshgrid,而'xy'返回一个带笛卡尔索引的meshgrid。 在输入长度为M和N的二维情况下,输出的形状为(N, M),表示“xy”索引,(M, N)表示“ij”索引。在输入长度为M、N和P的3-D情况下,输出的形状(N、M、P)表示“xy”索引,(M、N、P)表示“ij”索引。区别如下面的代码片段所示:: xv yv = np。meshgrid(x, y, sparse=False, index ='ij') i在range(nx)内: j in range(ny): 治疗xv[i,j], yv[i,j] xv yv = np。meshgrid(x, y, sparse=False, index ='xy') i在range(nx)内: j in range(ny): 在1-D和0-D情况下,索引和稀疏关键字没有影响。。 另请参阅 -------- index_tricks。mgrid:使用索引符号构造一个多维“meshgrid”。 index_tricks。ogrid:使用索引符号构造一个开放的多维“meshgrid”。
Examples -------- >>> nx, ny = (3, 2) >>> x = np.linspace(0, 1, nx) >>> y = np.linspace(0, 1, ny) >>> xv, yv = np.meshgrid(x, y) >>> xv array([[ 0. , 0.5, 1. ], [ 0. , 0.5, 1. ]]) >>> yv array([[ 0., 0., 0.], [ 1., 1., 1.]]) >>> xv, yv = np.meshgrid(x, y, sparse=True) # make sparse output arrays >>> xv array([[ 0. , 0.5, 1. ]]) >>> yv array([[ 0.], [ 1.]]) `meshgrid` is very useful to evaluate functions on a grid. >>> x = np.arange(-5, 5, 0.1) >>> y = np.arange(-5, 5, 0.1) >>> xx, yy = np.meshgrid(x, y, sparse=True) >>> z = np.sin(xx**2 + yy**2) / (xx**2 + yy**2) >>> h = plt.contourf(x,y,z)