Py之Numpy:Numpy库中常用函数的简介、应用之详细攻略

简介: Py之Numpy:Numpy库中常用函数的简介、应用之详细攻略

Numpy库中常用函数的简介、应用


1、X, Y = np.meshgrid(X, Y)


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)  

————————————————

版权声明:本文为CSDN博主「一个处女座的程序猿」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/qq_41185868/article/details/106112967

相关文章
|
18天前
|
Python
NumPy 教程 之 NumPy 统计函数 9
NumPy提供了多种统计函数,如计算数组中的最小值、最大值、百分位数、标准差及方差等。其中,标准差是一种衡量数据平均值分散程度的指标,它是方差的算术平方根。例如,对于数组[1,2,3,4],其标准差可通过计算各值与均值2.5的差的平方的平均数的平方根得出,结果为1.1180339887498949。示例代码如下: ```python import numpy as np print(np.std([1,2,3,4])) ``` 运行输出即为:1.1180339887498949。
110 50
|
18天前
|
Python
NumPy 教程 之 NumPy 统计函数 10
NumPy统计函数,包括查找数组中的最小值、最大值、百分位数、标准差和方差等。方差表示样本值与平均值之差的平方的平均数,而标准差则是方差的平方根。例如,`np.var([1,2,3,4])` 的方差为 1.25。
94 48
|
14天前
|
机器学习/深度学习 搜索推荐 算法
NumPy 教程 之 NumPy 排序、条件筛选函数 8
NumPy提供了多种排序方法,包括快速排序、归并排序及堆排序,各有不同的速度、最坏情况性能、工作空间和稳定性特点。此外,NumPy还提供了`numpy.extract()`函数,可以根据特定条件从数组中抽取元素。例如,在一个3x3数组中,通过定义条件选择偶数元素,并使用该函数提取这些元素。示例输出为:[0., 2., 4., 6., 8.]。
20 8
|
17天前
|
机器学习/深度学习 搜索推荐 算法
NumPy 教程 之 NumPy 排序、条件筛选函数 2
介绍NumPy` 中的排序方法与条件筛选函数。通过对比快速排序、归并排序及堆排序的速度、最坏情况性能、工作空间需求和稳定性,帮助读者选择合适的排序算法。此外,还深入讲解了 `numpy.argsort()` 的使用方法,并通过具体实例展示了如何利用该函数获取数组值从小到大的索引值,并据此重构原数组,使得其变为有序状态。对于学习 `NumPy` 排序功能来说,本教程提供了清晰且实用的指导。
19 7
|
14天前
|
机器学习/深度学习 搜索推荐 算法
NumPy 教程 之 NumPy 排序、条件筛选函数 5
NumPy中的排序方法及特性对比,包括快速排序、归并排序与堆排序的速度、最坏情况性能、工作空间及稳定性分析。并通过`numpy.argmax()`与`numpy.argmin()`函数演示了如何获取数组中最大值和最小值的索引,涵盖不同轴方向的操作,并提供了具体实例与输出结果,便于理解与实践。
17 4
|
16天前
|
机器学习/深度学习 搜索推荐 算法
NumPy 教程 之 NumPy 排序、条件筛选函数 4
NumPy提供了多种排序方法,包括快速排序、归并排序及堆排序等,具有不同的执行速度、最坏情况性能、工作空间需求及稳定性特征。教程涵盖了`msort`、`sort_complex`、`partition`和`argpartition`等函数的使用方法,并通过实例展示了复数排序与分区排序的应用。例如,`np.sort_complex()`用于复数排序,`np.partition()`实现基于指定位置的分区排序,而`argpartition()`则帮助快速找到数组中的特定值。
8 0
|
19天前
|
索引 Python
NumPy 教程 之 NumPy 统计函数 8
这段内容介绍了 NumPy 中的 `numpy.average()` 函数,该函数用于计算数组中元素的加权平均值。可以通过设置 `axis` 参数指定计算的轴,`weights` 参数用于指定权重,默认为等权重。示例展示了如何在一维和多维数组中使用此函数,并通过 `returned=True` 返回加权平均值和权重总和。
19 0
|
28天前
|
机器学习/深度学习 数据处理 计算机视觉
NumPy实践宝典:Python高手教你如何轻松玩转数据处理!
【8月更文挑战第22天】NumPy是Python科学计算的核心库,专长于大型数组与矩阵运算,并提供了丰富的数学函数。首先需安装NumPy (`pip install numpy`)。之后可通过创建数组、索引与切片、执行数学与逻辑运算、变换数组形状及类型、计算统计量和进行矩阵运算等操作来实践学习。NumPy的应用范围广泛,从基础的数据处理到图像处理都能胜任,是数据科学领域的必备工具。
43 0
|
20天前
|
存储 缓存 C语言
|
19天前
|
机器学习/深度学习 存储 算法
NumPy 与 SciPy:Python 科学计算库的比较
【8月更文挑战第30天】
51 1