numpy.ma.ptp#
- ma.ptp(obj,axis=None,out=None,fill_value=None,keepdims=<novalue>)[source]#
Return (maximum - minimum) along the given dimension(i.e. peak-to-peak value).
Warning
ptppreserves the data type of the array. This means thereturn value for an input of signed integers with n bits(e.g.np.int8,np.int16, etc) is also a signed integerwith n bits. In that case, peak-to-peak values greater than2**(n-1)-1will be returned as negative values. An examplewith a work-around is shown below.- Parameters:
- axis{None, int}, optional
Axis along which to find the peaks. If None (default) theflattened array is used.
- out{None, array_like}, optional
Alternative output array in which to place the result. It musthave the same shape and buffer length as the expected outputbut the type will be cast if necessary.
- fill_valuescalar or None, optional
Value used to fill in the masked values.
- keepdimsbool, optional
If this is set to True, the axes which are reduced are leftin the result as dimensions with size one. With this option,the result will broadcast correctly against the array.
- Returns:
- ptpndarray.
A new array holding the result, unless
outwasspecified, in which case a reference tooutis returned.
Examples
>>>importnumpyasnp>>>x=np.ma.MaskedArray([[4,9,2,10],...[6,9,7,12]])
>>>x.ptp(axis=1)masked_array(data=[8, 6], mask=False, fill_value=999999)
>>>x.ptp(axis=0)masked_array(data=[2, 0, 5, 2], mask=False, fill_value=999999)
>>>x.ptp()10
This example shows that a negative value can be returned whenthe input is an array of signed integers.
>>>y=np.ma.MaskedArray([[1,127],...[0,127],...[-1,127],...[-2,127]],dtype=np.int8)>>>y.ptp(axis=1)masked_array(data=[ 126, 127, -128, -127], mask=False, fill_value=np.int64(999999), dtype=int8)
A work-around is to use theview() method to view the result asunsigned integers with the same bit width:
>>>y.ptp(axis=1).view(np.uint8)masked_array(data=[126, 127, 128, 129], mask=False, fill_value=np.uint64(999999), dtype=uint8)