<div dir="ltr">On Thu, Oct 3, 2013 at 4:05 PM, Moroney, Catherine M (398D) <span dir="ltr"><<a href="mailto:Catherine.M.Moroney@jpl.nasa.gov" target="_blank">Catherine.M.Moroney@jpl.nasa.gov</a>></span> wrote:<br><div class="gmail_extra">
<div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">I know I have a lot yet to learn about array striding tricks, so please<br>

pardon the triviality of this question.<br>
<br>
Here is the problem both in words and "dumb" python:<br>
<br>
I have a large NxM array that I want to break down into smaller nxn chunks<br>
where n divides evenly into both N and M.  Then I want to calculate the<br>
fraction of pixels in each nxn chunk that meets a certain criteria: say (x > 1) & (x < 2).<br></blockquote><div><br></div><div>If n divides both N and M, you can simply reshape your array to 4D, then reduce it back to 2D:<div>
<br></div><div>import numpy as np</div><div><br></div><div>N, M = 1000, 2000</div><div>n = 100</div><div><br></div><div>a = np.random.rand(N,M) * 5</div><div>a_view = a.reshape(a.shape[0] // n, n, a.shape[1] // n, n)</div>
<div>a_fractions = np.sum((a_view > 1) & (a_view < 2), axis=(1, 3)) / (n * n)</div><div><br></div><div><div>>>> a_fractions.shape</div><div>(10L, 20L)</div></div><div><br></div><div><div>>>> a_fractions</div>
<div>array([[ 0.1965,  0.1964,  0.202 ,  0.1997,  0.1976,  0.1997,  0.2026,</div><div>         0.1951,  0.2051,  0.1995,  0.1926,  0.2006,  0.1973,  0.1964,</div><div>         0.2046,  0.1977,  0.2066,  0.2009,  0.2003,  0.2013],</div>
<div>       ...</div><div>       [ 0.2028,  0.1943,  0.2036,  0.1992,  0.2   ,  0.2009,  0.1971,</div><div>         0.1996,  0.196 ,  0.196 ,  0.1983,  0.2021,  0.2031,  0.1955,</div><div>         0.1916,  0.1939,  0.202 ,  0.2064,  0.2021,  0.1954]])</div>
</div></div><div><br></div><div>Jaime</div><div><br></div><div> -- </div></div>(\__/)<br>( O.o)<br>( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes de dominación mundial.
</div></div>