Questions on migrating from Numeric/Scipy to Numpy

V

vj

I've tried to post this to the numpy google group but it seems to be
down. My migration seems to be going well. I currently have one issue
with using scipy_base.insert.
a = zeros(5)
mask = zeros(5)
mask[1] = 1
c = zeros(1)
c[0] = 100
numpy.insert(a, mask, c)
array([ 100., 0., 100., 100., 100., 0., 0., 0.,
0., 0.])
a array([ 0., 0., 0., 0., 0.])
b array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1], dtype=int8)
mask array([ 0., 1., 0., 0., 0.])
c
array([ 100.])

I would have expected numpy.insert to update a so that the second
element in a would be 100.

Thanks,

VJ
 
R

Robert Kern

vj said:
I've tried to post this to the numpy google group but it seems to be
down.

It is just a redirection to the (e-mail address removed) list. If you just
tried in the past hour or so, I've discovered that our DNS appears to be down
right now.
My migration seems to be going well. I currently have one issue
with using scipy_base.insert.
a = zeros(5)
mask = zeros(5)
mask[1] = 1
c = zeros(1)
c[0] = 100
numpy.insert(a, mask, c)
array([ 100., 0., 100., 100., 100., 0., 0., 0.,
0., 0.])
a array([ 0., 0., 0., 0., 0.])
b array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1], dtype=int8)
mask array([ 0., 1., 0., 0., 0.])
c
array([ 100.])

I would have expected numpy.insert to update a so that the second
element in a would be 100.

No, that's not what insert() does. See the docstring:

In [1]: from numpy import *

In [2]: insert?
Type: function
Base Class: <type 'function'>
Namespace: Interactive
File:
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/numpy-1.0.2.dev3569-py2.5-macosx-10.3-fat.egg/numpy/lib/function_base.py
Definition: insert(arr, obj, values, axis=None)
Docstring:
Return a new array with values inserted along the given axis
before the given indices

If axis is None, then ravel the array first.

The obj argument can be an integer, a slice, or a sequence of
integers.

Example:
... [4,5,6],
... [7,8,9]])
>>> insert(a, [1,2], [[4],[5]], axis=0)
array([[1, 2, 3],
[4, 4, 4],
[4, 5, 6],
[5, 5, 5],
[7, 8, 9]])


The behaviour that you seem to want would be accomplished with the following:

In [3]: a = zeros(5)

In [4]: mask = zeros(5, dtype=bool)

In [5]: mask[1] = True

In [6]: mask
Out[6]: array([False, True, False, False, False], dtype=bool)

In [7]: a[mask] = 100

In [8]: a
Out[8]: array([ 0., 100., 0., 0., 0.])


Note that the mask needs to be a bool array.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
V

vj

It is just a redirection to the (e-mail address removed) list. If you just
tried in the past hour or so, I've discovered that our DNS appears to be down
right now.

I tried registering the twice the last couple of days and never got an
email back.
No, that's not what insert() does. See the docstring:

Then this behavior is different from the scipy_base.insert
In [7]: a[mask] = 100

What I needed was

a[mask] = array_of_values

I just tried it and it works.

Thanks,

VJ
 
V

vj

Note that the mask needs to be a bool array.
mask = zeros(5)
mask = zeros(5, numpy.int8)
mask[1] = True
mask[2] = True
a = zeros(5)
a[mask] = [100, 200]
a
array([ 100., 100., 0., 0., 0.])

I found this strange. It should just give an error if you try to use a
mask array of non booleans. It's working great so far.

Thanks for all the hard work.

VJ
 
R

Robert Kern

vj said:
I tried registering the twice the last couple of days and never got an
email back.

Hmm. Odd. I just tried to register and haven't gotten a confirmation email, yet,
either. I'll get our sysadmin on it.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
R

Robert Kern

vj said:
Note that the mask needs to be a bool array.
mask = zeros(5)
mask = zeros(5, numpy.int8)
mask[1] = True
mask[2] = True
a = zeros(5)
a[mask] = [100, 200]
a
array([ 100., 100., 0., 0., 0.])

I found this strange. It should just give an error if you try to use a
mask array of non booleans.

No, it simply does something different. Boolean arrays apply as a mask. Integer
arrays apply as indices into the array.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
V

vj

What should I be using to replace Numeric/arrayobject.h:

numpy/arrayobject.h

or

numpy/oldnumeric.h

Thanks,

VJ
 
R

Robert Kern

vj said:
What should I be using to replace Numeric/arrayobject.h:

numpy/arrayobject.h

or

numpy/oldnumeric.h

Replacing "numpy/oldnumeric.h" is the compatibility header. If you don't want to
convert your code to use the new APIs (and you might; it is much improved), then
that should be all that you need to do to get your old extension modules running
with numpy.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,128
Latest member
ElwoodPhil
Top