[Numpy-discussion] Adding .abs() method to the array object
Sebastian Berg
sebastian@sipsolutions....
Tue Feb 26 04:43:45 CST 2013
On Tue, 2013-02-26 at 11:16 +0100, Todd wrote:
> On Tue, Feb 26, 2013 at 10:58 AM, Sebastian Berg
> <sebastian@sipsolutions.net> wrote:
> On Mon, 2013-02-25 at 22:04 -0500, josef.pktd@gmail.com wrote:
> > On Mon, Feb 25, 2013 at 9:58 PM, <josef.pktd@gmail.com>
> wrote:
> > > 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:
> > >>> >
>
> <snip>
> > >> 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'
> >
> > or maybe more useful
> >
> > >>> from decimal import Decimal
> > >>> d = [Decimal(str(k)) for k in np.linspace(-1, 1, 5)]
> > >>> map(abs, d)
> > [Decimal('1.0'), Decimal('0.5'), Decimal('0.0'),
> Decimal('0.5'), Decimal('1.0')]
> >
> > >>> np.asarray(d).__abs__()
> > array([1.0, 0.5, 0.0, 0.5, 1.0], dtype=object)
> > >>> np.asarray(d).__abs__()[0]
> > Decimal('1.0')
> >
> > Josef
> >
> > >
> > > I don't see a difference.
> > >
> > > (I don't expect to use max abs on anything else than
> numbers.)
> > >
>
>
> The difference is about scalars only. And of course __abs__ is
> fine, but
> if numpy adds an abs method, its scalars would logically have
> it too.
> But then you diverge from python scalars. That has exactly the
> downside
> that you may write code that suddenly stops working for python
> scalars
> without noticing.
>
> I turned around the abs and max order here, so that the abs
> works on the
> scalar, not useful but just as an example.
>
>
> But doesn't this also apply to many existing methods?
>
I do not think that it does, or at a different quality. Almost all of
those methods either logically work on the container not the array. I.e.
reshape, etc. or are the most common reductions like mean/max/min (which
are also only sensible for containers). Now those also work on numpy
scalars but you should rarely use them on scalars. The only example I
can see is round (there is no special method for that before python 3,
so you could argue that python did not provide a canonical way for
numpy).
Now this is not a huge argument, obviously scalars can have a lot of
specialized methods (like decimals for example), but in the case of abs,
python provides a default way of doing it which always works, and it
makes me tend to say that it is better to use that (even if I sometimes
write .abs() too...).
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
More information about the NumPy-Discussion
mailing list