[Numpy-discussion] Adding .abs() method to the array object
josef.pktd@gmai...
josef.pktd@gmai...
Mon Feb 25 20:58:01 CST 2013
On Mon, Feb 25, 2013 at 9:20 PM, Sebastian Berg
<sebastian@sipsolutions.net> wrote:
> On Mon, 2013-02-25 at 10:50 -0500, Skipper Seabold wrote:
>> On Mon, Feb 25, 2013 at 10:43 AM, Till Stensitzki <mail.till@gmx.de>
>> wrote:
>> >
>> > First, sorry that i didnt search for an old thread, but because i
>> disagree with
>> > conclusion i would at least address my reason:
>> >
>> >> I don't like
>> >> np.abs(arr).max()
>> >> because I have to concentrate to much on the braces, especially if
>> arr
>> >> is a calculation
>> >
>> > This exactly, adding an abs into an old expression is always a
>> little annoyance
>> > due to the parenthesis. The argument that np.abs() also works is
>> true for
>> > (almost?) every other method. The fact that so many methods already
>> exists,
>> > especially for most of the commonly used functions (min, max, dot,
>> mean, std,
>> > argmin, argmax, conj, T) makes me missing abs. Of course, if one
>> would redesign
>> > the api, one would drop most methods (i am looking at you ptp and
>> byteswap). But
>> > the objected is already cluttered and adding abs is imo logical
>> application of
>> > "practicality beats purity".
>> >
>>
>> I tend to agree here. The situation isn't all that dire for the number
>> of methods in an array. No scrolling at reasonably small terminal
>> sizes.
>>
>> [~/]
>> [3]: x.
>> x.T x.copy x.getfield x.put x.std
>> x.all x.ctypes x.imag x.ravel
>> x.strides
>> x.any x.cumprod x.item x.real x.sum
>> x.argmax x.cumsum x.itemset x.repeat
>> x.swapaxes
>> x.argmin x.data x.itemsize x.reshape x.take
>> x.argsort x.diagonal x.max x.resize
>> x.tofile
>> x.astype x.dot x.mean x.round
>> x.tolist
>> x.base x.dtype x.min x.searchsorted
>> x.tostring
>> x.byteswap x.dump x.nbytes x.setfield
>> x.trace
>> x.choose x.dumps x.ndim x.setflags
>> x.transpose
>> x.clip x.fill x.newbyteorder x.shape x.var
>> x.compress x.flags x.nonzero x.size x.view
>> x.conj x.flat x.prod x.sort
>> x.conjugate x.flatten x.ptp x.squeeze
>>
>>
> Two small things (not sure if it matters much). But first almost all of
> these methods are related to the container and not the elements. Second
> actually using a method arr.abs() has a tiny pitfall, since abs would
> work on numpy types, but not on python types. This means that:
>
> np.array([1, 2, 3]).max().abs()
>
> works, but
>
> np.array([1, 2, 3], dtype=object).max().abs()
>
> breaks. Python has a safe name for abs already...
>>> (np.array([1, 2, 3], dtype=object)).max()
3
>>> (np.array([1, 2, 3], dtype=object)).__abs__().max()
3
>>> (np.array([1, 2, '3'], dtype=object)).__abs__()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: bad operand type for abs(): 'str'
>>> map(abs, [1, 2, 3])
[1, 2, 3]
>>> map(abs, [1, 2, '3'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: bad operand type for abs(): 'str'
I don't see a difference.
(I don't expect to use max abs on anything else than numbers.)
Josef
>
>
>> I find myself typing things like
>>
>> arr.abs()
>>
>> and
>>
>> arr.unique()
>>
>> quite often.
>>
>> Skipper
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion@scipy.org
>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
More information about the NumPy-Discussion
mailing list