Skip to content

Added how-to find mode value in SArray #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Added how-to find mode value in SArray #39

wants to merge 4 commits into from

Conversation

guy4261
Copy link

@guy4261 guy4261 commented Apr 13, 2016

Finally added how to find a mode value to the how-to repository
http://forum.dato.com/discussion/1785/finding-the-mode-of-sarray?#

@znation
Copy link
Contributor

znation commented Apr 13, 2016

@guy4261 is it worth pointing out that this could be done faster (albeit sacrificing some accuracy) using sa.sketch_summary().frequent_items()? There are some caveats there, though: won't work for very low-frequency mode values, and won't necessarily give the correct result if there are multiple likely candidates.

return sf2[sf2["count"] == max_count]["value"]

# Create an SArray with two modes (most-common elements: 2 and 3)
# sa = gl.SArray([1, 2, 2, 3, 3])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this commented-out? Should we uncomment it to make it clear that it's code you can run?

@guy4261
Copy link
Author

guy4261 commented Apr 14, 2016

@znation I followed your two comments, made it runnable (although not printing, like some of the examples) and added the sketch usage example. Also added a warning about handling empty SArrays. Please review, thanks!

raise ValueError("Can't find mode(s) in empty SArray")

frequent_items_sketch = sa.sketch_summary().frequent_items()
modes_sketch = [k for (k, v) in frequent_items_sketch.iteritems()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be slow if the list of frequent items is long (and there is not really an upper bound to how long it can be). Here it calls itervalues for each item of the dict and max on that. For perf this should be refactored to use a single call to max(itervalues) and a comparison to that stored value.

@znation
Copy link
Contributor

znation commented Apr 14, 2016

@guy4261 Thanks! One minor comment for a perf improvement, otherwise looks good to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants