469,364 Members | 2,398 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,364 developers. It's quick & easy.

Questions on migrating from Numeric/Scipy to Numpy

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

Mar 14 '07 #1
7 1752
vj wrote:
I've tried to post this to the numpy google group but it seems to be
down.
It is just a redirection to the nu**************@scipy.org 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:
>>a = array([[1,2,3],
... [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

Mar 14 '07 #2
vj
It is just a redirection to the numpy-discuss...@scipy.org 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

Mar 14 '07 #3
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

Mar 14 '07 #4
vj wrote:
>It is just a redirection to the numpy-discuss...@scipy.org 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.
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

Mar 14 '07 #5
vj wrote:
>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

Mar 14 '07 #6
vj
What should I be using to replace Numeric/arrayobject.h:

numpy/arrayobject.h

or

numpy/oldnumeric.h

Thanks,

VJ

Mar 14 '07 #7
vj wrote:
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

Mar 14 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by jantod | last post: by
10 posts views Thread by Bryan | last post: by
2 posts views Thread by robert | last post: by
18 posts views Thread by robert | last post: by
4 posts views Thread by HYRY | last post: by
5 posts views Thread by auditory | last post: by
5 posts views Thread by Erik Johnson | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.