bug when using numpy.percentile

111 views
Skip to first unread message

Stan

unread,
Feb 25, 2015, 11:13:35 AM2/25/15
to sage-...@googlegroups.com
Dear all,

I just ran into a bug in sage, which is reproducible both in my local Sage 6.3, 6.5 and on sage-cloud using the sage notebook but not when using iPython, i.e. it seems to really be a sage bug, not a numpy bug.
Any ideas what might be the reason?

Just using the syntax from the numpy.percentile docstring gives:
┌────────────────────────────────────────────────────────────────────┐
│ Sage Version 6.3, Release Date: 2014-08-10                         │
│ Type "notebook()" for the browser-based notebook interface.        │
│ Type "help()" for help.                                            │
└────────────────────────────────────────────────────────────────────┘
sage: import numpy as np
sage: a = np.array([[10, 7, 4], [3, 2, 1]])
sage: np.percentile(a, 75)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-54a2b61d0a48> in <module>()
----> 1 np.percentile(a, Integer(75))

/home/sschyman/Programs/sage/local/lib/python2.7/site-packages/numpy/lib/function_base.py in percentile(a, q, axis, out, overwrite_input)
   2818         axis = 0
   2819
-> 2820     return _compute_qth_percentile(sorted, q, axis, out)
   2821
   2822 # handle sequence of q's without calling sort multiple times

/home/sschyman/Programs/sage/local/lib/python2.7/site-packages/numpy/lib/function_base.py in _compute_qth_percentile(sorted, q, axis, out)
   2824     if not isscalar(q):
   2825         p = [_compute_qth_percentile(sorted, qi, axis, None)
-> 2826              for qi in q]
   2827
   2828         if out is not None:

TypeError: 'sage.rings.integer.Integer' object is not iterable

Cheers
Stan

Thierry

unread,
Feb 25, 2015, 11:19:27 AM2/25/15
to sage-...@googlegroups.com
Hi,

The reason is that 75 is preparsed into a Sage integer. If you want to use
Python int, just do:

sage: np.percentile(a, 75r)
6.25

or

sage: np.percentile(a, int(75))
6.25

If you want to turn Sage preparser off, just do

sage: preparser(False)
sage: np.percentile(a, 75)
6.25

Note that http://ask.sagemath.org is more appropriate for such questions.

Ciao,
Thierry
> --
> You received this message because you are subscribed to the Google Groups "sage-devel" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+...@googlegroups.com.
> To post to this group, send email to sage-...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sage-devel.
> For more options, visit https://groups.google.com/d/optout.

Stan

unread,
Mar 5, 2015, 4:53:47 AM3/5/15
to sage-...@googlegroups.com
Thanks, Thierry!
I already found an alternative way to do what I wanted, using

from scipy.stats.mstats import mquantiles

However, I thought that the examples in the documentation should work in the sage notebook without fundamentally changing the syntax, and hence I considered this a bug. Otherwise I would have asked the question on ask.sagemath, as you suggested.
I think I have run into similar problems when using sage functions on numpy objects or vice versa before. If preparser(False) is a general solution to avoid similar problems, should this be included in the documentation somewhere?

Cheers,
Stan
Reply all
Reply to author
Forward
0 new messages