9blue
9blue

Reputation: 4753

Given a list of numbers, find a fix range that can cover the most numbers

in python, we can use counter to find the most common element in a list. Is there a way we can pass in the function, so that we can counter the element that falls in certain range.

Say I have [123, 127, 99,75,86, 83,81], I want to return the something like {'12X':2, '8X':3, '99':1, '75':1}

Any ideas?

Upvotes: 0

Views: 68

Answers (1)

Julien Palard
Julien Palard

Reputation: 11576

What you're trying to do, depending on the context, may be:

Binning

That's grouping your elements in discret bins / buckets, either of equal size, or predefined. That's easily done using libraries like Pandas:

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.cut.html

>>> pandas.cut([123, 127, 99, 75, 86, 83, 81], 4, labels=False)
array([3, 3, 1, 0, 0, 0, 0])

(Which reads "123 and 127 are in group 3, 99 is in groupe one, and 75, 86, 83, 81 in group 0").

Clustering

That's grouping your elements in clusters, when you don't know the values, but know that they form groups.

Like "1, 2, 3, 11, 12, 13" is clearly two clusters "1, 2, 3" and "11, 12, 13".

A nice and easy way to cluster data is K-means but there's other algorithms, you should take a look at scipy.

>>> import sklearn.cluster
>>> import numpy as np
>>> 
>>> kmeans = sklearn.cluster.KMeans(n_clusters=4)
>>> kmeans.fit_predict(numpy.array([123, 127, 99, 75, 86, 83, 81]).reshape(-1, 1))
array([0, 0, 2, 3, 1, 1, 1], dtype=int32)

This one gave 123 and 127 together, 99 alone, 75 alone, 81 83 86 together.

Grouping

That's a simple "GROUP BY" ala SQL, when you can provide a function that returns the group where your value should be:

Like:

>>> from itertools import groupby
>>> for key, group in groupby([123, 127, 99, 75, 86, 83, 81], lambda x: int(x / 10)):
...     print(key, list(group))
... 
12 [123, 127]
9 [99]
7 [75]
8 [86, 83, 81]

Upvotes: 2

Related Questions