<div dir="ltr"><div><br></div><div><br>Hi all,<br><br>Please critique my draft exploring the possibilities of adding group_by support to numpy:<br></div><div><a href="http://pastebin.com/c5WLWPbp">http://pastebin.com/c5WLWPbp</a></div>
<div><br></div><div>In nearly ever project I work on, I require group_by functionality of some sort. There are other libraries that provide this kind of functionality, such as pandas for instance, but I will try to make the case here that numpy ought to have a solid core of group_by functionality. Primarily, one may argue that the concept of grouping values by a key is far more general than a pandas dataframe. In particular, one often needs a simple one-line transient association between some keys and values, and trying to wrangle your problem into the more permanent and specialized datastructure that a dataframe is, is simply not called for.<br>
<br>As a simple compact example:</div><blockquote style="margin-right:0px"><div>key1 = list('abaabb')</div><div>key2 = np.random.randint(0,2,(6,2))</div><div>values = np.random.rand(6,3)</div><div>print group_by((key1, key2)).median(values)</div>
</blockquote><div>Points of note; we can group by arbitrary combinations of keys, and subarrays can also act as keys. group_by has a rich set of reduction functionality, which performs efficient per-group reductions, as well as various ways to split your values per group.</div>
<div> </div><div>Also, the code here has a lot of overlap with np.unique and related arraysetops. functions like np.unique are easily reimplemented using the groundwork laid out here, and also may be extended to benefit from the generalizations made, allowing for a wider variety of objects to have their unique values taken; note the axis keyword here, meaning that what is unique here are the images found along the first axis; not the elements of shuffled.</div>
<blockquote style="margin-right:0px"><div>#create a stack of images </div><div>images = np.random.rand(4,64,64)</div><div> #shuffle the images; this is a giant mess now; how to find all the original ones?</div><div> shuffled = images[np.random.randint(0,4,200)]</div>
<div> #there you go</div><div> print unique(shuffled, axis=0)</div></blockquote><div>Some more examples and unit tests can be found at the end of the module.</div><div> </div><div>Id love to hear your feedback on this. Specifically:</div>
<ul><li>Do you agree numpy would benefit from group_by functionality?</li><li>Do you have suggestions for further generalizations/extensions?</li><li>Any commentary on design decisions / implementation? </li></ul><div>Regards,<br>
Eelco Hoogendoorn</div></div>