Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy don't these two math functions return the same result?
    primarykey
    data
    text
    <p>I'm trying to use fancy indexing instead of looping to speed up a function in Numpy. To the best of my knowledge, I've implemented the fancy indexing version correctly. The problem is that the two functions (loop and fancy-indexed) do not return the same result. I'm not sure why. It's worth pointing out that the functions do return the same result if a smaller array is used (e.g., 20 x 20 x 20).</p> <p>Below I've included everything necessary to reproduce the error. If the functions do return the same result, then the line <code>find_maxdiff(data) - find_maxdiff_fancy(data)</code> should return an array full of zeroes. </p> <pre><code>from numpy import * def rms(data, axis=0): return sqrt(mean(data ** 2, axis)) def find_maxdiff(data): samples, channels, epochs = shape(data) window_size = 50 maxdiff = zeros(epochs) for epoch in xrange(epochs): signal = rms(data[:, :, epoch], axis=1) for t in xrange(window_size, alen(signal) - window_size): amp_a = mean(signal[t-window_size:t], axis=0) amp_b = mean(signal[t:t+window_size], axis=0) the_diff = abs(amp_b - amp_a) if the_diff &gt; maxdiff[epoch]: maxdiff[epoch] = the_diff return maxdiff def find_maxdiff_fancy(data): samples, channels, epochs = shape(data) window_size = 50 maxdiff = zeros(epochs) signal = rms(data, axis=1) for t in xrange(window_size, alen(signal) - window_size): amp_a = mean(signal[t-window_size:t], axis=0) amp_b = mean(signal[t:t+window_size], axis=0) the_diff = abs(amp_b - amp_a) maxdiff[the_diff &gt; maxdiff] = the_diff return maxdiff data = random.random((600, 20, 100)) find_maxdiff(data) - find_maxdiff_fancy(data) data = random.random((20, 20, 20)) find_maxdiff(data) - find_maxdiff_fancy(data) </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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