<div dir="ltr"><div>Its pretty easy to implement this table functionality and more on top of the code I linked above. I still think such a comprehensive overhaul of arraysetops is worth discussing.<br><br>import numpy as np<br>
import grouping</div><div>x = [1, 1, 1, 1, 2, 2, 2, 2, 2]<br>y = [3, 4, 3, 3, 3, 4, 5, 5, 5]<br>z = np.random.randint(0,2,(9,2))</div><div>def table(*keys):<br>    """<br>    desired table implementation, building on the index object<br>
    cleaner, and more functionality<br>    performance should be the same<br>    """<br>    indices  = [grouping.as_index(k, axis=0) for k in keys]<br>    uniques  = [i.unique  for i in indices]<br>    inverses = [i.inverse for i in indices]<br>
    shape    = [i.groups  for i in indices]<br>    t = np.zeros(shape, <a href="http://np.int">np.int</a>)<br>    <a href="http://np.add.at">np.add.at</a>(t, inverses, 1)<br>    return tuple(uniques), t</div><div>#here is how to use<br>
print table(x,y)<br>#but we can use fancy keys as well; here a composite key and a row-key<br>print table((x,y), z)</div><div>#this effectively creates a sparse matrix equivalent of your desired table<br>print grouping.count((x,y))<br>
</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Aug 13, 2014 at 11:25 PM, Warren Weckesser <span dir="ltr"><<a href="mailto:warren.weckesser@gmail.com" target="_blank">warren.weckesser@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote"><div>On Wed, Aug 13, 2014 at 5:15 PM, Benjamin Root <span dir="ltr"><<a href="mailto:ben.root@ou.edu" target="_blank">ben.root@ou.edu</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;padding-left:1ex;border-left-color:rgb(204,204,204);border-left-width:1px;border-left-style:solid"><div dir="ltr">The ever-wonderful pylab mode in matplotlib has a table function for plotting a table of text in a plot. If I remember correctly, what would happen is that matplotlib's table() function will simply obliterate the numpy's table function. This isn't a show-stopper, I just wanted to point that out.<br>



<br>Personally, while I wasn't a particular fan of "count_unique" because I wouldn't necessarially think of it when needing a contingency table, I do like that it is verb-ish. "table()", in this sense, is not a verb. That said, I am perfectly fine with it if you are fine with the name collision in pylab mode.<br>



</div><div><div><div class="gmail_extra"><br></div></div></div></blockquote><div><br><br></div></div><div>Thanks for pointing that out.  I only changed it to have something that sounded more table-ish, like the Pandas, R and Matlab functions.   I won't update it right now, but if there is interest in putting it into numpy, I'll rename it to avoid the pylab conflict.  Anything along the lines of `crosstab`, `xtable`, etc., would be fine with me.<span class="HOEnZb"><font color="#888888"><br>

<br></font></span></div><span class="HOEnZb"><font color="#888888"><div>Warren<br><br><br></div></font></span><div><div class="h5"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;padding-left:1ex;border-left-color:rgb(204,204,204);border-left-width:1px;border-left-style:solid">
<div><div><div class="gmail_extra"><br><div class="gmail_quote">
On Wed, Aug 13, 2014 at 4:57 PM, Warren Weckesser <span dir="ltr"><<a href="mailto:warren.weckesser@gmail.com" target="_blank">warren.weckesser@gmail.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;padding-left:1ex;border-left-color:rgb(204,204,204);border-left-width:1px;border-left-style:solid"><div dir="ltr"><div><div><br><div class="gmail_extra"><br>
<br><div class="gmail_quote">On Tue, Aug 12, 2014 at 12:51 PM, Eelco Hoogendoorn <span dir="ltr"><<a href="mailto:hoogendoorn.eelco@gmail.com" target="_blank">hoogendoorn.eelco@gmail.com</a>></span> wrote:<br>



<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;padding-left:1ex;border-left-color:rgb(204,204,204);border-left-width:1px;border-left-style:solid"><div dir="ltr"><div>ah yes, that's also an issue I was trying to deal with. the semantics I prefer in these type of operators, is (as a default), to have every array be treated as a sequence of keys, so if calling unique(arr_2d), youd get unique rows, unless you pass axis=None, in which case the array is flattened.<br>





<br>I also agree that the extension you propose here is useful; but ideally, with a little more discussion on these subjects we can converge on an even more comprehensive overhaul</div></div><div class="gmail_extra"><br>




<br>
<div class="gmail_quote"><div><div>On Tue, Aug 12, 2014 at 6:33 PM, Joe Kington <span dir="ltr"><<a href="mailto:joferkington@gmail.com" target="_blank">joferkington@gmail.com</a>></span> wrote:<br></div>
</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;padding-left:1ex;border-left-color:rgb(204,204,204);border-left-width:1px;border-left-style:solid"><div><div>
<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote"><div>On Tue, Aug 12, 2014 at 11:17 AM, Eelco Hoogendoorn <span dir="ltr"><<a href="mailto:hoogendoorn.eelco@gmail.com" target="_blank">hoogendoorn.eelco@gmail.com</a>></span> wrote:<br>






<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;padding-left:1ex;border-left-color:rgb(204,204,204);border-left-width:1px;border-left-style:solid"><div dir="ltr"><div>Thanks. Prompted by that stackoverflow question, and similar problems I had to deal with myself, I started working on a much more general extension to numpy's functionality in this space. Like you noted, things get a little panda-y, but I think there is a lot of panda's functionality that could or should be part of the numpy core, a robust set of grouping operations in particular.<br>







<br></div><div>see pastebin here:<br><a href="http://pastebin.com/c5WLWPbp" target="_blank">http://pastebin.com/c5WLWPbp</a></div></div></blockquote></div><div><br><div>On a side note, this is related to a pull request of 
mine from awhile back: <a href="https://github.com/numpy/numpy/pull/3584" target="_blank">https://github.com/numpy/numpy/pull/3584</a><br><br></div>There was a lot of disagreement on the mailing list about what to call a "unique slices along a given axis" function, so I wound up closing the pull request pending more discussion.  <br>






<br></div><div>At any rate, I think it's a useful thing to have in "base" numpy.<br></div></div></div></div>
<br></div></div><div>_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org" target="_blank">NumPy-Discussion@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
<br></div></blockquote></div><br></div>
<br>_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org" target="_blank">NumPy-Discussion@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
<br></blockquote></div><br><br></div></div></div><div class="gmail_extra">Update: I renamed the function to `table` in the pull request: <a href="https://github.com/numpy/numpy/pull/4958" target="_blank">https://github.com/numpy/numpy/pull/4958</a><span><font color="#888888"><br>




<br><br></font></span></div><span><font color="#888888"><div class="gmail_extra">Warren</div></font></span></div>
<br>_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org" target="_blank">NumPy-Discussion@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
<br></blockquote></div><br></div>
</div></div><br>_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org" target="_blank">NumPy-Discussion@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
<br></blockquote></div></div></div><br></div></div>
<br>_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
<br></blockquote></div><br></div>