Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > numpy migration (also posted to numpy-discussion)

Reply
Thread Tools

numpy migration (also posted to numpy-discussion)

 
 
Duncan Smith
Guest
Posts: n/a
 
      04-23-2007
Hello,
Since moving to numpy I've had a few problems with my existing
code. It basically revolves around the numpy scalar types. e.g.

------------------------------------------------
>>> import Numeric as N
>>> a = N.array([[0,1],[2,3]])
>>> a

array([[0, 1],
[2, 3]])
>>> i = a[0,0]
>>> 1/i


Traceback (most recent call last):
File "<pyshell#30>", line 1, in -toplevel-
1/i
ZeroDivisionError: integer division or modulo by zero
>>> b = a * 1.5
>>> b

array([[ 0. , 1.5],
[ 3. , 4.5]])
>>> N.floor(b)

array([[ 0., 1.],
[ 3., 4.]])
>>> ================================ RESTART

================================
>>> import numpy as N
>>> a = N.array([[0,1],[2,3]])
>>> a

array([[0, 1],
[2, 3]])
>>> i = a[0,0]
>>> 1/i

0
>>> b = a * 1.5
>>> b

array([[ 0. , 1.5],
[ 3. , 4.5]])
>>> N.floor(b)

array([[ 0., 1.],
[ 3., 4.]])
>>> a = N.array([[0,1],[2,3]], dtype='O')
>>> a

array([[0, 1],
[2, 3]], dtype=object)
>>> i = a[0,0]
>>> 1/i


Traceback (most recent call last):
File "<pyshell#45>", line 1, in -toplevel-
1/i
ZeroDivisionError: integer division or modulo by zero
>>> b = a * 1.5
>>> b

array([[0.0, 1.5],
[3.0, 4.5]], dtype=object)
>>> N.floor(b)


Traceback (most recent call last):
File "<pyshell#48>", line 1, in -toplevel-
N.floor(b)
AttributeError: 'float' object has no attribute 'floor'
>>>

----------------------------------------------

An additional problem involves classes that have e.g. __rmul__ methods
defined and are sufficiently similar to numpy arrays that my classes'
__rmul__ methods are not invoked when using numpy scalars.


Using the 'O' dtype gives me Python types that raise zero division
errors appropriately (for my code) and the desired calls to e.g.
__rmul__ methods, but reduced functionality in other repects.

I might (I hope) be missing something obvious; but it seems like, to be
safe, I'm going to have to do a lot of explicit conversions to Python
types (or abandon catching zero division errors, and documenting some of
my classes to highlight that whether scalar * a equals a * scalar
depends on whether a.__rmul__ is called, which depends on the type of
scalar).

I suppose I might get round both issues by subclassing existing numpy
dtypes. Any ideas? Cheers. TIA.

Duncan
 
Reply With Quote
 
 
 
 
Travis E. Oliphant
Guest
Posts: n/a
 
      04-24-2007
Duncan Smith wrote:
> Hello,
> Since moving to numpy I've had a few problems with my existing
> code. It basically revolves around the numpy scalar types. e.g.
>


You will probably get more help on the numpy discussion list:

http://www.velocityreviews.com/forums/(E-Mail Removed)


You are encountering problems because numpy scalar types don't raise
errors (unless you have set the appropriate hardware flag using
numpy.seterr).

You can get Python scalars out of NumPy arrays if you really want them
using (for example...)

a.item(0,0)


>
> An additional problem involves classes that have e.g. __rmul__ methods
> defined and are sufficiently similar to numpy arrays that my classes'
> __rmul__ methods are not invoked when using numpy scalars.
>


Could you please post an example showing the problem?

>
> I might (I hope) be missing something obvious; but it seems like, to be
> safe, I'm going to have to do a lot of explicit conversions to Python
> types (or abandon catching zero division errors, and documenting some of
> my classes to highlight that whether scalar * a equals a * scalar
> depends on whether a.__rmul__ is called, which depends on the type of
> scalar).
>


numpy scalars are try a lot more things before giving up on
multiplication and letting the other class have a stab at it.

Post your problems to the numpy discussion list for better help and more
discussion.


-Travis

 
Reply With Quote
 
 
 
 
Duncan Smith
Guest
Posts: n/a
 
      04-24-2007
Travis E. Oliphant wrote:
> Duncan Smith wrote:
>
>> Hello,
>> Since moving to numpy I've had a few problems with my existing
>> code. It basically revolves around the numpy scalar types. e.g.
>>

>
> You will probably get more help on the numpy discussion list:
>
> (E-Mail Removed)
>
>
> You are encountering problems because numpy scalar types don't raise
> errors (unless you have set the appropriate hardware flag using
> numpy.seterr).
>


Aha!

> You can get Python scalars out of NumPy arrays if you really want them
> using (for example...)
>
> a.item(0,0)
>
>
>>
>> An additional problem involves classes that have e.g. __rmul__ methods
>> defined and are sufficiently similar to numpy arrays that my classes'
>> __rmul__ methods are not invoked when using numpy scalars.
>>

>
> Could you please post an example showing the problem?
>


I'll try to post a minimal example tomorrow. But they are classes that
have an ndarray as an attribute, and with __getitem__ and __setitem__
methods which simply call the corresponding array methods. Maybe that's
enough to account for the behaviour? I'll check tomorrow.

>>
>> I might (I hope) be missing something obvious; but it seems like, to be
>> safe, I'm going to have to do a lot of explicit conversions to Python
>> types (or abandon catching zero division errors, and documenting some of
>> my classes to highlight that whether scalar * a equals a * scalar
>> depends on whether a.__rmul__ is called, which depends on the type of
>> scalar).
>>

>
> numpy scalars are try a lot more things before giving up on
> multiplication and letting the other class have a stab at it.
>
> Post your problems to the numpy discussion list for better help and more
> discussion.
>


Yes, I have done. But it's awaiting moderation; presumably because I
posted using a different e-mail address than the one I registered with
(I wasn't thinking). Thanks for the reply.

Duncan
 
Reply With Quote
 
Duncan Smith
Guest
Posts: n/a
 
      04-25-2007
Travis E. Oliphant wrote:
> Duncan Smith wrote:
>
>> Hello,
>> Since moving to numpy I've had a few problems with my existing
>> code. It basically revolves around the numpy scalar types. e.g.
>>

>
> You will probably get more help on the numpy discussion list:
>
> (E-Mail Removed)
>
>
> You are encountering problems because numpy scalar types don't raise
> errors (unless you have set the appropriate hardware flag using
> numpy.seterr).
>


Unfortunately it seems to raise a FloatingPointError.

>>> import numpy as N
>>> N.__version__

'1.0.1'
>>> a = N.array([[0,1],[2,3]])
>>> a

array([[0, 1],
[2, 3]])
>>> i = a[0,0]
>>> 1/i

0
>>> N.seterr(divide='raise')

{'over': 'print', 'divide': 'print', 'invalid': 'print', 'under': 'ignore'}
>>> 1/i


Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
1/i
FloatingPointError: divide by zero encountered in long_scalars

> You can get Python scalars out of NumPy arrays if you really want them
> using (for example...)
>
> a.item(0,0)
>
>
>>
>> An additional problem involves classes that have e.g. __rmul__ methods
>> defined and are sufficiently similar to numpy arrays that my classes'
>> __rmul__ methods are not invoked when using numpy scalars.
>>

>
> Could you please post an example showing the problem?
>


[snip]

-----------------example.py--------------------

from __future__ import division

import numpy

class MyClass(object):

def __init__(self, arr, labels):
self.arr = arr
self.labels = labels

def __repr__(self):
return numpy.array2string(self.arr, separator=', ') +
repr(self.labels)

def __len__(self):
return len(self.labels)

def __getitem__(self, key):
return self.arr[key]

def __setitem__(self, key, item):
self.arr[key] = item

def __mul__(self, other):
return self.__class__(self.arr * other, self.labels)

__rmul__ = __mul__

----------------------------------------------------

>>> import example
>>> import numpy as N
>>> ex = example.MyClass(N.array([[6,7],[8,9]]), ['axis0', 'axis1'])
>>> i = ex.arr[0,0]
>>> ex

[[6, 7],
[8, 9]]['axis0', 'axis1']
>>> ex * i

[[36, 42],
[48, 54]]['axis0', 'axis1']
>>> i * ex

array([[36, 42],
[48, 54]])
>>>



It seems that it requires having __len__, __setitem__ and __getitem__
defined to get the undesired behaviour. Cheers.

Duncan
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sum returns numpy.float64 when applied to a sequence of numpy.uint64 suzaku Python 0 12-15-2012 09:40 AM
Installing numpy over an older numpy Tom Kacvinsky Python 1 06-15-2012 07:15 PM
A More Concise Description of Numpy than the Guide to Numpy? W. eWatson Python 2 11-23-2009 08:58 PM
NumPy Question - numpy.put in multi-dimensional array Bryan.Fodness@gmail.com Python 2 11-13-2007 10:36 PM
Trouble with numpy-0.9.4 and numpy-0.9.5 drife Python 1 03-01-2006 05:59 PM



Advertisments