Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To take full advantage of numpy's speed, you want to create <a href="http://docs.scipy.org/doc/numpy/reference/ufuncs.html" rel="nofollow noreferrer">ufuncs</a> whenever possible. Applying <code>vectorize</code> to a function as <a href="https://stackoverflow.com/a/9793070/577088">mgibsonbr</a> suggests is one way to do that, but a better way, if possible, is simply to construct a function that takes advantage of numpy's built-in ufuncs. So something like this:</p> <pre><code>&gt;&gt;&gt; import numpy &gt;&gt;&gt; a = numpy.random.random(10) &gt;&gt;&gt; a + 1 array([ 1.29738145, 1.33004628, 1.45825441, 1.46171177, 1.56863326, 1.58502855, 1.06693054, 1.93304272, 1.66056379, 1.91418473]) &gt;&gt;&gt; (a + 1) * 0.25 / 4 array([ 0.08108634, 0.08312789, 0.0911409 , 0.09135699, 0.09803958, 0.09906428, 0.06668316, 0.12081517, 0.10378524, 0.11963655]) </code></pre> <p>What is the nature of the function you want to apply across the numpy array? If you tell us, perhaps we can help you come up with a version that uses only numpy ufuncs.</p> <p>It's also possible to generate an array of indices without using <code>enumerate</code>. Numpy provides <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndenumerate.html" rel="nofollow noreferrer"><code>ndenumerate</code></a>, which is an iterator, and probably slower, but it also provides <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.indices.html" rel="nofollow noreferrer"><code>indices</code></a>, which is a very quick way to generate the indices corresponding to the values in an array. So...</p> <pre><code>&gt;&gt;&gt; numpy.indices(a.shape) array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]) </code></pre> <p>So to be more explicit, you can use the above and combine them using <code>numpy.rec.fromarrays</code>:</p> <pre><code>&gt;&gt;&gt; a = numpy.random.random(10) &gt;&gt;&gt; ind = numpy.indices(a.shape) &gt;&gt;&gt; numpy.rec.fromarrays([ind[0], a]) rec.array([(0, 0.092473494150913438), (1, 0.20853257641948986), (2, 0.35141455604686067), (3, 0.12212258656960817), (4, 0.50986868372639049), (5, 0.0011439325711705139), (6, 0.50412473457942508), (7, 0.28973489788728601), (8, 0.20078799423168536), (9, 0.34527678271856999)], dtype=[('f0', '&lt;i8'), ('f1', '&lt;f8')]) </code></pre> <p>It's starting to sound like your main concern is performing the operation in-place. That's harder to do using <code>vectorize</code> but it's easy with the ufunc approach:</p> <pre><code>&gt;&gt;&gt; def somefunc(a): ... a += 1 ... a /= 15 ... &gt;&gt;&gt; a = numpy.random.random(10) &gt;&gt;&gt; b = a &gt;&gt;&gt; somefunc(a) &gt;&gt;&gt; a array([ 0.07158446, 0.07052393, 0.07276768, 0.09813235, 0.09429439, 0.08561703, 0.11204622, 0.10773558, 0.11878885, 0.10969279]) &gt;&gt;&gt; b array([ 0.07158446, 0.07052393, 0.07276768, 0.09813235, 0.09429439, 0.08561703, 0.11204622, 0.10773558, 0.11878885, 0.10969279]) </code></pre> <p>As you can see, numpy performs these operations in-place.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload