Finding the mode of SArray

User 2568 | 3/8/2016, 6:09:23 AM

I can find mean() but not mode() for an SArray

Comments

User 1774 | 3/8/2016, 7:11:21 AM

mode is not implemented. However, an implementation utilizing the capabilities of SFrame/SArray (as opposed to using a single-threaded implementation such as numpy.mode) is this:

`python def modesa(sa, singlemode=True): """Return a mode of sa, or all modes if there are several.

single_mode: whether to return a single mode or a list of all modes (default: True)."""

import graphlab as gl
sf = gl.SFrame({"value": sa})
sf2 = sf.groupby("value", {"count": gl.aggregate.COUNT()})
max_count_index = sf2["count"].argmax()

if single_mode:
    return sf2[max_count_index]["value"]

else:
    max_count = sf2[max_count_index]["count"]
    return sf2[sf2["count"] == max_count]["value"]

sa = gl.SArray([1, 2, 2, 3, 3]) print modesa(sa) # Returns 2 print list(modesa(sa, single_mode=False)) # Returns [2, 3] `


User 2568 | 3/10/2016, 4:41:03 AM

Nice, thank you. Should be standard in GraphLab