I want to apply a function (myfunc which takes and returns a scalar) to each
element in a multi-dimensioned array (data):
I can do this:
newdata = numpy.array([myfunc(d) for d in data.flat]).reshape(data.shape)
But I'm wondering if there's a faster more numpy way. I've looked at the
vectorize function but can't work it out.
View this message in context: http://www.nabble.com/Apply-a-function-to-an-array-elementwise-tp20823768p2…
Sent from the Numpy-discussion mailing list archive at Nabble.com.
Sorry to bump my own post, and I was kinda threadjacking anyway:
Some functions of numy.ma (eg, ma.max, ma.min...) accept explicit
outputs that may not be MaskedArrays.
When such an explicit output is not a MaskedArray, a value that should
have been masked is transformed into np.nan.
That worked great in 2.5, with np.nan automatically transformed to 0
when the explicit output had a int dtype. With Python 2.6, a
ValueError is raised instead, as np.nan can no longer be casted to int.
What should be the recommended behavior in this case ? Raise a
ValueError or some other exception, to follow the new Python2.6
convention, or silently replace np.nan by some value acceptable by int
dtype (0, or something else) ?
Thanks for any suggestion,
On Dec 4, 2008, at 2:03 PM, Robert Kern wrote:
> It does. What problems are people seeing? Is it just the Windows build
> that causes people to say "numpy doesn't work with Python 2.6"?
There is currently no official Mac OSX binary for numpy for python 2.6,
but you can build it from source. Is there any time table for generating
a 2.6 Mac OS X binary?
Suppose I have array a with dimensions (d1, d3) and array b with
dimensions (d2, d3). I want to compute array c with dimensions (d1,
d2) holding the squared euclidian norms of vectors in a and b with
My first take was to use a python level loop:
>>> from numpy import *
>>> c = array([sum((a_i - b) ** 2, axis=1) for a_i in a])
But this is too slow and allocate a useless temporary list of python references.
To avoid the python level loop I then tried to use broadcasting as follows:
>>> c = sum((a[:,newaxis,:] - b) ** 2, axis=2)
But this build a useless and huge (d1, d2, d3) temporary array that
does not fit in memory for large values of d1, d2 and d3...
Do you have any better idea? I would like to simulate a runtime
behavior similar to:
>>> c = dot(a, b.T)
but for for squared euclidian norms instead of dotproducts.
I can always write a the code in C and wrap it with ctypes but I
wondered whether this is possible only with numpy.
I'm curious about how to control compiler options for mingw builds of
numpy on windows... Specifically, I want to build binaries without SSE
support, so that they can run on older hardware.
Setting a CFLAGS variable on the command-line doesn't appear to do
anything, but perhaps appearances are deceiving.
Thanks for any suggestions -- I've googled fruitlessly for a while.
I'm trying to write a small library of differential geometry, and I
have some trouble subclassing ndarray.
I'd like an HomogeneousMatrix class that subclasse ndarray and
overloads some methods, such as inv().
Here is my first try, the inv() function and the inv_v1() method work
as expected, but the inv_v2() and inv_v3() methods do not change the
object at all. Can somebody explain me what is happening here ?
import numpy as np
inverse of an homogeneous matrix
R = H[0:3,0:3]
p = H[0:3,3:4]
return np.vstack( (np.hstack((R.T,-np.dot(R.T,p))), [0,0,0,1]))
def __new__(subtype, data=np.eye(4)):
subarr = np.array(data)
self[0:4,0:4] = htr.inv(self)
data = htr.inv(self)
self = HomogeneousMatrix(data)
self = htr.inv(self)
Thank you !
>What version of gfortran are you using (i.e. exactly which binary did
GNU Fortran (GCC) 4.4.0 20080603 (experimental) [trunk revision 136333]
>Is this a write to standard output "write (*,*) szText" ?
yes, it is.
I forgot to say that it also works with pydev in Eclipse but I'm looking for a simple interactive python shell that can execute some programs.
IPython does the job but is less friendly than IDLE in term of program editing. Anyway, I think I will use it for now.
Thanks for your reply.
On Tue, Dec 2, 2008 at 08:26, Christophe Chappet
<Christophe.Chappet(a)onera.fr <http://projects.scipy.org/mailman/listinfo/numpy-discussion>> wrote:
>/ Hi all,
/>/ I compile the followinq code using "f2py -c --fcompiler=gnu95
/>/ --compiler=mingw32" -m hello
/>/ subroutine AfficheMessage(szText)
/>/ character szText*100
/>/ write (*,*) szText
/>/ Using python console :
/>/ works fine !
/>/ I do the same in the program window of IDLE and :
/>/ - no message is displayed.
/>/ - the shell restart (or IDLE crah if launched with -n)
/>/ Same problem with PyScripter IDE. (crash)./
Announcing HDF5 for Python (h5py) 1.0
What is h5py?
HDF5 for Python (h5py) is a general-purpose Python interface to the
Hierarchical Data Format library, version 5. HDF5 is a versatile,
mature scientific software library designed for the fast, flexible
storage of enormous amounts of data.
>From a Python programmer's perspective, HDF5 provides a robust way to
store data, organized by name in a tree-like fashion. You can create
datasets (arrays on disk) hundreds of gigabytes in size, and perform
random-access I/O on desired sections. Datasets are organized in a
filesystem-like hierarchy using containers called "groups", and
accesed using the tradional POSIX /path/to/resource syntax.
This is the fourth major release of h5py, and represents the end
of the "unstable" (0.X.X) design phase.
Why should I use it?
H5py provides a simple, robust read/write interface to HDF5 data
from Python. Existing Python and NumPy concepts are used for the
interface; for example, datasets on disk are represented by a proxy
class that supports slicing, and has dtype and shape attributes.
HDF5 groups are are presented using a dictionary metaphor, indexed
A major design goal of h5py is interoperability; you can read your
existing data in HDF5 format, and create new files that any HDF5-
aware program can understand. No Python-specific extensions are
used; you're free to implement whatever file structure your application
Almost all HDF5 features are available from Python, including things
like compound datatypes (as used with NumPy recarray types), HDF5
attributes, hyperslab and point-based I/O, and more recent features
in HDF 1.8 like resizable datasets and recursive iteration over entire
The foundation of h5py is a near-complete wrapping of the HDF5 C API.
HDF5 identifiers are first-class objects which participate in Python
reference counting, and expose the C API via methods. This low-level
interface is also made available to Python programmers, and is
See the Quick-Start Guide for a longer introduction with code examples:
Where to get it
* Main website, documentation: http://h5py.alfven.org
* Downloads, bug tracker: http://h5py.googlecode.com
* The HDF group website also contains a good introduction:
* UNIX-like platform (Linux or Mac OS-X); Windows version is in
* Python 2.5 or 2.6
* NumPy 1.0.3 or later (1.1.0 or later recommended)
* HDF5 1.6.5 or later, including 1.8. Some features only available
when compiled against HDF5 1.8.
* Optionally, Cython (see cython.org) if you want to use custom install
options. You'll need version 0.9.8.1.1 or later.
About this version
Version 1.0 follows version 0.3.1 as the latest public release. The
major design phase (which began in May of 2008) is now over; the design
of the high-level API will be supported as-is for the rest of the 1.X
series, with minor enhancements.
This is the first version to support Python 2.6, and the first to use
Cython for the low-level interface. The license remains 3-clause BSD.
** This project is NOT affiliated with The HDF Group. **
Thanks to D. Dale, E. Lawrence and other for their continued support
and comments. Also thanks to the PyTables project, for inspiration
and generously providing their code to the community, and to everyone
at the HDF Group for creating such a useful piece of software.