【Numpy】numpy.maximum,取绝对值

numpy.minimum

numpy.minimum(x1x2/out=None*where=Truecasting='same_kind'order='K'dtype=Nonesubok=True[, signatureextobj]) = <ufunc 'minimum'>

其实这个函数就是x1和x2对应位置元素进行比较,选取小的那个元素即可。

Element-wise minimum of array elements.

Compare two arrays and returns a new array containing the element-wise minima. If one of the elements being compared is a NaN, then that element is returned. If both elements are NaNs then the first is returned. The latter distinction is important for complex NaNs, which are defined as at least one of the real or imaginary parts being a NaN. The net effect is that NaNs are propagated.

Parameters:

x1, x2 : array_like

The arrays holding the elements to be compared. If x1.shape != x2.shape, they must be broadcastable to a common shape (which becomes the shape of the output).

out : ndarray, None, or tuple of ndarray and None, optional

A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple (possible only as a keyword argument) must have length equal to the number of outputs.

where : array_like, optional

This condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value. Note that if an uninitialized out array is created via the default out=None, locations within it where the condition is False will remain uninitialized.

**kwargs

For other keyword-only arguments, see the ufunc docs.

Returns:

y : ndarray or scalar

The minimum of x1 and x2, element-wise. This is a scalar if both x1 and x2 are scalars.

import numpy as np
a = np.array([-1,-2,0,1,2,3])
print(np.minimum(a, 0))

结果为
[-1 -2  0  0  0  0]

相对应的有maximum 

import numpy as np
a = np.array([-1,-2,0,1,2,3])
print(np.maximum(a, 0))
结果为:
array([0, 0, 0, 1, 2, 3])

print(np.maximum(a, -a))
结果为:
array([1, 2, 0, 1, 2, 3])

print(np.maximum(a, -a)) # 这步就相当于取绝对值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值