Hi. This is a very basic question, sorry if it's irritating. If i
didn't find the answer written already somewhere on the site, please
point me to it. That'd be great.
OK: how do i iterate over an axis other than 0?
I have a 3D array of data[year, week, location]. I want to iterate
over each year at each location and run a series of stats on the
columns (on the 52 weeks in a particular year at a particular location).
'for years in data:' will get the first one, but then how do i not
iterate over the 1 axis and iterate over the 2 axis instead?
thanks,
alex.

David, in order to put clear what I was proposing to you in previous
mail regarding to implementing plugin systems for numpy, please take a
look at the attached tarball.
The plugins are in charge of implementing the action of generic foo()
and bar() functions in C. The example actually implements two
different plugins in Cython. There is also an include file with some
support declarations and some macro definitions wrap_foo and wrap_bar
Next, in mymodule.pyx, I define python side foo() and bar() functions
(calling funtins pointers in a struct via the macros wrap_xxx) and a
use() for selecting the plugin at runtime.
Finally, the test.py script is pure python code showing all this in
action. If you want to give a try, then install Cython, next do
'make', and finally 'python test.py'
Please note that I've implemented this is Cython just for convenience,
it does not actually depend in any special Cython feature, and could
be translated to pure C code with Python C/API calls.
IMHO, this is the easier and cleaner way to deal inside python with
plugins written in low-level C. It does not depend explicitely on
'dlopen' stuff from your side.
Now tell me. What is in your mind that this is not general/robust
enough as to have to explicitely deal with dlopen? It even works on
systems with no dynload, provided that all is put inside the python
executable.
--
Lisandro Dalcín
---------------
Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC)
Instituto de Desarrollo Tecnológico para la Industria Química (INTEC)
Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET)
PTLC - Güemes 3450, (3000) Santa Fe, Argentina
Tel/Fax: +54-(0)342-451.1594

Hello,
I have a quick question that I'm hoping will improve my numpy
understanding. I noticed some behavior when using float64 to convert
a matrix type that I didn't expect:
In [35]: b1 = array([1.0])
In [36]: float64(b1)
Out[36]: 1.0
In [37]: b2 = array([1.0, 2.0])
In [38]: float64(b2)
Out[38]: array([ 1., 2.])
I didn't expect calling float64 would convert b1 to a scalar. Seems
like an inconsistency. I assume this is intentional, as someone would
have noticed it a long time ago if not, so could someone explain the
reasoning behind it? (or point me to a source that will help?)
Thanks!
--Hoyt
--
+++++++++++++++++++++++++++++++++++
Hoyt Koepke
UBC Department of Computer Science
http://www.cs.ubc.ca/~hoytak/
hoytak(a)gmail.com
+++++++++++++++++++++++++++++++++++

Hi folks,
Someone on the wxPython list posted a nifty recarray example that I
don't quite understand. The idea is to have an array for an RGBA image:
rgbarec = numpy.dtype({'r':(numpy.uint8,0),
'g':(numpy.uint8,1),
'b':(numpy.uint8,2),
'a':(numpy.uint8,3)})
A = numpy.zeros(shape, dtype=(numpy.uint32, rgbarec) )
what I don't understand is having BOTH numpy.uint32 and rgbrec as the
dtype. How does that work?
Actually, with a bit of testing, it's pretty cool -- if you index like:
A[i,j] ==> uint32
A['r'][i,j] ==> uint8
pretty cool really.
it seems that numpy is treating it as both a recarray and a regular
uint32 array, which makes some sense, but I'm still confused by the
semantics.
also:
>>> A
array([[4278190080,
...
[4278190080, 4278190080, 4278190080, 4278190080, 4278190080]],
dtype=uint32)
but:
>>> A.dtype
dtype(('>u4', [('r', '|u1'), ('g', '|u1'), ('b', '|u1'), ('a', '|u1')]))
so what is the dtype?
Also, I see advantages and disadvantages to either way. If you do:
A = numpy.zeros(shape, dtype=(numpy.uint32, rgbarec) )
then you can do:
A[i,j]['r'] to get the red value of a pixel
A[i,j] = (red, green, blue, alpha) to set a pixel
A[:,:] = (red, green, blue, alpha) to make the whole image one color
With the "dual dtype" approach:
A = numpy.zeros(shape, dtype=(numpy.uint32, rgbarec) )
You need to set the pixels separately:
A['r'][i,j] = red
B['r'][i,j] = green
...
or construct uint32 values:
C = numpy.uint32(red)
C += numpy.uint32(green) << 8
C += numpy.uint32(blue) << 16
Which is really ugly! (and I may not even have it right!). Is there a
better way?
One more idea -- is there a way to have two arrays, each with a
different dtype, that point to the same data? maybe:
RGBImage = numpy.zeros(shape, dtype=rgbarec )
IntImage = RGBImage.view()
IntImage.dtype = numpy.uint32
That way you could work with it either way, whichever was easier in the
context.
So -- what are folks' thoughts about how best to deal with images as
numpy arrays? (I'll put it in a Wiki page if I get some good comments)
-Chris
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker(a)noaa.gov

Hi Jarrod,
any news with the 1.0.5? If you have same prerelease, I'd like to test
it. Debian has just moved from python2.4 to python2.5 yesterday, so
I'd like to test numpy in advance, I am sure there will be some issues
to fix.
Ondrej

Hello i have a quick question about MA ans scalar
the following works:
import numpy.ma as MA
a0= MA.array(0)/1
a1= MA.array((0,0))/1
but not:
import numpy.ma as MA
a0= MA.array(0)/0
a1= MA.array((0,0))/0
Is that a bug ?
I'm using numpy 1.0.5.dev4958 and also whats in trunk right now
(1.1.0.dev5113)
C.

HI,
import numpy.oldnumeric.ma as MA
a0= MA.array(0)/0
sh0=list(a0.shape)
sh0.insert(0,1)
b0=MA.resize(a0,sh0)
Does not work anymore, I believe it used to work
It does works using numpy.ma (but i can't subclass these yet...)
C.

Greetings,
Just a reminder: the abstracts for the EuroSciPy Conference in Leipzig
are due by midnight tonight (CST, US [UTC -6]) April, 30. If you'd
like to present, please submit your abstract as a PDF, MS Word or
plain text file to euroabstracts(a)scipy.org.
For more information on the EuroSciPy Conference, please see:
http://www.scipy.org/EuroSciPy2008

As I was looking at Bill's conjugate gradient posting,
I found myself wondering if there would be a payoff
to an output argument for ``numpy.outer``. (It is fairly
natural to repeatedly recreate the outer product of
the adjusted residuals, which is only needed during a
single iteration.)
Cheers,
Alan Isaac

Timothy Hochberg has proposed a generalization of the matrix mechanism
to support manipulating arrays of linear algebra objects. For example,
one might have an array of matrices one wants to apply to an array of
vectors, to yield an array of vectors:
In [88]: A = np.repeat(np.eye(3)[np.newaxis,...],2,axis=0)
In [89]: A
Out[89]:
array([[[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]],
[[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]]])
In [90]: V = np.array([[1,0,0],[0,1,0]])
Currently, it is very clumsy to handle this kind of situation even
with arrays, keeping track of dimensions by hand. For example if one
wants to multiply A by V "elementwise", one cannot simply use dot:
In [92]: np.dot(A,V.T)
Out[92]:
array([[[ 1., 0.],
[ 0., 1.],
[ 0., 0.]],
[[ 1., 0.],
[ 0., 1.],
[ 0., 0.]]])
tensordot() suffers from the same problem. It arises because when you
combine two multiindexed objects there are a number of ways you can do
it:
A: Treat all indices as different and form all pairwise products
(outer product):
In [93]: np.multiply.outer(A,V).shape
Out[93]: (2, 3, 3, 2, 3)
B: Contract the outer product on a pair of indices:
In [98]: np.tensordot(A,V,axes=(-1,-1)).shape
Out[98]: (2, 3, 2)
C: Take the diagonal along a pair of indices:
In [111]: np.diagonal(np.multiply.outer(A,V),axis1=0,axis2=3).shape
Out[111]: (3, 3, 3, 2)
What we want in this case is a combination of B and C:
In [106]: np.diagonal(np.tensordot(A,V,axes=(-1,-1)),axis1=0,axis2=2).T.shape
Out[106]: (2, 3)
but it cannot be done without constructing a larger array and pulling
out its diagonal.
If this seems like an exotic thing to want to do, suppose instead we
have two arrays of vectors, and we want to evaluate the array of dot
products. None of no.dot, np.tensordot, or np.inner produce the
results you want. You have to multiply elementwise and sum. (This also
precludes automatic use of BLAS, if indeed tensordot uses BLAS.)
Does it make sense to implement a generalized tensordot that can do
this, or is it the sort of thing one should code by hand every time?
Is there any way we can make it easier to do simple common operations
like take the elementwise matrix-vector product of A and V?
The more general issue, of making linear algebra natural by keeping
track of which indices are for elementwise operations, and which
should be used for dots (and how), is a whole other kettle of fish. I
think for that someone should think hard about writing a full-featured
multilinear algebra package (might as well keep track of coordinate
transformations too while one was at it) if this is intended.
Anne