error using all()/numpy [TypeError: cannot perform reduce withflexible type]

Discussion in 'Python' started by Marc Oldenhof, May 23, 2008.

  1. Hello all,

    I'm pretty new to Python, but use it a lot lately. I'm getting a crazy
    error trying to do operations on a string list after importing numpy.
    Minimal example:

    [start Python]

    Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit
    (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.

    >>> a=['1','2','3']
    >>> all(a)

    True
    >>> from numpy import *
    >>> all(a)

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "C:\Python25\lib\site-packages\numpy\core\fromnumeric.py", line
    982, in all
    return _wrapit(a, 'all', axis, out)
    File "C:\Python25\lib\site-packages\numpy\core\fromnumeric.py", line
    37, in _wrapit
    result = getattr(asarray(obj),method)(*args, **kwds)
    TypeError: cannot perform reduce with flexible type

    [end of example]

    It seems that Python calls numpy's "all" instead of the standard one,
    is that right? If so, how can I call the standard "all" after the
    numpy import? ["import numpy" is not a desirable option, I use a lot
    of math in my progs]

    Is there another way around this error?

    Marc
    Marc Oldenhof, May 23, 2008
    #1
    1. Advertising

  2. On 23 mei, 09:12, Marc Oldenhof <> wrote:
    > Hello all,
    >
    > I'm pretty new to Python, but use it a lot lately. I'm getting a crazy
    > error trying to do operations on a string list after importing numpy.
    > Minimal example:
    >
    > [start Python]
    >
    > Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit
    > (Intel)] on win32
    > Type "help", "copyright", "credits" or "license" for more information.
    >
    > >>> a=['1','2','3']
    > >>> all(a)

    > True
    > >>> from numpy import *
    > >>> all(a)

    >
    > Traceback (most recent call last):
    >   File "<stdin>", line 1, in <module>
    >   File "C:\Python25\lib\site-packages\numpy\core\fromnumeric.py", line
    > 982, in all
    >     return _wrapit(a, 'all', axis, out)
    >   File "C:\Python25\lib\site-packages\numpy\core\fromnumeric.py", line
    > 37, in _wrapit
    >     result = getattr(asarray(obj),method)(*args, **kwds)
    > TypeError: cannot perform reduce with flexible type
    >
    > [end of example]
    >
    > It seems that Python calls numpy's "all" instead of the standard one,
    > is that right? If so, how can I call the standard "all" after the
    > numpy import? ["import numpy" is not a desirable option, I use a lot
    > of math in my progs]
    >
    > Is there another way around this error?
    >
    > Marc


    Never mind, I found a way. For reference:

    >>> import __builtin__
    >>> __builtin__.all(a)

    True

    Works!

    Marc
    Marc Oldenhof, May 23, 2008
    #2
    1. Advertising

  3. Marc Oldenhof

    Gary Herron Guest

    Marc Oldenhof wrote:
    > Hello all,
    >
    > I'm pretty new to Python, but use it a lot lately. I'm getting a crazy
    > error trying to do operations on a string list after importing numpy.
    > Minimal example:
    >
    > [start Python]
    >
    > Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit
    > (Intel)] on win32
    > Type "help", "copyright", "credits" or "license" for more information.
    >
    >
    >>>> a=['1','2','3']
    >>>> all(a)
    >>>>

    > True
    >
    >>>> from numpy import *
    >>>> all(a)
    >>>>

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    > File "C:\Python25\lib\site-packages\numpy\core\fromnumeric.py", line
    > 982, in all
    > return _wrapit(a, 'all', axis, out)
    > File "C:\Python25\lib\site-packages\numpy\core\fromnumeric.py", line
    > 37, in _wrapit
    > result = getattr(asarray(obj),method)(*args, **kwds)
    > TypeError: cannot perform reduce with flexible type
    >
    > [end of example]
    >
    > It seems that Python calls numpy's "all" instead of the standard one,
    > is that right? If so, how can I call the standard "all" after the
    > numpy import? ["import numpy" is not a desirable option, I use a lot
    > of math in my progs]
    >
    > Is there another way around this error?
    >


    Yes, there are several solutions, but before that I'll say that "from
    .... import *" is frowned upon for just this reason. You have no
    control (and often no idea) what * ends up importing, and if any of
    those names overwrite an existing name (as you've found here), you may
    not notice. (It's not quite fair to say "Python calls numpy's "all".
    *You* call it after you chose to replace Python's "all" with numpy's "all".)

    Solutions:



    Save Python's "all" first under another name:
    original_all = all
    from numpy import all
    Now you can call all(...) or original_all(...).



    The built-in (as they are called) are always available through __builtins__:
    from numpy import *
    all(...) # is numpy's all
    __builtins__.all(...) # Is the original all




    Don't import *, but rather import only those things you need.
    from numpy import array, dot, float32, int32, ...
    and if you need numpy's all
    from numpy import all as numpy_all


    Gary Herron




    > Marc
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    Gary Herron, May 23, 2008
    #3
  4. Marc Oldenhof

    Peter Otten Guest

    Re: error using all()/numpy [TypeError: cannot perform reduce with flexible type]

    Marc Oldenhof wrote:

    > I'm pretty new to Python, but use it a lot lately. I'm getting a crazy
    > error trying to do operations on a string list after importing numpy.
    > Minimal example:
    >
    > [start Python]
    >
    > Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit
    > (Intel)] on win32
    > Type "help", "copyright", "credits" or "license" for more information.
    >
    >>>> a=['1','2','3']
    >>>> all(a)

    > True
    >>>> from numpy import *
    >>>> all(a)

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    > File "C:\Python25\lib\site-packages\numpy\core\fromnumeric.py", line
    > 982, in all
    > return _wrapit(a, 'all', axis, out)
    > File "C:\Python25\lib\site-packages\numpy\core\fromnumeric.py", line
    > 37, in _wrapit
    > result = getattr(asarray(obj),method)(*args, **kwds)
    > TypeError: cannot perform reduce with flexible type
    >
    > [end of example]
    >
    > It seems that Python calls numpy's "all" instead of the standard one,
    > is that right? If so, how can I call the standard "all" after the
    > numpy import? ["import numpy" is not a desirable option, I use a lot
    > of math in my progs]
    >
    > Is there another way around this error?


    That's not an error; star-import is a rebinding operation, just like
    assignments and the def statement.

    >>> from numpy import *
    >>> del all, sum, any
    >>> all("123")

    True

    Peter
    Peter Otten, May 23, 2008
    #4
  5. Marc Oldenhof

    I V Guest

    On Fri, 23 May 2008 00:12:35 -0700, Marc Oldenhof wrote:
    > It seems that Python calls numpy's "all" instead of the standard one, is
    > that right? If so, how can I call the standard "all" after the numpy
    > import? ["import numpy" is not a desirable option, I use a lot of math
    > in my progs]


    I think the ideal solution is to try and persuade yourself that typing
    "numpy." in front of some functions now and then is not that big a price
    to pay to avoid name collisions; using an editor with code completion
    would probably help in that task.

    You could also try:

    import numpy as n

    which reduces the typing a bit and limits you to one possible name
    collision, or

    from numpy import array, gradient #and whatever else you need

    or

    import numpy

    array = numpy.array
    gradient = numpy.gradient # Then you can access the names you use a lot
    # directly, while accessing stuff you use less
    # frequently via numpy.whatever

    in which case you'll know exactly which names you're overwriting. Peter's
    solution, of explicitly deleting some names that you've imported, strikes
    me as less good, because you might find the same problem recurs later, if
    the numpy developers add new names to the module.
    I V, May 23, 2008
    #5
  6. On 23 mei, 09:12, Marc Oldenhof <> wrote:
    <snip my post>

    Thanks for the reactions, I'll use the "from numpy import <individual
    stuff>" from now on :)

    Marc
    Marc Oldenhof, May 23, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. drife
    Replies:
    1
    Views:
    359
    Travis E. Oliphant
    Mar 1, 2006
  2. Duncan Smith
    Replies:
    3
    Views:
    416
    Duncan Smith
    Apr 25, 2007
  3. Replies:
    2
    Views:
    486
    Robert Kern
    Nov 13, 2007
  4. Slaunger
    Replies:
    3
    Views:
    437
    Slaunger
    Nov 14, 2008
  5. W. eWatson
    Replies:
    2
    Views:
    928
    W. eWatson
    Nov 23, 2009
Loading...

Share This Page