• np.around()、np.floor()、np.ceil()、np.where()函数解析(最清晰的解释)


    欢迎关注WX公众号:【程序员管小亮】

    np.around()函数用于返回五舍六入后的值,可指定精度。

    np.floor()函数用于以元素方式返回输入的下限。

    np.ceil()函数用于以元素方式返回输入的上限。

    np.where()函数根据 condition 从 x 和 y 中选择元素,当为 True 时,选 x,否则选 y。

    1、np.around

    np.around(a, 
    	decimals=0, 
    	out=None
    )
    

    对于正好在舍入小数值之间的值,NumPy舍入到最接近的偶数值。当超过5时候(不包含5),才会进位!因此,1.5和2.5轮到2.0,-0.5和0.5轮到0.0等。由于IEEE浮点标准[1]中的小数部分的不精确表示以及当以10的幂进行缩放时引入的误差,结果也可能是令人惊讶的。

    参数:

    • a : array_like。输入数据。

    • decimals : int,可选。要舍入的小数位数(默认值:0)。如果小数为负数,则指定小数点左侧的位置数。

    • out : ndarray,可选。替代输出数组,用于放置结果。它必须具有与预期输出相同的形状,但如果需要,将输出输出值的类型。

    返回:

    • rounded_array : ndarray。与a相同类型的数组,包含舍入值。除非指定了out,否则将创建一个新数组。返回对结果的引用。复数的实部和虚部分别舍入。舍入浮点数的结果是浮点数。

    例子1:

    >>> np.around([0.37, 1.64])
    array([ 0.,  2.])
    >>> np.around([0.37, 1.64], decimals=1)
    array([ 0.4,  1.6])
    >>> np.around([.5, 1.5, 2.5, 3.5, 4.5]) # rounds to nearest even value
    array([ 0.,  2.,  2.,  4.,  4.])
    >>> np.around([1,2,3,11], decimals=1) # ndarray of ints is returned
    array([ 1,  2,  3, 11])
    >>> np.around([1,2,3,11], decimals=-1)
    array([ 0,  0,  0, 10])
    

    2、np.floor

    np.floor(x, /, out=None, *, where=True, 
    	casting='same_kind', order='K', dtype=None, 
    	subok=True[, signature, extobj]) = <ufunc 'floor'>
    

    参数:

    • x : array_like。输入数据。

    • out : ndarray,None或ndarray和None的元组,可选。存储结果的位置。如果提供,它必须具有输入广播的形状。如果未提供或None,则返回新分配的数组。元组(仅可作为关键字参数)的长度必须等于输出的数量。

    • where : array_like,可选。值True表示计算该位置的ufunc,值False表示仅将值保留在输出中。

    返回:

    • y : ndarray或标量。x中每个元素的底限。如果x是标量,则这是标量。

    例子2:

    >>> a = np.array([-2.5, -1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0])
    >>> np.floor(a)
    array([-3., -2., -2., -1.,  0.,  1.,  1.,  2.])
    

    3、np.ceil

    np.ceil(x, /, out=None, *, where=True, 
    	casting='same_kind', order='K', dtype=None, 
    	subok=True[, signature, extobj]) = <ufunc 'ceil'>
    

    参数:

    • x : array_like。输入数据。

    • out : ndarray,None或ndarray和None的元组,可选。存储结果的位置。如果提供,它必须具有输入广播的形状。如果未提供或None,则返回新分配的数组。元组(仅可作为关键字参数)的长度必须等于输出的数量。

    • where : array_like,可选。值True表示计算该位置的ufunc,值False表示仅将值保留在输出中。

    返回:

    • y : ndarray或标量。x中每个元素的上限,带floatdtype。如果x是标量,则这是标量。

    例子3:

    >>> a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0])
    >>> np.ceil(a)
    array([-1., -1., -0.,  1.,  2.,  2.,  2.])
    

    4、np.where

    np.where(condition, 
    	x, 
    	y
    )
    

    如果只给出条件,则返回condition.nonzero()

    参数:

    • condition:array_like,bool。如果为True,则产生x,否则产生y

    • x,y:array_like,可选。要从中选择的值。x,y和条件需要可以广播broadcastable到某种形状。

    返回:

    • out:ndarray或ndarray元组。如果同时指定了xy,则输出数组包含x的元素,其中conditionTrue,其他元素来自 y。如果只给出条件,则返回元组condition.nonzero(),条件为True 的索引。

    例子4:

    # 如果给出x和y并且输入数组是1-D,where则相当于:
    
    [xv if c else yv for (c,xv,yv) in zip(condition,x,y)]
    

    例子5:

    >>> np.where([[True, False], [True, True]],
    ...          [[1, 2], [3, 4]],
    ...          [[9, 8], [7, 6]])
    array([[1, 8],
           [3, 4]])
    >>> np.where([[0, 1], [1, 0]])
    (array([0, 1], dtype=int64), array([1, 0], dtype=int64))
    

    例子6:

    >>> x = np.arange(9.).reshape(3, 3)
    >>> print(x)
    [[0. 1. 2.]
     [3. 4. 5.]
     [6. 7. 8.]]
    
    >>> np.where( x > 5 )
    (array([2, 2, 2]), array([0, 1, 2]))
    >>> x[np.where( x > 3.0 )]               # Note: result is 1D.
    array([ 4.,  5.,  6.,  7.,  8.])
    >>> np.where(x < 5, x, -1)               # Note: broadcasting.
    array([[ 0.,  1.,  2.],
           [ 3.,  4., -1.],
           [-1., -1., -1.]])
           
    >>> goodvalues = [3, 4, 7]
    >>> ix = np.isin(x, goodvalues)
    >>> ix
    array([[False, False, False],
           [ True,  True, False],
           [False,  True, False]])
    >>> np.where(ix)
    (array([1, 1, 2]), array([0, 1, 1]))
    

    python课程推荐。
    在这里插入图片描述

  • 相关阅读:
    ArcMap导出图层属性为excel表
    ArcMap面转为线
    vue(18)声明周期函数
    geoserver发布mbtiles文件
    docker部署geoserver
    vue(17)组件插槽slot的使用
    vue(16)父子组件之间直接相互访问
    MySQL常用查询语句积累
    数据库的基本设计
    HashMap学习
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13302798.html
Copyright © 2020-2023  润新知